@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String key();
public String value();
}
@MyAnnotation(key = "k", value = "v")
public class UserAnnotation {
@MyAnnotation(key = "km", value = "vm")
public void sayHello() {
System.out.println("111");
}
public static void main(String[] args) throws Exception {
Class<?> cla = Class
.forName("com.kaishengit.annotation.UserAnnotation");
Method[] methods = cla.getMethods();
boolean flag = cla.isAnnotationPresent(MyAnnotation.class);
System.out.println(flag);
if (flag) {
MyAnnotation mya = (MyAnnotation) cla
.getAnnotation(MyAnnotation.class);
System.out.println(mya.key() + "====" + mya.value());
}
Set<Method> set = new HashSet<Method>();
for (int i = 0; i < methods.length; i++) {
boolean otherflag = methods[i]
.isAnnotationPresent(MyAnnotation.class);
if (otherflag) {
set.add(methods[i]);
System.out.println(methods[i].getName());
}
}
for (Method method : set) {
MyAnnotation name = method.getAnnotation(MyAnnotation.class);
System.out.println(name.key());
System.out.println("value===:" + name.value());
}
}
}