プロジェクトには 2 つの注釈があり、注釈付きのクラスを収集し、クラスの両方のリストに基づいてマージされた出力を作成したいと考えています。
Processor
これは 1 つのインスタンスのみで可能ですか? Processor
インスタンスがすべての注釈付きクラスで呼び出されたかどうかを知るにはどうすればよいですか?
プロジェクトには 2 つの注釈があり、注釈付きのクラスを収集し、クラスの両方のリストに基づいてマージされた出力を作成したいと考えています。
Processor
これは 1 つのインスタンスのみで可能ですか? Processor
インスタンスがすべての注釈付きクラスで呼び出されたかどうかを知るにはどうすればよいですか?
フレームワークは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
。