利用java反射技术提取类信息
1
package
day15;
2
3 import java.lang.reflect. * ;
4
5 public class ReflectionTest {
6
7 public static void main(String[] args) {
8
9 Class c = null ;
10
11 try {
12
13 c = Class.forName(args[ 0 ]);
14
15 System.out.println( " package " + c.getPackage().getName() + " ; " );
16
17 System.out.print(Modifier.toString(c.getModifiers()) + " " );
18
19 System.out.print( " class " + c.getSimpleName());
20
21 if (c.getSuperclass() != Object. class ){
22
23 System.out.print( " extends " + c.getSuperclass().getSimpleName());
24
25 }
26
27 Class[] inters = c.getInterfaces();
28
29 if (inters.length > 0 ){
30
31 System.out.print( " implements " );
32
33 for ( int i = 0 ;i < inters.length;i ++ ){
34
35 System.out.print(inters[i].getSimpleName());
36
37 if (i < inters.length - 1 ){
38
39 System.out.print( " , " );
40
41 }
42
43 }
44
45 }
46
47
48
49 System.out.println( " { " );
50
51 // System.out.println("/t
");
52
53 printFields(c);
54
55 printMethods(c);
56
57 System.out.println( " } " );
58
59
60
61 } catch (ClassNotFoundException e) {
62
63 e.printStackTrace();
64
65 }
66
67 }
68
69
70
71 public static void printFields(Class c){
72
73 Field[] fs = c.getDeclaredFields();
74
75 for ( int i = 0 ;i < fs.length ;i ++ ){
76
77 System.out.print( " /t " + Modifier.toString(fs[i].getModifiers()));
78
79 System.out.print( " " + fs[i].getType().getSimpleName()); // 类名
80
81 System.out.print( " " + fs[i].getName() + " ; " );
82
83 }
84
85 }
86
87
88
89 public static void printMethods(Class c){
90
91
92
93 Method[] ms = c.getDeclaredMethods();
94
95 for ( int i = 0 ;i < ms.length ;i ++ ){
96
97 System.out.print( " /t " + Modifier.toString(ms[i].getModifiers()));
98
99 System.out.print( " " + ms[i].getReturnType().getSimpleName());
100
101 System.out.print( " " + ms[i].getName() + " " + " ( " );
102
103
104
105 Class[] parameters = ms[i].getParameterTypes();
106
107 for ( int j = 0 ;j < parameters.length;j ++ ){
108
109 System.out.print(parameters[j].getSimpleName() + " arg " + j);
110
111 if (j < parameters.length - 1 ){
112
113 System.out.print( " , " );
114
115 }
116
117 }
118
119 System.out.print( " ) " );
120
121 Class[] exceptions = ms[i].getExceptionTypes();
122
123 if (exceptions.length > 0 ){
124
125 System.out.print( " throws " );
126
127 for ( int k = 0 ;k < exceptions.length;k ++ ){
128
129 System.out.print(exceptions[k].getSimpleName());
130
131 if (k < exceptions.length - 1 ){
132
133 System.out.print( " , " );
134
135 }
136
137 }
138
139 }
140
141 System.out.println( " { " );
142
143 System.out.println( " /t/t
"
);
144
145 System.out.println( " /t} " );
146
147 }
148
149 }
150
151 }
152
153编译时给定要提取的类做参数。 java ReflectionTest.java java.lang.String 即可!
2
3 import java.lang.reflect. * ;
4
5 public class ReflectionTest {
6
7 public static void main(String[] args) {
8
9 Class c = null ;
10
11 try {
12
13 c = Class.forName(args[ 0 ]);
14
15 System.out.println( " package " + c.getPackage().getName() + " ; " );
16
17 System.out.print(Modifier.toString(c.getModifiers()) + " " );
18
19 System.out.print( " class " + c.getSimpleName());
20
21 if (c.getSuperclass() != Object. class ){
22
23 System.out.print( " extends " + c.getSuperclass().getSimpleName());
24
25 }
26
27 Class[] inters = c.getInterfaces();
28
29 if (inters.length > 0 ){
30
31 System.out.print( " implements " );
32
33 for ( int i = 0 ;i < inters.length;i ++ ){
34
35 System.out.print(inters[i].getSimpleName());
36
37 if (i < inters.length - 1 ){
38
39 System.out.print( " , " );
40
41 }
42
43 }
44
45 }
46
47
48
49 System.out.println( " { " );
50
51 // System.out.println("/t
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
52
53 printFields(c);
54
55 printMethods(c);
56
57 System.out.println( " } " );
58
59
60
61 } catch (ClassNotFoundException e) {
62
63 e.printStackTrace();
64
65 }
66
67 }
68
69
70
71 public static void printFields(Class c){
72
73 Field[] fs = c.getDeclaredFields();
74
75 for ( int i = 0 ;i < fs.length ;i ++ ){
76
77 System.out.print( " /t " + Modifier.toString(fs[i].getModifiers()));
78
79 System.out.print( " " + fs[i].getType().getSimpleName()); // 类名
80
81 System.out.print( " " + fs[i].getName() + " ; " );
82
83 }
84
85 }
86
87
88
89 public static void printMethods(Class c){
90
91
92
93 Method[] ms = c.getDeclaredMethods();
94
95 for ( int i = 0 ;i < ms.length ;i ++ ){
96
97 System.out.print( " /t " + Modifier.toString(ms[i].getModifiers()));
98
99 System.out.print( " " + ms[i].getReturnType().getSimpleName());
100
101 System.out.print( " " + ms[i].getName() + " " + " ( " );
102
103
104
105 Class[] parameters = ms[i].getParameterTypes();
106
107 for ( int j = 0 ;j < parameters.length;j ++ ){
108
109 System.out.print(parameters[j].getSimpleName() + " arg " + j);
110
111 if (j < parameters.length - 1 ){
112
113 System.out.print( " , " );
114
115 }
116
117 }
118
119 System.out.print( " ) " );
120
121 Class[] exceptions = ms[i].getExceptionTypes();
122
123 if (exceptions.length > 0 ){
124
125 System.out.print( " throws " );
126
127 for ( int k = 0 ;k < exceptions.length;k ++ ){
128
129 System.out.print(exceptions[k].getSimpleName());
130
131 if (k < exceptions.length - 1 ){
132
133 System.out.print( " , " );
134
135 }
136
137 }
138
139 }
140
141 System.out.println( " { " );
142
143 System.out.println( " /t/t
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
144
145 System.out.println( " /t} " );
146
147 }
148
149 }
150
151 }
152
153编译时给定要提取的类做参数。 java ReflectionTest.java java.lang.String 即可!