// 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);
}
}
I have copy-pasted this code from a book and I tried to run this code in my system with JDK version 1.8.0_131, but I'm getting the following compilation error:
error: cannot find symbol @Repeatable(MyRepeatedAnnos.class) ^ symbol: class Repeatable RepeatAnno.java:19: error: duplicate annotation @MyAnno(str = "Second annotation", val = 100) ^ 2 errors
I can't understand what is wrong here. Please help.