日期:2014-05-17  浏览次数:20689 次

求指教,Java反射问题
如题:
##自定义Annotation##
public @interface MyAnnotation {
 String role();
}

##Users类##
public class User {
 @MyAnnotation(role="aaa")
 public void say()
 {
 System.out.println("HI!");
 }
}

##Main方法##
Class a=Class.forName("Users");
Method [] method=a.getMethods();
for (int i = 0; i < method.length; i++) {
 System.out.println(method[i].getName());
 System.out.println(method[i].getAnnotations().length);
}


结果Say方法为的Annotations为0;
为什么?

------解决方案--------------------
想要在运气时去到注解那么需要设置@Retention(RetentionPolicy.RUNTIME)