-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter.java.reflect.xml
More file actions
347 lines (281 loc) · 8.15 KB
/
chapter.java.reflect.xml
File metadata and controls
347 lines (281 loc) · 8.15 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="reflect" ?>
<title>Reflection 反射</title>
<screen>
this.getClass().getName() //当前Class名字
Thread.currentThread().getStackTrace()[1].getMethodName()); //当前方法名
</screen>
<section>
<title>获得所有变量</title>
<programlisting>
<![CDATA[
Field[] fields = objClass.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
]]>
</programlisting>
<para>注意:只能去除 public变量</para>
</section>
<section>
<title>批量赋值</title>
</section>
<section>
<title>方法操作</title>
<para>JAVA反射调用方法的步骤有三步</para>
<screen>
得到要调用类的class
得到要调用的类中的方法(Method)
方法调用(invoke)
</screen>
<section>
<title>获得所有方法</title>
<programlisting>
<![CDATA[
Class<?> objClass = a.getClass();
Method[] methods = objClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
]]>
</programlisting>
</section>
<section>
<title>set/get 方法</title>
<programlisting>
<![CDATA[
package cn.netkiller.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Member {
public String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "ClassA [name=" + name + ", age=" + age + "]";
}
public Member() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class<?> cls = Class.forName("cn.netkiller.reflect.Member");
Object member = cls.newInstance();
Method setMethod = cls.getDeclaredMethod("setAge", int.class);
setMethod.invoke(member, 15);
Method getMethod = cls.getDeclaredMethod("getAge");
System.out.println(getMethod.invoke(member));
}
}
]]>
</programlisting>
<para>下面做一个稍微复杂点的例子,ClassB继承ClassA,取出ClassA的成员变量赋值到ClassA。</para>
<screen>
<![CDATA[
package cn.netkiller.reflect;
public class ClassA {
public String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ClassA() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "ClassA [name=" + name + ", age=" + age + "]";
}
}
package cn.netkiller.reflect;
public class ClassB extends ClassA{
public ClassB() {
// TODO Auto-generated constructor stub
}
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "ClassB [address=" + address + "]";
}
}
package cn.netkiller.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionTest {
public ReflectionTest() {
// TODO Auto-generated constructor stub
}
public void testSetMethod() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
// ClassA a = new ClassA();
ClassB b = new ClassB();
b.setAddress("Shenzhen");
Class<ClassA> classA = ClassA.class;
ClassA a = classA.newInstance();
a.setName("Neo");
a.setAge(30);
System.out.println(classA.getDeclaredMethod("getAge").invoke(a));
Method m = classA.getDeclaredMethod("setAge", int.class);
m.setAccessible(true); // 因为写成private 所以这里必须设置
m.invoke(b, 26);
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(b.getName());
System.out.println(b.getAge());
}
public static void main(String[] args) throws InvocationTargetException {
ReflectionTest rt = new ReflectionTest();
try {
rt.testSetMethod();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
]]>
</screen>
<para>set 方法</para>
<programlisting>
<![CDATA[
System.out.println(classA.getDeclaredMethod("getAge").invoke(a));
]]>
</programlisting>
<para>get 方法</para>
<programlisting>
<![CDATA[
Method m = classA.getDeclaredMethod("setAge", int.class);
m.setAccessible(true); //因为写成private 所以这里必须设置
m.invoke(b, 26);
]]>
</programlisting>
</section>
<section>
<title>static 方法调用</title>
<para></para>
<programlisting>
<![CDATA[
Class cls = Class.forName("cn.netkiller.reflect.Student");
Method setMethod = cls.getDeclaredMethod("setAge",int.class);
setMethod.invoke(cls.newInstance(), 15);
]]>
</programlisting>
<para>static 方法调用时,不必得实例化对象</para>
<programlisting>
<![CDATA[
Class cls = Class.forName("cn.netkiller.reflect.Student");
Method staticMethod = cls.getDeclaredMethod("setAge",int.class);
staticMethod.invoke(cls,20); //这里不需要newInstance
]]>
</programlisting>
</section>
</section>
<section>
<title>完成的例子</title>
<programlisting>
<![CDATA[
package cn.netkiller;
import java.lang.reflect.Field;
public class Test {
static class MyClass {
public String var1 = "Hello";
public int var2 = 123;
public double var3;
public String var4;
}
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
MyClass obj = new MyClass();
// obj.var4 = "World";
Class<? extends MyClass> clazz = obj.getClass();
Field field1 = clazz.getDeclaredField("var4");
field1.setAccessible(true); // 设置可访问性
field1.set(obj, "Neo"); // 设置值
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true); // 设置为可访问
if (field.get(obj) != null) {
System.out.println(field.getName() + " = " + field.get(obj));
}
}
}
}
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller;
import lombok.Data;
import java.lang.reflect.Field;
public class Test {
@Data
static class MyClass {
public String var1;
public Integer var2;
public Double var3;
public String var4;
}
public static Object setValue(Object obj, Object target) throws IllegalAccessException, NoSuchFieldException {
Field[] fields = obj.getClass().getDeclaredFields();
Class<?> clazz = target.getClass();
for (Field field : fields) {
field.setAccessible(true); // 设置为可访问
System.out.println(field.getName() + " = " + field.get(obj));
if (field.get(obj) != null) {
Field field1 = clazz.getDeclaredField(field.getName());
field1.setAccessible(true); // 设置可访问性
field1.set(target, field.get(obj)); // 设置值
}
}
System.out.println(target);
return target;
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
MyClass obj = new MyClass();
obj.var1 = "Hello";
obj.var4 = "World";
MyClass target = new MyClass();
setValue(obj, target);
}
}
]]>
</programlisting>
</section>
</chapter>