1

I have this:

public class Bootstrapper {
  public static List<Class<? extends Annotation>> annots = Arrays.asList(
    NotNull.class,
    ColumnType.class,
    RuntimeType.class,
    DefaultValue.class
  );
}

and then this is called in a static method:

  for(Class<? extends Annotation> x: Bootstrapper.annots){
      if (field.isAnnotationPresent((Class)x)) {
        Object value=  field.getAnnotation(x).value();  // doesn't compile :(
      }
  }

but then I get this compilation error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project vertx-start-project: Compilation failure [ERROR] /home/oleg/codes/oresoftware/vertx.api/src/main/java/huru/query/Bootstrapper.java:[55,50] cannot find symbol [ERROR] symbol: method value() [ERROR]
location: interface java.lang.annotation.Annotation

however it will compile if I hardcode an annotation:

Object value=  field.getAnnotation(ColumnType.class).value();  // compiles!

I looked at the source, the field.getAnnotation() method looks like:

  public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
    Objects.requireNonNull(annotationClass);
    return (Annotation)annotationClass.cast(this.declaredAnnotations().get(annotationClass));
  }
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

0 Answers0