-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMeta2.java
More file actions
36 lines (28 loc) · 890 Bytes
/
Meta2.java
File metadata and controls
36 lines (28 loc) · 890 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
package chapter12;
import java.lang.annotation.*;
import java.lang.annotation.*;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno2 {
String str();
int val();
}
public class Meta2 {
//myMeth has two arguments
@MyAnno2(str = "Two arguments", val = 19)
public static void myMeth(String str, int i) {
Meta2 ob = new Meta2();
try {
Class<?> c = ob.getClass();
//Here the parameter types are specified
Method m = c.getMethod("myMeth", String.class, int.class);
MyAnno2 anno2 = m.getAnnotation(MyAnno2.class);
System.out.println(anno2.str() + " " + anno2.val());
} catch (NoSuchMethodException e) {
System.out.println("Method not found");
}
}
public static void main(String[] args) {
myMeth("test",10);
}
}