-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMeta3.java
More file actions
54 lines (45 loc) · 1.4 KB
/
Meta3.java
File metadata and controls
54 lines (45 loc) · 1.4 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
48
49
50
51
52
53
54
package chapter12;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
//Show all annotations for class and method.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno3 {
String str();
int val();
}
@Retention(RetentionPolicy.RUNTIME)
@interface What{
String description();
}
@What(description = "An annotation test class")
@MyAnno3(str = "Meta3",val = 99)
public class Meta3 {
@What(description = "An annotation test method")
@MyAnno3(str = "Testing",val = 100)
public static void myMeth(){
Meta3 ob = new Meta3();
try {
Annotation annos[]=ob.getClass().getAnnotations();
//Display all annotations for Meta3
System.out.println("All annotations for Meta3");
for(Annotation a:annos){
System.out.println(a);
}
System.out.println();
//Display all annotations for myMeth
Method m=ob.getClass().getMethod("myMeth");
annos=m.getAnnotations();
System.out.println("All annotations for myMeth");
for (Annotation a:annos){
System.out.println(a);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
myMeth();
}
}