forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo1.java
More file actions
28 lines (25 loc) · 796 Bytes
/
Copy pathDemo1.java
File metadata and controls
28 lines (25 loc) · 796 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
package reflect;
import java.lang.reflect.Method;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("input Class name:");
String className = in.nextLine();
//动态加载类
Class cls = Class.forName(className);
//动态获取全部方法
Method[] ary = cls.getDeclaredMethods();
//动态检查方法的注解信息
for (Method method : ary) {
//检查一个方法的注解信息
//method.getAnnotation(被检查的注解类型);
Demo ann = method.getAnnotation(Demo.class);
//返回注册类型,如果为空表示没有注解
//不为空表示找到了被检查的注解Annotation
if (ann != null) {
System.out.println(method);
}
}
}
}