简体   繁体   English

使用反射检查方法是否是接口的重写方法

[英]Checking if a method is an overridden method of Interface using reflection

I am trying to auto generate some EJB service code, which are wrappers around Java DAO classes. 我试图自动生成一些EJB服务代码,它们是Java DAO类的包装器。 DAO classes implement DAO interfaces, but also have their own public methods. DAO类实现DAO接口,但也具有自己的公共方法。 This DAO layer is implemented by another team, so I cannot play around with it. 这个DAO层是由另一个团队实现的,因此我无法使用它。

I am using CodeModel API to generate the code. 我正在使用CodeModel API生成代码。 I get each DAO class and want now to create the EJB Service code. 我得到了每个DAO类,现在想创建EJB Service代码。 Using java reflection I am trying to check if the method declared in the DAO class is an overridden implementing method of the interface or not. 使用Java反射,我正在尝试检查DAO类中声明的方法是否是接口的重写实现方法。 Is there anyway in which I can check that? 无论如何,我可以检查一下吗?

1) If overridden methods are with @Overridden annotation, than you could iterate through these methods, and check their annotation using this API: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Method.html#getDeclaredAnnotations%28%29 1)如果重写的方法带有@Overridden批注,则可以迭代这些方法,并使用以下API检查其批注: http : //docs.oracle.com/javase/1.5.0/docs/api/java/lang /reflect/Method.html#getDeclaredAnnotations%28%29

2) If there are no annotations, I think, the only way is to iterate through parent classes and interfaces and compare method signatures, declared there with signatures in your class. 2)如果没有注释,我认为,唯一的方法是遍历父类和接口并比较方法签名,这些签名与您的类中的签名进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM