关于 Processor 接口的 process(...) 方法中定义的参数

About the parameter defined in process(...) method of Processor interface

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

JavaAPIAbstractProcessor实现以上接口。现在我创建了自己的处理器 class:

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

the the annotation types requested to be processed

那么,为什么类型是 TypeElement 而不是 java.lang.annotation.Annotation ?我对此感到困惑,因为我不确定 annotations 实际上是指被注释的元素还是真正的注释元素。

  1. 由于我上面的第一个问题,我如何从 TypeElement 中获取每个注释的 class?

有一个包 javax.lang.model 遵循 Mirrors: Design Principles for Meta-level Facilities 中描述的基于镜像的设计 面向对象编程语言。它是一个经过精心设计的抽象,用于(但不限于)注释处理框架。

  1. Then, why is it with type TypeElement not java.lang.annotation.Annotation?

    设计框架时,保持它 open(可用于扩展)很重要。您永远不知道该语言的未来版本会带来什么。假设可能出现新形式的元数据,您需要保持灵活性。

    I get confused by this because I am not sure whether the annotations actually mean the elements that being annotated or the real annotations annotating elements.

    它是 "the real annotations annotating elements",因为您正在处理注释类型。

    for (TypeElement typeElement : annotations) {
        // it's likely the ElementKind.ANNOTATION_TYPE
        typeElement.getKind();
    
        // elements annotated with the annotation
        environment.getElementsAnnotatedWith(typeElement);
    }
    
  2. Because of my 1st question above, how can I get each annotation's class from the TypeElement?

    TypeElement代表你的注解class。

阅读:

  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 (it's a good practical 示例)

  4. the package and the core classes such as Processor, AnnotatedConstruct, Element, TypeElement, TypeMirror 的 Javadoc。