什么是反射?反射有什么用处?
“反射(Reflection)能够让运行于JVM中的程序检测和修改运行时的行为。”这个概念常常会和内省(Introspection)混淆,以下是这两个术语在Wikipedia中的解释:
从它们的定义可以看出,内省是反射的一个子集。有些语言支持内省,但并不支持反射,如C++。

内省示例:instanceof 运算符用于检测某个对象是否属于特定的类。
|
1
2
3
4
|
if (obj instanceof Dog) { Dog d = (Dog) obj; d.bark();} |
反射示例:Class.forName()方法可以通过类或接口的名称(一个字符串或完全限定名)来获取对应的Class对象。forName方法会触发类的初始化。
|
1
2
3
4
5
|
// 使用反射Class<?> c = Class.forName("classpath.and.classname");Object dog = c.newInstance();Method m = c.getDeclaredMethod("bark", new Class<?>[0]);m.invoke(dog); |
在Java中,反射更接近于内省,因为你无法改变一个对象的结构。虽然一些API可以用来修改方法和属性的可见性,但并不能修改结构。
反射能够让我们:
反射是框架中常用的方法。
例如,JUnit通过反射来遍历包含 @Test 注解的方法,并在运行单元测试时调用它们。(这个连接中包含了一些JUnit的使用案例)
对于Web框架,开发人员在配置文件中定义他们对各种接口和类的实现。通过反射机制,框架能够快速地动态初始化所需要的类。
例如,Spring框架使用如下的配置文件:
|
1
2
3
|
<bean id="someID" class="com.programcreek.Foo"> <property name="someField" value="someValue" /></bean> |
当Spring容器处理<bean>元素时,会使用Class.forName("com.programcreek.Foo")来初始化这个类,并再次使用反射获取<property>元素对应的setter方法,为对象的属性赋值。
Servlet也会使用相同的机制:
|
1
2
3
4
|
<servlet> <servlet-name>someServlet</servlet-name> <servlet-class>com.programcreek.WhyReflectionServlet</servlet-class><servlet> |
让我们通过几个典型的案例来学习如何使用反射。
示例1:获取对象的类型名称。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package myreflection;import java.lang.reflect.Method;public class ReflectionHelloWorld { public static void main(String[] args){ Foo f = new Foo(); System.out.println(f.getClass().getName()); }}class Foo { public void print() { System.out.println("abc"); }} |
输出:
|
1
|
myreflection.Foo |
示例2:调用未知对象的方法。
在下列代码中,设想对象的类型是未知的。通过反射,我们可以判断它是否包含print方法,并调用它。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package myreflection;import java.lang.reflect.Method;public class ReflectionHelloWorld { public static void main(String[] args){ Foo f = new Foo(); Method method; try { method = f.getClass().getMethod("print", new Class<?>[0]); method.invoke(f); } catch (Exception e) { e.printStackTrace(); } }}class Foo { public void print() { System.out.println("abc"); }} |
输出:
|
1
|
abc |
示例3:创建对象
|
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
|
package myreflection;public class ReflectionHelloWorld { public static void main(String[] args){ // 创建Class实例 Class<?> c = null; try{ c=Class.forName("myreflection.Foo"); }catch(Exception e){ e.printStackTrace(); } // 创建Foo实例 Foo f = null; try { f = (Foo) c.newInstance(); } catch (Exception e) { e.printStackTrace(); } f.print(); }}class Foo { public void print() { System.out.println("abc"); }} |
示例4:获取构造函数,并创建对象。
|
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
|
package myreflection;import java.lang.reflect.Constructor;public class ReflectionHelloWorld { public static void main(String[] args){ // 创建Class实例 Class<?> c = null; try{ c=Class.forName("myreflection.Foo"); }catch(Exception e){ e.printStackTrace(); } // 创建Foo实例 Foo f1 = null; Foo f2 = null; // 获取所有的构造函数 Constructor<?> cons[] = c.getConstructors(); try { f1 = (Foo) cons[0].newInstance(); f2 = (Foo) cons[1].newInstance("abc"); } catch (Exception e) { e.printStackTrace(); } f1.print(); f2.print(); }}class Foo { String s; public Foo(){} public Foo(String s){ this.s=s; } public void print() { System.out.println(s); }} |
输出:
|
1
2
|
nullabc |
此外,你可以通过Class实例来获取该类实现的接口、父类、声明的属性等。
示例5:通过反射来修改数组的大小。
|
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
|
package myreflection;import java.lang.reflect.Array;public class ReflectionHelloWorld { public static void main(String[] args) { int[] intArray = { 1, 2, 3, 4, 5 }; int[] newIntArray = (int[]) changeArraySize(intArray, 10); print(newIntArray); String[] atr = { "a", "b", "c", "d", "e" }; String[] str1 = (String[]) changeArraySize(atr, 10); print(str1); } // 修改数组的大小 public static Object changeArraySize(Object obj, int len) { Class<?> arr = obj.getClass().getComponentType(); Object newArray = Array.newInstance(arr, len); // 复制数组 int co = Array.getLength(obj); System.arraycopy(obj, 0, newArray, 0, co); return newArray; } // 打印 public static void print(Object obj) { Class<?> c = obj.getClass(); if (!c.isArray()) { return; } System.out.println("\nArray length: " + Array.getLength(obj)); for (int i = 0; i < Array.getLength(obj); i++) { System.out.print(Array.get(obj, i) + " "); } }} |
输出:
|
1
2
3
4
|
Array length: 101 2 3 4 5 0 0 0 0 0 Array length: 10a b c d e null null null null null |
上述示例代码仅仅展现了Java反射机制很小一部分的功能。如果你觉得意犹未尽,可以前去阅读官方文档。
参考资料:
原文链接: Programcreek 翻译: ImportNew.com - 薄荷脑
译文链接: http://www.importnew.com/9078.html
原文:http://www.cnblogs.com/firstdream/p/4817971.html