使用Java反射获取类的信息
1/**
2 * copied from core java volume 1 chapter 5 page 195<br>
3 * class to analysis a java class via reflect
4 *
5 * @author wangy
6 * @version 1.0
7 * @date 2020/4/28 / 16:04
8 */
9@SuppressWarnings("rawtypes")
10public class ReflectionTest {
11 public static void main(String[] args) {
12 String name;
13 Scanner scanner = new Scanner(System.in);
14 if (args.length > 0) {
15 name = args[0];
16 } else {
17 System.out.println("Enter Class name(e.g. java.util.ArrayList)");
18 name = scanner.next();
19 }
20 try {
21 // print class name and super class name(if != Object)
22 Class cls = Class.forName(name);
23 Class superCls = cls.getSuperclass();
24 String modifiers = Modifier.toString(cls.getModifiers());
25 if (modifiers.length() > 0) {
26 System.out.print(modifiers + " ");
27 }
28 System.out.print("class " + cls.getSimpleName());
29 if (superCls != null && superCls != Object.class) {
30 System.out.print(" extends " + superCls.getSimpleName());
31 }
32 System.out.print("{\n");
33 printConstructors(cls);
34 System.out.println();
35 printMethods(cls);
36 System.out.println();
37 printFields(cls);
38 System.out.println("}");
39 } catch (Exception e) {
40 e.printStackTrace();
41 }
42 scanner.close();
43 System.exit(0);
44 }
45
46 /**
47 * print all constructors of a class
48 *
49 * @param cls
50 */
51 private static void printConstructors(Class cls) {
52 Constructor[] constructors = cls.getDeclaredConstructors();
53 for (Constructor c : constructors) {
54 String cName = c.getDeclaringClass().getSimpleName();
55 System.out.print("\t");
56 String s = Modifier.toString(c.getModifiers());
57 if (s.length() > 0) {
58 System.out.print(s + " ");
59 }
60 System.out.print(cName + " (");
61 // parameters
62 Class<?>[] pTypes = c.getParameterTypes();
63 for (int i = 0; i < pTypes.length; i++) {
64 if (i > 0) {
65 System.out.print(", ");
66 }
67 System.out.print(pTypes[i].getSimpleName());
68 }
69 System.out.print(");\n");
70 }
71 }
72
73 private static void printMethods(Class cls) {
74 Method[] methods = cls.getDeclaredMethods();
75 for (Method m : methods) {
76 Class<?> r = m.getReturnType();
77 String mName = m.getName();
78 System.out.print("\t");
79 String s = Modifier.toString(m.getModifiers());
80 if (s.length() > 0) {
81 System.out.print(s + " ");
82 }
83 System.out.print(r.getSimpleName() + " " + mName + "(");
84 // print parameters
85 Class<?>[] p = m.getParameterTypes();
86 for (int i = 0; i < p.length; i++) {
87 if (i > 0) {
88 System.out.print(", ");
89 }
90 System.out.print(p[i].getSimpleName());
91 }
92 System.out.print(");\n");
93 }
94 }
95
96 private static void printFields(Class cls) {
97 Field[] fields = cls.getDeclaredFields();
98 for (Field f : fields) {
99 Class<?> fType = f.getType();
100 String fName = f.getName();
101 System.out.print("\t");
102 String s = Modifier.toString(f.getModifiers());
103 if (s.length() > 0) {
104 System.out.print(s + " ");
105 }
106 System.out.print(fType.getName() + " " + fName + ";\n");
107 }
108 }
109}