// Demonstrate a repeated annotation.
import java.lang.annotation.*;
import java.lang.reflect.*;
// Make MyAnno repeatable.
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyRepeatedAnnos.class)
@interface MyAnno {
String str() default "Testing";
int val() default 9000;
}
// This is the container annotation.
@Retention(RetentionPolicy.RUNTIME)
@interface MyRepeatedAnnos {
MyAnno[] value();
}
class RepeatAnno {
// Repeat MyAnno on myMeth().
@MyAnno(str = "First annotation", val = -1)
@MyAnno(str = "Second annotation", val = 100)
public static void myMeth(String str, int i)
{
RepeatAnno ob = new RepeatAnno();
try {
Class<?> c = ob.getClass();
// Obtain the annotations for myMeth().
Method m = c.getMethod("myMeth", String.class, int.class);
// Display the repeated MyAnno annotations.
Annotation anno = m.getAnnotation(MyRepeatedAnnos.class);
System.out.println(anno);
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth("test", 10);
}
}
我已从一本书中复制粘贴此代码,并尝试使用jdk版本1.8.0_131在我的系统中运行此代码,但我收到以下编译错误:
error: cannot find symbol
@Repeatable(MyRepeatedAnnos.class)
^
symbol: class Repeatable
RepeatAnno.java:19: error: duplicate annotation
@MyAnno(str = "Second annotation", val = 100)
^
2 errors
我无法理解这里有什么问题。 请帮忙。