代码
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(value=RetentionPolicy.RUNTIME)
@interface MyAnnotation{
public String vaule();
}
class A{
@MyAnnotation(vaule = "xjc")
public void fun() {
}
}
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
// TODO Auto-generated method stub
System.out.println("Annotation");
Class<?> c = Class.forName("A");
Method mt = c.getMethod("fun");
if(mt.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation ma = mt.getAnnotation(MyAnnotation.class);
String str = ma.vaule();
System.out.println(str);
}
}
}
运行效果