Java 检查注释处理器上编译时的类层次结构
我正在编写注释处理器,以便在编译时执行以下检查:Java 检查注释处理器上编译时的类层次结构,java,annotations,jvm,annotation-processing,Java,Annotations,Jvm,Annotation Processing,我正在编写注释处理器,以便在编译时执行以下检查: 有一个接口E 有一个注释@Apply,用于注释方法 用@Apply注释的方法应被称为Apply,并且只取实现E 我已经识别了所有被称为apply的带注释方法,并提取了它们作为参数的类的名称。所以我只剩下: Element method // this is the Element that represents the `apply` method TypeMirror mirror //method's TypeMirror Str
- 有一个接口
E
- 有一个注释
,用于注释方法@Apply
- 用
注释的方法应被称为@Apply
,并且只取实现Apply
E
apply
的带注释方法,并提取了它们作为参数的类的名称。所以我只剩下:
Element method // this is the Element that represents the `apply` method
TypeMirror mirror //method's TypeMirror
String paramClass // the parameter's class Name.
问题是:如果有的话,我如何从这些参数中获取类层次结构表示,以便检查它是否实现了E
。
无法使用
ClassLoader.loadClass
,因为该类尚不存在,但我只需要类层次结构。使用javax.lang.model.util.Types.IsubType()方法很简单:
TypeMirror parameterTypes = mirror.getParameterTypes();
TypeMirror typeOfE = processingEnv.getElementUtils().getTypeElement(E.class.getName()).asType();
boolean isSubTypeOfE = processingEnv.getTypeUtils().isSubtype(parameterType, eventType)
这样,我就可以检查由mirror
表示的方法的参数类是否是所需类型E