Why the follow line produce a compile error in Java? Or how I can write the correct generics syntax?
Class<? extends Annotation> annotation = annotations[i];
Class<? extends Annotation> anno = javaClass.getAnnotation(annotation);
The signature of the method is:
public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
Compile error from Eclipse:
Type mismatch: cannot convert from capture#5-of ? extends Annotation to Class<? extends Annotation>
Compile error from javac:
incompatible types
Class<? extends Annotation> anno = javaClass.getAnnotation(annotation);
^
required: Class<? extends Annotation>
found: CAP#1
where CAP#1 is a fresh type-variable:
CAP#1 extends Annotation from capture of ? extends Annotation
getAnnotationreturns the annotation itself – not the class of the annotation. I suspect you can just use: