/
Reflect.java
312 lines (257 loc) · 10.2 KB
/
Reflect.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cn.lee.demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* @author http://blog.csdn.net/ljphhj/article/details/12858767
*
*/
public class Reflect {
/**
* 为了看清楚Java反射部分代码,所有异常我都最后抛出来给虚拟机处理!
* @param args
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException, NoSuchMethodException {
// TODO Auto-generated method stub
//Demo1. 通过Java反射机制得到类的包名和类名
Demo1();
System.out.println("===============================================");
//Demo2. 验证所有的类都是Class类的实例对象
Demo2();
System.out.println("===============================================");
//Demo3. 通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在],无参构造
Demo3();
System.out.println("===============================================");
//Demo4: 通过Java反射机制得到一个类的构造函数,并实现构造带参实例对象
Demo4();
System.out.println("===============================================");
//Demo5: 通过Java反射机制操作成员变量, set 和 get
Demo5();
System.out.println("===============================================");
//Demo6: 通过Java反射机制得到类的一些属性: 继承的接口,父类,函数信息,成员信息,类型等
Demo6();
System.out.println("===============================================");
//Demo7: 通过Java反射机制调用类中方法
Demo7();
System.out.println("===============================================");
//Demo8: 通过Java反射机制获得类加载器
Demo8();
System.out.println("===============================================");
}
/**
* Demo1: 通过Java反射机制得到类的包名和类名
*/
public static void Demo1()
{
Person person = new Person();
System.out.println("Demo1: 包名: " + person.getClass().getPackage().getName() + ","
+ "完整类名: " + person.getClass().getName());
}
/**
* Demo2: 验证所有的类都是Class类的实例对象
* @throws ClassNotFoundException
*/
public static void Demo2() throws ClassNotFoundException
{
//定义两个类型都未知的Class , 设置初值为null, 看看如何给它们赋值成Person类
Class<?> class1 = null;
Class<?> class2 = null;
//写法1, 可能抛出 ClassNotFoundException [多用这个写法]
class1 = Class.forName("cn.lee.demo.Person");
System.out.println("Demo2:(写法1) 包名: " + class1.getPackage().getName() + ","
+ "完整类名: " + class1.getName());
//写法2
class2 = Person.class;
System.out.println("Demo2:(写法2) 包名: " + class2.getPackage().getName() + ","
+ "完整类名: " + class2.getName());
}
/**
* Demo3: 通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在]
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void Demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.Person");
//由于这里不能带参数,所以你要实例化的这个类Person,一定要有无参构造函数哈~
Person person = (Person) class1.newInstance();
person.setAge(20);
person.setName("LeeFeng");
System.out.println("Demo3: " + person.getName() + " : " + person.getAge());
}
/**
* Demo4: 通过Java反射机制得到一个类的构造函数,并实现创建带参实例对象
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws IllegalArgumentException
*/
public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
{
Class<?> class1 = null;
Person person1 = null;
Person person2 = null;
class1 = Class.forName("cn.lee.demo.Person");
//得到一系列构造函数集合
Constructor<?>[] constructors = class1.getConstructors();
person1 = (Person) constructors[0].newInstance();
person1.setAge(30);
person1.setName("leeFeng");
person2 = (Person) constructors[1].newInstance(20,"leeFeng");
System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge()
+ " , " + person2.getName() + " : " + person2.getAge()
);
}
/**
* Demo5: 通过Java反射机制操作成员变量, set 和 get
*
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public static void Demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException, ClassNotFoundException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.Person");
Object obj = class1.newInstance();
Field personNameField = class1.getDeclaredField("name");
personNameField.setAccessible(true);
personNameField.set(obj, "胖虎先森");
System.out.println("Demo5: 修改属性之后得到属性变量的值:" + personNameField.get(obj));
}
/**
* Demo6: 通过Java反射机制得到类的一些属性: 继承的接口,父类,函数信息,成员信息,类型等
* @throws ClassNotFoundException
*/
public static void Demo6() throws ClassNotFoundException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.SuperMan");
//取得父类名称
Class<?> superClass = class1.getSuperclass();
System.out.println("Demo6: SuperMan类的父类名: " + superClass.getName());
System.out.println("===============================================");
Field[] fields = class1.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println("类中的成员: " + fields[i]);
}
System.out.println("===============================================");
//取得类方法
Method[] methods = class1.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Demo6,取得SuperMan类的方法:");
System.out.println("函数名:" + methods[i].getName());
System.out.println("函数返回类型:" + methods[i].getReturnType());
System.out.println("函数访问修饰符:" + Modifier.toString(methods[i].getModifiers()));
System.out.println("函数代码写法: " + methods[i]);
}
System.out.println("===============================================");
//取得类实现的接口,因为接口类也属于Class,所以得到接口中的方法也是一样的方法得到哈
Class<?> interfaces[] = class1.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
System.out.println("实现的接口类名: " + interfaces[i].getName() );
}
}
/**
* Demo7: 通过Java反射机制调用类方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
public static void Demo7() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.SuperMan");
System.out.println("Demo7: \n调用无参方法fly():");
Method method = class1.getMethod("fly");
method.invoke(class1.newInstance());
System.out.println("调用有参方法walk(int m):");
method = class1.getMethod("walk",int.class);
method.invoke(class1.newInstance(),100);
}
/**
* Demo8: 通过Java反射机制得到类加载器信息
*
* 在java中有三种类类加载器。[这段资料网上截取]
1)Bootstrap ClassLoader 此加载器采用c++编写,一般开发中很少见。
2)Extension ClassLoader 用来进行扩展类的加载,一般对应的是jre\lib\ext目录中的类
3)AppClassLoader 加载classpath指定的类,是最常用的加载器。同时也是java中默认的加载器。
*
* @throws ClassNotFoundException
*/
public static void Demo8() throws ClassNotFoundException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.SuperMan");
String nameString = class1.getClassLoader().getClass().getName();
System.out.println("Demo8: 类加载器类名: " + nameString);
}
}
/**
*
* @author xiaoyaomeng
*
*/
class Person{
private int age;
private String name;
public Person(){
}
public Person(int age, String name){
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class SuperMan extends Person implements ActionInterface
{
private boolean BlueBriefs;
public void fly()
{
System.out.println("超人会飞耶~~");
}
public boolean isBlueBriefs() {
return BlueBriefs;
}
public void setBlueBriefs(boolean blueBriefs) {
BlueBriefs = blueBriefs;
}
@Override
public void walk(int m) {
System.out.println("超人会走耶~~走了" + m + "米就走不动了!");
}
}
interface ActionInterface{
public void walk(int m);
}