用反射的方式获取父类中的所有属性和方法
- 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 {
- /**
- * 循环向上转型, 获 * @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 method = getDeclaredMethod(object, methodName, parameterTypes) ;
- //抑制Java对方法进行检查,主要是针对私有方法而言
- method.setAccessible(true) ;
- try {
- if(null != method) {
- //调用object 的 method 所代表的方法,其方法的参数是 parameters
- return method.invoke(object, parameters) ;
- }
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 循环向上转型, 获 * @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 修饰符, 也 * @param object : 子类对象
- * @param fieldName : 父类中 * @param value : 将要设置的值
- */
- public static void setFieldValue(Object object, String fieldName, Object value){
- //根据 对象和属性名通过取 Field对象
- Field field = getDeclaredField(object, fieldName) ;
- //抑制Java对其的检查
- field.setAccessible(true) ;
- try {
- //将 object 中 field 所代表的值 设置为 value
- field.set(object, value) ;
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- /**
- * 直接读的属性值, 忽略 private/protected 修饰符, 也 * @param object : 子类对象
- * @param fieldName : 父类中 * @return : 父类中 */
- public static Object getFieldValue(Object object, String fieldName){
- //根据 对象和属性名通过取 Field对象
- Field field = getDeclaredField(object, fieldName) ;
- //抑制Java对其的检查
- 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"));
- }
- }
posted on 2016-07-18 08:54 nickTimer 阅读(26024) 评论(0) 编辑 收藏 举报