原文连接:小ben马的java Reflection(反射)基础知识讲解java
1.1)使用 "Class#forName"git
public static Class<?> forName(String className) throws ClassNotFoundException;
若是没有获取到Class对象,则抛出异常 ClassNotFoundException
;github
eg:数组
Class<?> customerClazz = Class.forName("cn.xiaobenma.demo.core.reflection.VipCustomer");
1.2)使用某个类的 ".class",eg:code
Class<?> customerClazz = VipCustomer.class;
1.3)某个对象的 "#getClass()",eg:component
VipCustomer customer = new VipCustomer("001", "小ben马", "10086", VipCustomer.VIP_ADVANCED); Class<?> customerClazz = customer.getClass();
咱们一般使用 "instanceof" 来判断对象是否为某个类的实例。一样,能够使用 "Class#isInstance()" 来判断对象
public native boolean isInstance(Object obj);
例子:blog
boolean isCustomer = customer instanceof VipCustomer; isCustomer = VipCustomer.class.isInstance(customer);
若是person为null, 上述例子都返回false。索引
3.1)使用 "Class#newInstance()": 要保证能访问类的无参构造方法ip
public T newInstance() throws InstantiationException, IllegalAccessException;
3.2)经过 "Constructor#newInstance(Object ... initargs)"
public T newInstance(Object ... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
eg:
Constructor<VipCustomer> constructor = getVipCustomerClass().getConstructor(String.class, String.class, String.class, int.class); VipCustomer customer = constructor.newInstance("001", "小ben马", "10086", VipCustomer.VIP_ADVANCED);
在Class中定义的方法以下:
//a. 获取当前类及其父类,全部`public`类方法和成员方法 public Method[] getMethods() throws SecurityException; //b. 经过方法名、参数类型,获取单个`public`类方法或者成员方法(当前类或父类定义的) public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException; //c. 获取当前类(不包括超类)定义的全部类方法和成员方法 public Method[] getDeclaredMethods() throws SecurityException; //d. 经过方法名、参数类型,获取单个当前类(不包括超类)定义的类方法或成员方法 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException;
在Class中定义的方法以下:
//a. 获取当前类全部`public`构造方法 public Constructor<?>[] getConstructors() throws SecurityException; //b. 根据参数类型,获取当前类单个`public`构造方法 public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException; //c. 获取当前类全部构造方法 public Constructor<?>[] getDeclaredConstructors() throws SecurityException; //d. 根据参数类型,获取当前类单个构造方法 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException;
在Class中定义的方法以下:
//a. 获取当前类及其超类,全部`public`类变量和成员变量 public Field[] getFields() throws SecurityException; //b. 经过名称,获取当前类或超类,单个`public`类变量或者成员变量 public Field getField(String name) throws NoSuchFieldException, SecurityException; //c. 获取当前类(不包括超类),全部类变量和成员变量 public Field[] getDeclaredFields() throws SecurityException; //d. 经过名称,获取当前类(不包括超类),单个类变量或成员变量 public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException;
eg:
Method setRank = getVipCustomerClass().getDeclaredMethod("setRank", int.class); setRank.invoke(customer, VipCustomer.VIP_NORMAL);
8.1) Field#set(Object obj, Object value): 经过对象和变量值,设置变量,eg:
Field field = getVipCustomerClass().getDeclaredField("rank"); field.set(customer, VipCustomer.VIP_ADVANCED);
8.2) Field#get(Object obj): 经过对象获取变量值,eg:
Field field = getVipCustomerClass().getDeclaredField("rank"); int rank = (int) field.get(customer);
在使用反射获取被调用类的构造方法、方法或变量,可能对于调用类是不可访问的,如被调用类的"private"构造方法,"private" 方法, "private" 变量,会抛出 IllegalAccessException。
java能够经过使用 "AccessibleObject#setAccessible(boolean flag)" 改变可访问性。
"Constructor"、"Method" 和 "Field" 都是 "AccessibleObject" 的子类。
eg:
//在VipCustomer中定义类静态常量PRI_NO=100 Field field = getVipCustomerClass().getDeclaredField("PRI_NO"); field.setAccessible(true); Assert.assertEquals(100, field.get(null)); //私有构造方法 Constructor<VipCustomer> constructor = getVipCustomerClass().getDeclaredConstructor(); constructor.setAccessible(true); VipCustomer customer = constructor.newInstance(); Assert.assertNull(customer.getCustomerNo()); Assert.assertNull(customer.getName()); Assert.assertNull(customer.getMobilePhone()); //调用私有类方法 Method method = getVipCustomerClass().getDeclaredMethod("doNothingByVipCustomer"); method.setAccessible(true); method.invoke(null);
数组是比较特殊的类型。
10.1) Array#newInstance(Class<?> componentType, int length),建立一维数组,eg:
//一维数组 Object array = Array.newInstance(String.class, 2); Array.set(array, 0, "小ben马"); Array.set(array, 1, "xiaobenma"); Assert.assertEquals("小ben马", Array.get(array, 0)); Assert.assertEquals("xiaobenma", Array.get(array, 1));
10.2) Array#newInstance(Class<?> componentType, int... dimensions),建立多维数组,eg:
//多维数组 //String[2][1] Object arrays = Array.newInstance(String.class, 2, 1); Object array0 = Array.newInstance(String.class, 1); Array.set(array0, 0, "小ben马"); Array.set(arrays, 0, array0); Object array1 = Array.newInstance(String.class, 1); Array.set(array1, 0, "xiaobenma"); Array.set(arrays, 1, array1); Assert.assertEquals("小ben马", Array.get(Array.get(arrays, 0), 0)); Assert.assertEquals("xiaobenma", Array.get(Array.get(arrays, 1), 0));
10.3)Array#get(Object array, int index)
根据数组和相关索引获取对应的值
10.4)Array#set(Object array, int index, Object value)
根据索引和相关索引,设置对应的值
一般在类中
"Member#getModifiers()" 返回一个int类型,经过解释int的值,能获取定义的修饰符列表。"Constructor"、"Method" 和 "Field" 都是 "Member" 的实现类。
修饰符返回的int值,须要经过 "Modifier" 解析。
其中,
若是你看过Modifier
的源码,你会发现一个有趣的事情,修饰符是按bit
位定义的,如:
/** * The {@code int} value representing the {@code public} * modifier. */ public static final int PUBLIC = 0x00000001; /** * The {@code int} value representing the {@code private} * modifier. */ public static final int PRIVATE = 0x00000002; /** * The {@code int} value representing the {@code protected} * modifier. */ public static final int PROTECTED = 0x00000004;
java.lang.reflect
Proxy
是反射中比较重要的应用,在后续博客单独更新。