-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMeta4.java
More file actions
38 lines (29 loc) · 914 Bytes
/
Meta4.java
File metadata and controls
38 lines (29 loc) · 914 Bytes
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
package chapter12;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
//An annotation type declaration that includes defaults
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno4 {
String str() default "Testing";
int val() default 9000;
}
public class Meta4 {
//Annotate the method using the default values
@MyAnno4()
public static void myMeth() {
Meta4 ob = new Meta4();
//Obtain the annotation for this method.
try {
Class<?> c = ob.getClass();
Method m = c.getMethod("myMeth");
MyAnno4 anno = m.getAnnotation(MyAnno4.class);
System.out.println(anno.str() + " - " + anno.val());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
myMeth();
}
}