package com.syh.jdbc.reflection_super;
/**
* 父类
* @author syh
*
*/
public class Parent {
public String publicField = "1";
String defaultField = "2";
protected String protectedField = "3";
private String privateField = "4" ;
public void publicMethod() {
System.out.println("publicMethod...");
}
void defaultMethod() {
System.out.println("defaultMethod...");
}
protected void protectedMethod() {
System.out.println("protectedMethod...");
}
private void privateMethod() {
System.out.println("privateMethod...");
}
}
package com.syh.jdbc.reflection_super;
/**
* 子类
* @author syh
*
*/
public class Son extends Parent{
}
package com.syh.jdbc.reflection_super;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 方法类
* @author syh
*
*/
public class ReflectionUtils {
/**
* 循环向上转型, 获取对象的 DeclaredMethod
* @param object : 子类对象
* @param methodName : 父类中的方法名
* @param parameterTypes : 父类中的方法参数类型
* @return 父类中的方法对象
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){
Method method = null ;
for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
try {
method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
return method ;
} catch (Exception e) {
}
}
return null;
}
/**
* 直接调用对象方法, 而忽略修饰符(private, protected, default)
* @param object : 子类对象
* @param methodName : 父类中的方法名
* @param parameterTypes : 父类中的方法参数类型
* @param parameters : 父类中的方法参数
* @return 父类中方法的执行结果
*/
public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
Object [] parameters) {
Method method = getDeclaredMethod(object, methodName, parameterTypes) ;
method.setAccessible(true) ;
try {
if(null != method) {
return method.invoke(object, parameters) ;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/**
* 循环向上转型, 获取对象的 DeclaredField
* @param object : 子类对象
* @param fieldName : 父类中的属性名
* @return 父类中的属性对象
*/
public static Field getDeclaredField(Object object, String fieldName){
Field field = null ;
Class<?> clazz = object.getClass() ;
for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName) ;
return field ;
} catch (Exception e) {
}
}
return null;
}
/**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
* @param object : 子类对象
* @param fieldName : 父类中的属性名
* @param value : 将要设置的值
*/
public static void setFieldValue(Object object, String fieldName, Object value){
Field field = getDeclaredField(object, fieldName) ;
field.setAccessible(true) ;
try {
field.set(object, value) ;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
* @param object : 子类对象
* @param fieldName : 父类中的属性名
* @return : 父类中的属性值
*/
public static Object getFieldValue(Object object, String fieldName){
Field field = getDeclaredField(object, fieldName) ;
field.setAccessible(true) ;
try {
return field.get(object) ;
} catch(Exception e) {
e.printStackTrace() ;
}
return null;
}
}
package com.syh.jdbc.reflection_super;
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* 测试类,用JUnit4 进行测试
* @author syh
*
*/
public class ReflectionUtilsTest {
/**
* 测试获取父类的各个方法对象
*/
@Test
public void testGetDeclaredMethod() {
Object obj = new Son() ;
Method publicMethod = ReflectionUtils.getDeclaredMethod(obj, "publicMethod") ;
System.out.println(publicMethod.getName());
Method defaultMethod = ReflectionUtils.getDeclaredMethod(obj, "defaultMethod") ;
System.out.println(defaultMethod.getName());
Method protectedMethod = ReflectionUtils.getDeclaredMethod(obj, "protectedMethod") ;
System.out.println(protectedMethod.getName());
Method privateMethod = ReflectionUtils.getDeclaredMethod(obj, "privateMethod") ;
System.out.println(privateMethod.getName());
}
/**
* 测试调用父类的方法
* @throws Exception
*/
@Test
public void testInvokeMethod() throws Exception {
Object obj = new Son() ;
ReflectionUtils.invokeMethod(obj, "publicMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "defaultMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "protectedMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "privateMethod", null , null) ;
}
/**
* 测试获取父类的各个属性名
*/
@Test
public void testGetDeclaredField() {
Object obj = new Son() ;
Field publicField = ReflectionUtils.getDeclaredField(obj, "publicField") ;
System.out.println(publicField.getName());
Field defaultField = ReflectionUtils.getDeclaredField(obj, "defaultField") ;
System.out.println(defaultField.getName());
Field protectedField = ReflectionUtils.getDeclaredField(obj, "protectedField") ;
System.out.println(protectedField.getName());
Field privateField = ReflectionUtils.getDeclaredField(obj, "privateField") ;
System.out.println(privateField.getName());
}
@Test
public void testSetFieldValue() {
Object obj = new Son() ;
System.out.println("原来的各个属性的值: ");
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
ReflectionUtils.setFieldValue(obj, "publicField", "a") ;
ReflectionUtils.setFieldValue(obj, "defaultField", "b") ;
ReflectionUtils.setFieldValue(obj, "protectedField", "c") ;
ReflectionUtils.setFieldValue(obj, "privateField", "d") ;
System.out.println("***********************************************************");
System.out.println("将属性值改变后的各个属性值: ");
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
}
@Test
public void testGetFieldValue() {
Object obj = new Son() ;
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
}
}