でjavax.annotation.processing
、パッケージのインタフェースがあるProcessor
機能が存在するが:
/**
* Processes a set of annotation types on type elements
* originating from the prior round and returns whether or not
* these annotation types are claimed by this processor. If {@code
* true} is returned, the annotation types are claimed and subsequent
* processors will not be asked to process them; if {@code false}
* is returned, the annotation types are unclaimed and subsequent
* processors may be asked to process them. A processor may
* always return the same boolean value or may vary the result
* based on chosen criteria.
*
* <p>The input set will be empty if the processor supports {@code
* "*"} and the root elements have no annotations. A {@code
* Processor} must gracefully handle an empty set of annotations.
*
* @param annotations the annotation types requested to be processed
* @param roundEnv environment for information about the current and prior round
* @return whether or not the set of annotation types are claimed by this processor
*/
boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv);
Java APIのAbstractProcessor
インターフェース上で実装しています。今、私は自分のプロセッサクラスを作成しました:
public class MyProcessor extends AbstractProcessor {
...
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation: annotations) {
// How can I get the class of the annotation ?
}
}
}
私の質問:
- APIドキュメントは、私に告げる
annotations
処理機能にされています
処理されるように要求された注釈の種類
次に、なぜそれがタイプでありTypeElement
ませんかjava.lang.annotation.Annotation
?私は確信しているかどうかではないですので、私はこれで混乱annotations
実際に注釈を付けされている要素または要素に注釈を付ける実際の注釈を意味します。
- そのため上記の私の第一の質問の、どのように私はから各注釈のクラスを取得することができますか
TypeElement
?
パッケージがありjavax.lang.model
、ミラーベースの設計はで説明した次のデザイン原則をオブジェクト指向プログラミング言語のメタレベルの施設のために:ミラーは。これは、(これに限定されない)注釈処理フレームワークで使用される大きく設計抽象です。
-
次に、なぜそれがタイプであり
TypeElement
ませんかjava.lang.annotation.Annotation
?あなたは、フレームワークを設計するとき、それはそれを維持することが重要ですオープン(延長のために利用可能)。あなたは言語の将来のバージョンがもたらすかを知ることはありません。あなたは、メタデータの新しいフォームが表示されることがありますと仮定柔軟に滞在する必要があります。
私は確信して注釈が実際に注釈を付けされている要素または要素に注釈を付ける実際の注釈を意味するかどうかではないですので、私はこれで混乱します。
アノテーションの種類を処理しているので、それは「要素を注釈する本当の注釈」です。
for (TypeElement typeElement : annotations) { // it's likely the ElementKind.ANNOTATION_TYPE typeElement.getKind(); // elements annotated with the annotation environment.getElementsAnnotatedWith(typeElement); }
-
そのため上記の私の第一の質問の、どのように私はから各注釈のクラスを取得することができますか
TypeElement
?TypeElement
表しあなたのアノテーションクラスを。
読むには:
https://bracha.org/mirrors.pdf(それはプロセスに多くの時間がかかります)
https://www.baeldung.com/java-annotation-processing-builder(それは簡単な注釈プロセッサです)
https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/core/AnnotationProcessor.java(それは良いことだ実用的な例)
Javadocのパッケージとのようなコアクラス
Processor
、AnnotatedConstruct
、Element
、TypeElement
、TypeMirror
。