运行注释处理器时,您无权访问已编译的类。注释处理的要点是它发生在预编译时。
相反,您需要创建一个专门处理注释类型的注释处理器,然后使用镜像 API 访问该字段。例如:
@SupportedAnnotationTypes("com.example.MyAnnotation")
public class CompileTimeAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
// Only one annotation, so just use annotations.iterator().next();
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(
annotations.iterator().next());
Set<VariableElement> fields = ElementFilter.fieldsIn(elements);
for (VariableElement field : fields) {
TypeMirror fieldType = field.asType();
String fullTypeClassName = fieldType.toString();
// Validate fullTypeClassName
}
return true;
}
}
对于验证,不能使用任何尚未编译的类(包括即将使用注释编译的类),例如 。对于这些,必须仅使用字符串。这是因为注释处理发生在称为“源代码生成”的预编译阶段,该阶段允许您在编译器使用注释运行之前生成源代码。MyType.class
验证字段类型是否为(已编译)的示例验证:java.lang.String
for (VariableElement field : fields) {
TypeMirror fieldType = field.asType();
String fullTypeClassName = fieldType.toString();
if (!String.class.getName().equals(fullTypeClassName)) {
processingEnv.getMessager().printMessage(
Kind.ERROR, "Field type must be java.lang.String", field);
}
}
资源
编辑:
我想获取字段类型以获取该类型的注释。但这似乎是不可能的吗?
这确实是可能的!这可以在以下位置使用更多方法完成:TypeMirror
if (fieldType.getKind() != TypeKind.DECLARED) {
processingEnv.getMessager().printMessage(
Kind.ERROR, "Field cannot be a generic type.", field);
}
DeclaredType declaredFieldType = (DeclaredType) fieldType;
TypeElement fieldTypeElement = (TypeElement) declaredFieldType.asElement();
在这里,您有两个选择:
- 如果您尝试查找的注释已经编译(即它来自另一个库),则可以直接引用该类来获取注释。
- 如果您尝试查找的注释未编译(即,它在当前调用中编译,以运行APT),则可以通过实例引用它。
javac
AnnotationMirror
已编译
DifferentAnnotation diffAnn = fieldTypeElement.getAnnotation(
DifferentAnnotation.class);
// Process diffAnn
非常直接,这使您可以直接访问注释本身。
未编译
请注意,无论是否编译注释,此解决方案都可以工作,它只是不如上面的代码干净。
以下是我曾经写过的几个方法,用于按类名从注释镜像中提取某个值:
private static <T> T findAnnotationValue(Element element, String annotationClass,
String valueName, Class<T> expectedType) {
T ret = null;
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
DeclaredType annotationType = annotationMirror.getAnnotationType();
TypeElement annotationElement = (TypeElement) annotationType
.asElement();
if (annotationElement.getQualifiedName().contentEquals(
annotationClass)) {
ret = extractValue(annotationMirror, valueName, expectedType);
break;
}
}
return ret;
}
private static <T> T extractValue(AnnotationMirror annotationMirror,
String valueName, Class<T> expectedType) {
Map<ExecutableElement, AnnotationValue> elementValues = new HashMap<ExecutableElement, AnnotationValue>(
annotationMirror.getElementValues());
for (Entry<ExecutableElement, AnnotationValue> entry : elementValues
.entrySet()) {
if (entry.getKey().getSimpleName().contentEquals(valueName)) {
Object value = entry.getValue().getValue();
return expectedType.cast(value);
}
}
return null;
}
假设您正在寻找注释,并且您的源代码如下所示:DifferentAnnotation
@DifferentAnnotation(name = "My Class")
public class MyClass {
@MyAnnotation
private String field;
// ...
}
此代码将打印:My Class
String diffAnnotationName = findAnnotationValue(fieldTypeElement,
"com.example.DifferentAnnotation", "name", String.class);
System.out.println(diffAnnotationName);