import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
  String value() default "mipaifu";
  Color  color() default Color.YELLOW;
}enum Color
{
BLACK,RED,WHITE,YELLOW;
}上面是自定义的一个注解。import java.lang.reflect.Method;@MyAnnotation(value = "Start", color = Color.RED)
public class MyTest
{
@MyAnnotation()
public void output()
{
System.out.println("output something you like");
}
public static void main(String[] args) throws Exception
{
MyTest mytest = new MyTest();
Class<?> c = MyTest.class;
Method method = c.getMethod("output", new Class[] {}); if (method.isAnnotationPresent(MyAnnotation.class))
{
method.invoke(mytest, new Object[] {});
MyAnnotation myAnnotation = method
.getAnnotation(MyAnnotation.class);
String str = myAnnotation.value();
Color color = myAnnotation.color();

System.out.println(str  + color);
}
}
}
运行报错:
output something you like
Exception in thread "main" java.lang.IllegalAccessError: tried to access class chengengsheng.Annotation.Color from class $Proxy1
at $Proxy1.color(Unknown Source)
at chengengsheng.Annotation.MyTest.main(MyTest.java:25)我想了很久还是不知道为什么会错,单独String str = myAnnotation.value();就可以。为什么加上后面的Color color = myAnnotation.color();就错了呢?