如何在注释处理器中获取注释参数
How to get annotation parameter in annotation processor
我正在编写我自己的注解处理器,我正在尝试获取我的注解参数,就像下面代码中的处理方法:
roundEnv.getElementsAnnotatedWith(annotation).forEach {
val annotation = it.getAnnotation(annotation)
annotation.interfaces
}
我在构建过程中得到的是 An exception occurred: javax.lang.model.type.MirroredTypesException: Attempt to access Class objects for TypeMirrors []
。有人知道如何获取注释数据吗?
关于 getAnnotation
方法的文档解释了为什么 Class<?>
对象对于注释处理器有问题:
The annotation returned by this method could contain an element whose value is of type Class. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a Class object by invoking the relevant method on the returned annotation will result in a MirroredTypeException, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.
要访问 类 等注释元素,您需要改为使用 Element.getAnnotationMirrors()
并手动查找感兴趣的注释。这些注释镜像将包含表示实际值的元素,但不需要存在所讨论的 类。
这个 blog post 似乎是如何做到这一点的规范来源。它引用了一个 Sun 论坛讨论,并在许多注释处理器中被引用。
对于以下注释:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
Class<?> value();
}
类型Class<?>
的字段可以通过此代码访问:
for (ExecutableElement ee : ElementFilter.methodsIn(te.getEnclosedElements())) {
Action action = ee.getAnnotation(Action.class);
if (action == null) {
// Look for the overridden method
ExecutableElement oe = getExecutableElement(te, ee.getSimpleName());
if (oe != null) {
action = oe.getAnnotation(Action.class);
}
}
TypeMirror value = null;
if (action != null) {
try {
action.value();
} catch (MirroredTypeException mte) {
value = mte.getTypeMirror();
}
}
System.out.printf(“ % s Action value = %s\n”,ee.getSimpleName(), value);
}
我正在编写我自己的注解处理器,我正在尝试获取我的注解参数,就像下面代码中的处理方法:
roundEnv.getElementsAnnotatedWith(annotation).forEach {
val annotation = it.getAnnotation(annotation)
annotation.interfaces
}
我在构建过程中得到的是 An exception occurred: javax.lang.model.type.MirroredTypesException: Attempt to access Class objects for TypeMirrors []
。有人知道如何获取注释数据吗?
关于 getAnnotation
方法的文档解释了为什么 Class<?>
对象对于注释处理器有问题:
The annotation returned by this method could contain an element whose value is of type Class. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a Class object by invoking the relevant method on the returned annotation will result in a MirroredTypeException, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.
要访问 类 等注释元素,您需要改为使用 Element.getAnnotationMirrors()
并手动查找感兴趣的注释。这些注释镜像将包含表示实际值的元素,但不需要存在所讨论的 类。
这个 blog post 似乎是如何做到这一点的规范来源。它引用了一个 Sun 论坛讨论,并在许多注释处理器中被引用。
对于以下注释:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
Class<?> value();
}
类型Class<?>
的字段可以通过此代码访问:
for (ExecutableElement ee : ElementFilter.methodsIn(te.getEnclosedElements())) {
Action action = ee.getAnnotation(Action.class);
if (action == null) {
// Look for the overridden method
ExecutableElement oe = getExecutableElement(te, ee.getSimpleName());
if (oe != null) {
action = oe.getAnnotation(Action.class);
}
}
TypeMirror value = null;
if (action != null) {
try {
action.value();
} catch (MirroredTypeException mte) {
value = mte.getTypeMirror();
}
}
System.out.printf(“ % s Action value = %s\n”,ee.getSimpleName(), value);
}