如果MyAnnotation 是您的处理器支持的注释,那么您只需编写如下内容:
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment env) {
if (shouldClaim(annotations)) {
for (Element e : env.getElementsAnnotatedWith(MyAnnotation.class)) {
MyAnnotation a = e.getAnnotation(MyAnnotation.class);
String str1 = a.strNumberOne();
String str2 = a.strNumberTwo();
String str3 = a.strNumberThree();
// Add them to a List or whatever you need.
}
return true;
}
return false;
}
private boolean shouldClaim(Set<? extends TypeElement> annotations) {
Set<String> supported = getSupportedAnnotationTypes();
for (TypeElement a : annotations) {
if (supported.contains(a.getQualifiedName().toString()))
return true;
}
return false;
}
shouldClaim 方法的逻辑由process 的文档解释。如果您的注释支持例如,它会更复杂。 * 或 name.* 形式的类型,但一般情况下您不会。 (有关这些含义的描述,请参阅 getSupportedAnnotationTypes。)
如果MyAnnotation不是你的处理器支持的注解,那么你将需要通过getElementValuesWithDefaults如果它是在您正在编译的包中声明的类型。因为注解处理发生在在编译期间,正在编译的源文件尚不存在类文件,这就是我们改用Element API 的原因。
Element 表示某种声明,例如类、方法或变量。 TypeElement 表示类、接口、枚举或注解类型声明。 TypeElement 与 Class 的用途相似,只是我们可以将 TypeElement 用于不一定要编译的类。
要通过元素 API 获取注释值,您需要执行以下操作:
Elements elements = processingEnv.getElementUtils();
TypeElement myAnnotation = elements.getTypeElement("com.example.MyAnnotation");
for (Element e : env.getElementsAnnotatedWith(myAnnotation)) {
for (AnnotationMirror mirror : e.getAnnotationMirrors()) {
DeclaredType annotationType = mirror.getAnnotationType();
Element annotationDecl = annotationType.asElement();
if (myAnnotation.equals(annotationDecl)) {
Map<? extends ExecutableElement, ? extends AnnotationValue> values =
elements.getAnnotationValuesWithDefaults(mirror);
String str1 = (String) getValue(values, "strNumberOne");
String str2 = (String) getValue(values, "strNumberTwo");
String str3 = (String) getValue(values, "strNumberThree");
// ...
}
}
}
private Object getValue(Map<? extends ExecutableElement,
? extends AnnotationValue> values,
String name) {
for (Map.Entry<? extends ExecutableElement,
? extends AnnotationValue> e : values.entrySet()) {
if (name.contentEquals(e.getKey().getSimpleName()))
return e.getValue().getValue();
}
return null;
}
这很痛苦,但我们只需要使用元素 API如果我们感兴趣的注解是正在编译的类之一。
我们可能还想找到AnnotationMirror 和/或AnnotationValue 以使用Messager.printMessage 重载之一在特定元素上生成某种消息,该重载将上述对象之一作为参数。