Annotation中定义的enum类型的如何调用,我写了一个代码,
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MyAnnotation {
int i();
String str();
em e();
String[] value();
}
enum em {
Hello,
world
}
然后我在测试代码中调用枚举类型的e,代码是这样的:
public class Main {
public static void main(String[] args) {
Class<TestMyAnnotation> c = TestMyAnnotation.class;
Method test = null ;
try {
test = c.getDeclaredMethod("testAnnoation",String.class);
} catch (Exception e) {
e.printStackTrace();
}
MyAnnotation a = test.getAnnotation(MyAnnotation.class);
System.out.println(a.i());
System.out.println(a.str());
System.out.println(a.e()); //此处出错,抛出IllegalAccessError
em e = em.Hello; //em是我定义的一个enum
System.out.println(e); //这样是可以的,why?
}
}