-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRepeatAnno.java
More file actions
47 lines (37 loc) · 1.16 KB
/
RepeatAnno.java
File metadata and controls
47 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package chapter12;
//Demonstrate a repeated annotation
import java.lang.annotation.*;
import java.lang.reflect.*;
//Make MyAnno repeatable
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyRepeatedAnnos.class)
@interface MyAnno5{
String str() default "Testing";
int val() default 9000;
}
//This is the container annotation
@Retention(RetentionPolicy.RUNTIME)
@interface MyRepeatedAnnos{
MyAnno5[] value();
}
public class RepeatAnno {
//Repeat MyAnno5 on myMeth()
@MyAnno5(str = "First annotation",val = -1)
@MyAnno5(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 e){
System.out.println("Method not found.");
}
}
public static void main(String[] args) {
myMeth("test",10);
}
}