我有这个:
public class Bootstrapper {
public static List<Class<? extends Annotation>> annots = Arrays.asList(
NotNull.class,
ColumnType.class,
RuntimeType.class,
DefaultValue.class
);
}
然后在静态方法中调用它:
for(Class<? extends Annotation> x: Bootstrapper.annots){
if (field.isAnnotationPresent((Class)x)) {
Object value= field.getAnnotation(x).value(); // doesn't compile :(
}
}
但随后出现此编译错误:
[错误]无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (默认编译)在项目vertx-start-project上:编译失败 [错误] /home/oleg/codes/oresoftware/vertx.api/src/main/java/huru/query/Bootstrapper.java:[55,50] 找不到符号[ERROR]符号:方法value()[ERROR]
位置:接口java.lang.annotation.Annotation
但是如果我对注释进行硬编码,它将编译:
Object value= field.getAnnotation(ColumnType.class).value(); // compiles!
我查看了源代码,field.getAnnotation()方法如下:
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return (Annotation)annotationClass.cast(this.declaredAnnotations().get(annotationClass));
}