I m trying to get the annotation details from super type reference variable using reflection, to make the method accept all sub types. But isAnnotationPresent()
returning false
. Same with other annotation related methods. If used on the exact type, output is as expected.
I know that annotation info will be available on the Object even I m referring through super type.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Table {
String name();
}
@Table(name = "some_table")
public class SomeEntity {
public static void main(String[] args) {
System.out.println(SomeEntity.class.isAnnotationPresent(Table.class)); // true
System.out.println(new SomeEntity().getClass().isAnnotationPresent(Table.class)); // true
Class<?> o1 = SomeEntity.class;
System.out.println(o1.getClass().isAnnotationPresent(Table.class)); // false
Class<SomeEntity> o2 = SomeEntity.class;
System.out.println(o2.getClass().isAnnotationPresent(Table.class)); // false
Object o3 = SomeEntity.class;
System.out.println(o3.getClass().isAnnotationPresent(Table.class)); // false
}
}
How to get the annotation info?
getClass()
after changing the method parameter fromObject
toClass
. I knew my question is so dumb as I've done similar thing thousand times. Thanks for spotting my ignorance. – Gery