forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysisApp.java
More file actions
38 lines (33 loc) · 1.37 KB
/
AnalysisApp.java
File metadata and controls
38 lines (33 loc) · 1.37 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
package onlyfun.caterpillar;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class AnalysisApp {
public static void main(String[] args)
throws NoSuchMethodException {
Class<SomeClass3> c = SomeClass3.class;
// 因為SomeAnnotation標示於doSomething()方法上
// 所以要取得doSomething()方法的Method實例
Method method = c.getMethod("doSomething");
// 如果SomeAnnotation存在的話
if(method.isAnnotationPresent(SomeAnnotation.class)) {
System.out.println("找到 @SomeAnnotation");
// 取得SomeAnnotation
SomeAnnotation annotation =
method.getAnnotation(SomeAnnotation.class);
// 取得value成員值
System.out.println("\tvalue = " + annotation.value());
// 取得name成員值
System.out.println("\tname = " + annotation.name());
}
else {
System.out.println("找不到 @SomeAnnotation");
}
// 取得doSomething()方法上所有的Annotation
Annotation[] annotations = method.getAnnotations();
// 顯示Annotation名稱
for(Annotation annotation : annotations) {
System.out.println("Annotation名稱:" +
annotation.annotationType().getName());
}
}
}