-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMeta.java
More file actions
44 lines (34 loc) · 1.13 KB
/
Meta.java
File metadata and controls
44 lines (34 loc) · 1.13 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
package chapter12;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
//An annotation type declaration
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str();
int val();
}
public class Meta {
//Annotate a method
@MyAnno(str = "Annotation Example", val = 100)
public static void myMeth() {
Meta ob = new Meta();
//Obtain annotation for this method
//and display the values of the members
try {
//First, get a Class Object that represents this class.
Class<?> c = ob.getClass();
//Now, get a method object that represents this method.
Method m = c.getMethod("myMeth");
//Next get the annotation for this class
MyAnno anno = m.getAnnotation(MyAnno.class);
//Finally display the values
System.out.println(anno.str() + " " + anno.val());
} catch (NoSuchMethodException exc) {
System.out.println("Method not found");
}
}
public static void main(String[] args) {
myMeth();
}
}