我正在编写我的第一个注解处理器,在一些看似微不足道的事情上遇到了麻烦,但我找不到任何有关它的信息。
我用我的注解注释了一个元素
@MyAnnotation String property;
当我在处理器中将此属性作为元素获取时,似乎无法以任何方式获取元素的类型。在这种情况下,需要获取表示字符串的类或TypeElement实例。
我尝试用Class.forName()
实例化容器类型的类对象,但它抛出了一个ClassNotFoundException。我想这是因为我不能访问包含类的类加载器?
发布于 2013-07-16 02:08:10
在运行批注处理器时,您不能访问已编译的类。批注处理的要点是它是预编译的。
相反,您需要创建一个专门处理您的注释类型的注释处理器,然后使用镜像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);
}
}
资源
javax.lang.model
下的Java8中标准化,旧的应用程序接口已弃用。有关详细信息,请参阅this blog post。如果您一直在使用javax
类,那么就不需要使用worry.
编辑:
我想要获取字段类型,以获得该类型的注释。但这似乎不太可能?
这确实是可能的!这可以在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();
从这里开始,您有两个选择:
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);
https://stackoverflow.com/questions/17660469
复制相似问题