-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
48 lines (40 loc) · 1.3 KB
/
Copy pathTest.java
File metadata and controls
48 lines (40 loc) · 1.3 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
package Annotation_14;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyTag {
String name() default "mhb";
int age();
}
public class Test {
@MyTag(age = 12, name = "sss")
public void info() {
}
public static void main(String[] args) {
try {
Annotation[] aArray = Class.forName("Annotation_14.Test").getMethod("info").getAnnotations();
for (Annotation an : aArray
) {
System.out.println("annotation is " + an);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
Test tt = new Test();
try {
Annotation[] anArray = tt.getClass().getMethod("info").getAnnotations();
for (Annotation an : anArray
) {
System.out.println("annotation is " + an);
if (an instanceof MyTag) {
System.out.println("Tag is :" + an);
MyTag anTag = (MyTag) an;
System.out.println(anTag.name() + " "+ anTag.age());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}