2

プロジェクトには 2 つの注釈があり、注釈付きのクラスを収集し、クラスの両方のリストに基づいてマージされた出力を作成したいと考えています。

Processorこれは 1 つのインスタンスのみで可能ですか? Processorインスタンスがすべての注釈付きクラスで呼び出されたかどうかを知るにはどうすればよいですか?

4

1 に答える 1

3

フレームワークはProcessor.processメソッドを (ラウンドごとに) 1 回だけ呼び出し、渡されたパラメーターを介して同時に両方のリストにアクセスできRoundEnvironmentます。processしたがって、同じメソッド呼び出しで両方のリストを処理できます。

これを行うには、注釈に両方の注釈をリストしますSupportedAnnotationTypes

@SupportedAnnotationTypes({ 
    "hu.palacsint.annotation.MyAnnotation", 
    "hu.palacsint.annotation.MyOtherAnnotation" 
})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Processor extends AbstractProcessor { ... }

サンプルprocessメソッドは次のとおりです。

@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;
}

そしてその出力:

   > ---- 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

MyAnnotationまたは で注釈が付けられたすべてのクラスを出力しますMyOtherAnnotation

于 2012-07-29T21:20:34.880 に答える