Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查注释处理器上编译时的类层次结构_Java_Annotations_Jvm_Annotation Processing - Fatal编程技术网

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