プロセッサインタフェースのプロセスで定義されたパラメータについて(...)メソッド

Leem.fin:

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

私の質問:

  1. APIドキュメントは、私に告げるannotations処理機能にされています

処理されるように要求された注釈の種類

次に、なぜそれがタイプでありTypeElementませんかjava.lang.annotation.Annotation私は確信しているかどうかではないですので、私はこれで混乱annotations実際に注釈を付けされている要素または要素に注釈を付ける実際の注釈を意味します。

  1. そのため上記の私の第一の質問の、どのように私はから各注釈のクラスを取得することができますかTypeElement
アンドリューTobilko:

パッケージがありjavax.lang.model、ミラーベースの設計はで説明した次のデザイン原則をオブジェクト指向プログラミング言語のメタレベルの施設のために:ミラーはこれは、(これに限定されない)注釈処理フレームワークで使用される大きく設計抽象です。

  1. 次に、なぜそれがタイプであり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);
    }
    
  2. そのため上記の私の第一の質問の、どのように私はから各注釈のクラスを取得することができますかTypeElement

    TypeElement 表しあなたのアノテーションクラスを。

読むには:

  1. https://bracha.org/mirrors.pdf(それはプロセスに多くの時間がかかります)

  2. https://www.baeldung.com/java-annotation-processing-builder(それは簡単な注釈プロセッサです)

  3. https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/core/AnnotationProcessor.java(それは良いことだ実用的な例)

  4. JavadocのパッケージとのようなコアクラスProcessorAnnotatedConstructElementTypeElementTypeMirror

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=311625&siteId=1