反射
作用:
-
-
在运行时查看对象
-
实现通用的数组操作代码
- 利用 Method 对象
一、Class
在程序运行期间,Java 运行时系统始终为所有对象维护一个运行时(runtime)的类型标识,这个信息跟踪着每个对象所属的类。虚拟机会利用运行时类型信息选择相应的方法执行,而这些信息就保存在 Class 类中,表示一个特定类的属性,可以对任意一个对象调用 Object 中定义的 getClass 方法,获得一个 Class 对象,再对其调用 getName 方法,返回类名:
Employee e = new Employee("Babara", ...); System.out.println(e.getClass().getName() + ": " + e.getName()); // Employee: Babara
也可以用静态方法 Class.forName ,根据类名获取 Class 对象:
String className = "java.util.Random";
Class cl = Class.forName(className);
一个 Class 对象实际上表示的是一个类型(未必是一个类),假设 T 是任意的类型,那么 T.class 就代表了对应的 Class 对象:
Class cl1 = Random.class; Class cl2 = int.class; Class cl3 = Double[].class; // ...
获取 Class 对象的三种方式
-
-
类名.class / 多用于传递参数
-
对象.getClass() / 多用于获取对象的字节码
虚拟机为每个类型管理一个 Class 对象,因此可以利用 == 运算符进行比较:
Employee e = new Employee(); if (e.getClass() == Employee.class) // always true
获取了一个 Class 的对象之后,就可以用 newInstance 动态地创建这个类的一个实例(如果没有无参构造器,会抛出异常):
String str = "java.util.Random"; Object m = Class.forName(str).newInstance(); // m = new Random();
Class 类实际上是一个泛型类。
API - java.lang.Class
-
-
getName
-
newInstance
- getField
-
getFields / getMethods / getConstructors
- getDeclareFields / getDeclareMethods / getDeclareConstructors
二、检查类的结构
java.lang.reflect 包中有三个类 Field、Method、Constructor 分别用于描述类的域、方法、构造器,还有 Modifier 用于反映方法或构造器的修饰符。
API - java.lang.reflect.Constructor
-
-
getExceptionTypes
-
getModifiers
- getName
-
getParameterTypes
- getReturnType
下面是书中一个测试 demo ,输入类名,能够输出这个类所有域名以及方法和构造器的签名:
import java.util.*; import java.lang.reflect.*; public class ReflectionTest { public static void main(String[] args) { // 读取类名 String name; if (args.length > 0) { name = args[0]; } else { Scanner in = new Scanner(System.in); System.out.println("输入类名(如:java.util.Data):"); name = in.next(); } // 打印类名和超类名 try { Class cl = Class.forName(name); Class supercl = cl.getSuperclass(); String modifiers = Modifier.toString(cl.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print("class " + name); if (supercl != null && supercl != Object.class) { System.out.print(" extends " + supercl.getName()); }
System.out.print(" {\n"); printFields(cl); System.out.println(); printConstructors(cl); System.out.println(); printMethods(cl); System.out.println("}"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.exit(0); } public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (Field f : fields) { Class type = f.getType(); String name = f.getName(); System.out.print("\t"); String modifiers = Modifier.toString(f.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.println(type.getName() + " " + name + ";"); } } public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (Constructor c : constructors) { String name = c.getName(); System.out.print("\t"); String modifiers = Modifier.toString(c.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print(name + " "); printParameters(c.getParameterTypes()); } } public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods(); for (Method m : methods) { Class retType = m.getReturnType(); String name = m.getName(); System.out.print("\t"); String modifiers = Modifier.toString(m.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print(retType.getName() + " " + name); printParameters(m.getParameterTypes()); } } public static void printParameters(Class[] paramTypes) { System.out.print("("); for (int i = 0; i < paramTypes.length; i++) { if (i > 0) { System.out.print(", "); } System.out.print(paramTypes[i].getName()); } System.out.println(");"); } }
运行情况:
三、在运行时分析对象
除了查看指定类中的域这项基本操作,反射机制还可以查看在编译时还不清楚的对象域。虽然反射机制的默认行为受限于 Java 的访问控制,但调用 Field 、Method 、Constructor 对象的 setAccessible 方法可以进行覆盖。假设 f 是一个 Field 类型的对象,obj 是某个包含 f 域的类的对象,先对调用 f.setAccessible(true) ,再调用 f.get(obj) ,将返回一个对象,其值为 obj 中对应域的当前值。
Employee e = new Employee("Harry Potter", 30000, 10, 1, 1997); Class cl = e.getClass(); Field f = cl.getDeclaredField("name"); f.setAccessible(true); Object v = f.get(e); System.out.println(v); // Harry Potter
API - java.lang.reflect.Field
-
get
-
set
API - java.lang.reflect.AccessibleObject
-
setAccessible
-
isAccessible
公认的 toString
作者提供了一个 toString 方法,通过反射得到对象在某一个时刻所有域的值,可供借鉴:
import java.lang.reflect.*; import java.util.ArrayList; public class ObjectAnalyzer { private ArrayList<Object> visited = new ArrayList<>(); public String toString(Object obj) { if (obj == null) return "null"; if (visited.contains(obj)) return "..."; visited.add(obj); Class cl = obj.getClass(); if (cl == String.class) return (String)obj; if (cl.isArray()) { String r = cl.getComponentType() + "[]{"; for (int i = 0; i < Array.getLength(obj); i++) { if (i > 0) r += ","; Object val = Array.get(obj, i); if (cl.getComponentType().isPrimitive()) r += val; else r += toString(val); } return r + "}"; } String r = cl.getName(); do { r += "["; Field[] fields = cl.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (Field f : fields) { if (!Modifier.isStatic(f.getModifiers())) { if (!r.endsWith("[")) r += ","; r += f.getName() + "="; try { Class t = f.getType(); Object val = f.get(obj); if (t.isPrimitive()) r += val; else r += toString(val); // recursion } catch (IllegalAccessException e) { e.printStackTrace(); } } } r += "]"; cl = cl.getSuperclass(); } while (cl != null); // bottom-to-top return r; } }
四、编写泛型数组代码
当一个数组的类型未知,而又需要对它进行拷贝的时候,可以这么做:首先,使用 Class 类的 getComponentType 方法获得数组元素的类型,再通过 Array 类的静态方法 newInstance 构建一个相同类型的数组,最后再调用 System.arraycopy :
public static Object copyOf(Object a, int newLength) { Class cl = a.getClass; if (!cl.isArray()) return null; Class componentType = cl.getComponentType(); int length = Array.getLength(a); // a 是 Object 对象的引用,所以不能像数组那样直接访问 length 域 Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength)); return newArray; } /* (java.lang.System) public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) src : 原数组 srcPos : 原数据的起始位置 dest : 目标数组 destPos : 目标数组的起始位置 length : 复制范围 */
API - java.lang.reflect.Array
-
-
getXxx / setXxx(原始类型)
-
getLength
- newInstance
五、调用任意方法
在 Method 类中有一个 invoke(调用)方法,允许调用包装在当前 Method 对象中的方法:
Object invoke(Object ImplicitParameter, Object... explicitParamenters)
第一个参数是隐式参数,其余的对象提供了显式参数,对于静态方法,第一个参数可以为 null 。
invoke 的参数和返回值都是 Object 类型,意味着必须进行多次类型转换,不利于在编译时发现错误。除非情况特殊,否则还是建议使用接口和 lambda 表达式(请看下一篇)。