25
Aquí está una clase de prueba:de Java: no se puede acceder a través de la reflexión anotaciones
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
lo que me falta para hacer anotaciones visible a través de la reflexión?
¿Necesito un procesador de anotación incluso para comprobar su presencia?
¡Muchas gracias! – glmxndr
¿Puedo preguntar, sin embargo, si usted sabe si hay una manera de cambiar el comportamiento predeterminado de la política de retención de CLASS a RUNTIME? – glmxndr
Hasta donde sé, eso no es posible, pero puedo estar equivocado. – abyx