java - no suitable method found for getAnnotation(Class<CAP#1>) -
i'm coding few simple methods annotations class, field, or method. first method, gets class-level annotation, works fine. copy , paste create new method field-level annotation results in compile error saying:
error: no suitable method found getannotation(class<cap#1>)
not sure how getannotation method call on class object works ok same method call on field object results in compilation error.
thoughts?
public class annotationtool { public static <t> t getannotation(class c, class<? extends t> annotation) { return (t)c.getannotation(annotation); } public static <t> t getannotation(class c, string fieldname, class<? extends t> annotation) { try { field f = c.getdeclaredfield(fieldname); return (t)f.getannotation(annotation); // compile error here?? } catch (nosuchfieldexception nsfe) { throw new runtimeexception(nsfe); } } }
field.getannotation
expects class subclass of annotation:
public <t extends annotation> t getannotation(class<t> annotationclass) {
therefore in helper method need restrict type parameter accordingly:
public static <t extends annotation> t getannotation(class<?> c, string fieldname, class<t> annotation) {
note same holds class level helper method. used raw type class c
prevented compiler issue same error.
Comments
Post a Comment