62,623
社区成员
发帖
与我相关
我的任务
分享
package annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
@MyAnnotation(Value="aa", Mehtod = "bb")
public void get() {
System.out.println("Method invoked");
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Test t = new Test();
Method method = t.getClass().getMethod("get");
System.out.println(method.isAnnotationPresent(MyAnnotation.class));
method.invoke(t);
MyAnnotation a = method.getAnnotation(MyAnnotation.class);
System.out.println(a);
}
}
public enum RetentionPolicy {
SOURCE, // Annotation is discarded by the compiler
CLASS, // Annotation is stored in the class file, but ignored by the VM
RUNTIME // Annotation is stored in the class file and read by the VM
}
package annotation;
public @interface MyAnnotation {
String Mehtod();
String Value();
}