Java 使用同一处理器实例处理不同的注释

Java 使用同一处理器实例处理不同的注释,java,annotations,annotation-processing,Java,Annotations,Annotation Processing,我们的项目中有两个注释,我想收集注释的类,并基于这两个类列表创建一个合并输出 只有一个处理器实例才能实现这一点吗?如何知道是否每个带注释的类都调用了处理器实例?框架只调用处理器.process方法一次(每轮),您可以通过传递的RoundEnvironment参数同时访问这两个列表。因此,您可以在同一个process方法调用中处理这两个列表 要执行此操作,请列出SupportedAnnotationTypes注释中的两个注释: @SupportedAnnotationTypes({ "h

我们的项目中有两个注释,我想收集注释的类,并基于这两个类列表创建一个合并输出


只有一个
处理器
实例才能实现这一点吗?如何知道是否每个带注释的类都调用了
处理器
实例?

框架只调用
处理器.process
方法一次(每轮),您可以通过传递的
RoundEnvironment
参数同时访问这两个列表。因此,您可以在同一个
process
方法调用中处理这两个列表

要执行此操作,请列出
SupportedAnnotationTypes
注释中的两个注释:

@SupportedAnnotationTypes({ 
    "hu.palacsint.annotation.MyAnnotation", 
    "hu.palacsint.annotation.MyOtherAnnotation" 
})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Processor extends AbstractProcessor { ... }
下面是一个示例
过程
方法:

@Override
public boolean process(final Set<? extends TypeElement> annotations, 
        final RoundEnvironment roundEnv) {
    System.out.println("   > ---- process method starts " + hashCode());
    System.out.println("   > annotations: " + annotations);

    for (final TypeElement annotation: annotations) {
        System.out.println("   >  annotation: " + annotation.toString());
        final Set<? extends Element> annotateds = 
            roundEnv.getElementsAnnotatedWith(annotation);
        for (final Element element: annotateds) {
            System.out.println("      > class: " + element);
        }
    }
    System.out.println("   > processingOver: " + roundEnv.processingOver());
    System.out.println("   > ---- process method ends " + hashCode());
    return false;
}
它打印使用
MyAnnotation
MyOtherAnnotation
注释注释的所有类

   > ---- process method starts 21314930
   > annotations: [hu.palacsint.annotation.MyOtherAnnotation, hu.palacsint.annotation.MyAnnotation]
   >  annotation: hu.palacsint.annotation.MyOtherAnnotation
      > class: hu.palacsint.annotation.p2.OtherClassOne
   >  annotation: hu.palacsint.annotation.MyAnnotation
      > class: hu.palacsint.annotation.p2.ClassTwo
      > class: hu.palacsint.annotation.p3.ClassThree
      > class: hu.palacsint.annotation.p1.ClassOne
   > processingOver: false
   > ---- process method ends 21314930
   > ---- process method starts 21314930
   > roots: []
   > annotations: []
   > processingOver: true
   > ---- process method ends 21314930