Java: no se puede acceder a las anotaciones a través de la reflexión
Aquí hay una clase de prueba:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestAnnotations {
@interface Annotate{}
@Annotate public void myMethod(){}
public static void main(String[] args) {
try{
Method[] methods = TestAnnotations.class.getDeclaredMethods();
Method m = methods[1];
assert m.getName().equals("myMethod");
System.out.println("method inspected ? " + m.getName());
Annotation a = m.getAnnotation(Annotate.class);
System.out.println("annotation ? " + a);
System.out.println("annotations length ? "
+ m.getDeclaredAnnotations().length);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Aquí está mi salida:
method inspected ? myMethod
annotation : null
annotations length : 0
¿Qué me falta para hacer visibles las anotaciones a través de la reflexión?
¿Necesito un procesador de anotaciones incluso para verificar su presencia?