В пакете 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
?