用反射的方式获取父类中的所有属性和方法

Java代码  收藏代码
  1. package com.syh.jdbc.reflection_super;  
  2.   
  3. /** 
  4.  * 父类 
  5.  * @author syh 
  6.  * 
  7.  */  
  8.   
  9. public class Parent {  
  10.   
  11.     public String publicField  = "1";  
  12.       
  13.     String defaultField = "2";   
  14.       
  15.     protected String protectedField = "3";  
  16.       
  17.     private String privateField = "4" ;  
  18.       
  19.     public void publicMethod() {  
  20.         System.out.println("publicMethod...");  
  21.     }  
  22.       
  23.     void defaultMethod() {  
  24.         System.out.println("defaultMethod...");  
  25.     }  
  26.       
  27.     protected void protectedMethod() {  
  28.         System.out.println("protectedMethod...");  
  29.     }  
  30.       
  31.     private void privateMethod() {  
  32.         System.out.println("privateMethod...");  
  33.     }  
  34.       
  35. }  


Java代码  收藏代码
  1. package com.syh.jdbc.reflection_super;  
  2.   
  3. /** 
  4.  * 子类 
  5.  * @author syh 
  6.  * 
  7.  */  
  8.   
  9. public class Son extends Parent{  
  10.   
  11. }  


Java代码  收藏代码
  1. package com.syh.jdbc.reflection_super;  
  2.   
  3. import java.lang.reflect.Field;  
  4.   
  5. import java.lang.reflect.InvocationTargetException;  
  6. import java.lang.reflect.Method;  
  7.   
  8. /** 
  9.  * 方法类 
  10.  * @author syh 
  11.  * 
  12.  */  
  13.   
  14. public class ReflectionUtils {  
  15.   
  16.     /** 
  17.      * 循环向上转型, 获     * @param object : 子类对象 
  18.      * @param methodName : 父类中的方法名 
  19.      * @param parameterTypes : 父类中的方法参数类型 
  20.      * @return 父类中的方法对象 
  21.      */  
  22.       
  23.     public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){  
  24.         Method method = null ;  
  25.           
  26.         for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {  
  27.             try {  
  28.                 method = clazz.getDeclaredMethod(methodName, parameterTypes) ;  
  29.                 return method ;  
  30.             } catch (Exception e) {  
  31.                 //这里甚么都能抛出去。  
  32.                 //如果这里的异常打印或者往外抛,则就会进入              
  33.             }  
  34.         }  
  35.           
  36.         return null;  
  37.     }  
  38.       
  39.     /** 
  40.      * 直接调用对象方法, 而忽略修饰符(private, protected, default) 
  41.      * @param object : 子类对象 
  42.      * @param methodName : 父类中的方法名 
  43.      * @param parameterTypes : 父类中的方法参数类型 
  44.      * @param parameters : 父类中的方法参数 
  45.      * @return 父类中方法的执行结果 
  46.      */  
  47.       
  48.     public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,  
  49.             Object [] parameters) {  
  50.         //根据 对象、方法名和对应的方法参数 通过 Method 对象  
  51.         Method method = getDeclaredMethod(object, methodName, parameterTypes) ;  
  52.           
  53.         //抑制Java对方法进行检查,主要是针对私有方法而言  
  54.         method.setAccessible(true) ;  
  55.           
  56.             try {  
  57.                 if(null != method) {  
  58.                       
  59.                     //调用object 的 method 所代表的方法,其方法的参数是 parameters  
  60.                     return method.invoke(object, parameters) ;  
  61.                 }  
  62.             } catch (IllegalArgumentException e) {  
  63.                 e.printStackTrace();  
  64.             } catch (IllegalAccessException e) {  
  65.                 e.printStackTrace();  
  66.             } catch (InvocationTargetException e) {  
  67.                 e.printStackTrace();  
  68.             }  
  69.           
  70.         return null;  
  71.     }  
  72.   
  73.     /** 
  74.      * 循环向上转型, 获     * @param object : 子类对象 
  75.      * @param fieldName : 父类中     * @return 父类中     */  
  76.       
  77.     public static Field getDeclaredField(Object object, String fieldName){  
  78.         Field field = null ;  
  79.           
  80.         Class<?> clazz = object.getClass() ;  
  81.           
  82.         for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {  
  83.             try {  
  84.                 field = clazz.getDeclaredField(fieldName) ;  
  85.                 return field ;  
  86.             } catch (Exception e) {  
  87.                 //这里甚么都能抛出去。  
  88.                 //如果这里的异常打印或者往外抛,则就会进入                  
  89.             }   
  90.         }  
  91.       
  92.         return null;  
  93.     }     
  94.       
  95.     /** 
  96.      * 直接设置对象属性值, 忽略 private/protected 修饰符, 也     * @param object : 子类对象 
  97.      * @param fieldName : 父类中     * @param value : 将要设置的值 
  98.      */  
  99.       
  100.     public static void setFieldValue(Object object, String fieldName, Object value){  
  101.       
  102.         //根据 对象和属性名通过 Field对象  
  103.         Field field = getDeclaredField(object, fieldName) ;  
  104.           
  105.         //抑制Java对其的检查  
  106.         field.setAccessible(true) ;  
  107.           
  108.         try {  
  109.             //将 object 中 field 所代表的值 设置为 value  
  110.              field.set(object, value) ;  
  111.         } catch (IllegalArgumentException e) {  
  112.             e.printStackTrace();  
  113.         } catch (IllegalAccessException e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.           
  117.     }  
  118.       
  119.     /** 
  120.      * 直接读的属性值, 忽略 private/protected 修饰符, 也     * @param object : 子类对象 
  121.      * @param fieldName : 父类中     * @return : 父类中     */  
  122.       
  123.     public static Object getFieldValue(Object object, String fieldName){  
  124.           
  125.         //根据 对象和属性名通过 Field对象  
  126.         Field field = getDeclaredField(object, fieldName) ;  
  127.           
  128.         //抑制Java对其的检查  
  129.         field.setAccessible(true) ;  
  130.           
  131.         try {  
  132.             //获的属性值  
  133.             return field.get(object) ;  
  134.               
  135.         } catch(Exception e) {  
  136.             e.printStackTrace() ;  
  137.         }  
  138.           
  139.         return null;  
  140.     }  
  141. }  


Java代码  收藏代码
  1. package com.syh.jdbc.reflection_super;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.lang.reflect.Field;  
  6. import java.lang.reflect.Method;  
  7.   
  8. import org.junit.Test;  
  9.   
  10. /** 
  11.  * 测试类,用JUnit4 进行测试 
  12.  * @author syh 
  13.  * 
  14.  */  
  15.   
  16. public class ReflectionUtilsTest {  
  17.   
  18.     /** 
  19.      * 测试获父类的各个方法对象 
  20.      */  
  21.       
  22.     @Test  
  23.     public void testGetDeclaredMethod() {  
  24.           
  25.         Object obj = new Son() ;  
  26.           
  27.         //获        Method publicMethod = ReflectionUtils.getDeclaredMethod(obj, "publicMethod") ;  
  28.         System.out.println(publicMethod.getName());  
  29.           
  30.         //获        Method defaultMethod = ReflectionUtils.getDeclaredMethod(obj, "defaultMethod") ;  
  31.         System.out.println(defaultMethod.getName());  
  32.           
  33.         //获        Method protectedMethod = ReflectionUtils.getDeclaredMethod(obj, "protectedMethod") ;  
  34.         System.out.println(protectedMethod.getName());  
  35.           
  36.         //获        Method privateMethod = ReflectionUtils.getDeclaredMethod(obj, "privateMethod") ;  
  37.         System.out.println(privateMethod.getName());  
  38.     }  
  39.   
  40.     /** 
  41.      * 测试调用     * @throws Exception 
  42.      */  
  43.       
  44.     @Test  
  45.     public void testInvokeMethod() throws Exception {  
  46.         Object obj = new Son() ;  
  47.           
  48.         //调用        ReflectionUtils.invokeMethod(obj, "publicMethod", null , null) ;  
  49.           
  50.         //调用        ReflectionUtils.invokeMethod(obj, "defaultMethod", null , null) ;  
  51.           
  52.         //调用        ReflectionUtils.invokeMethod(obj, "protectedMethod", null , null) ;  
  53.           
  54.         //调用        ReflectionUtils.invokeMethod(obj, "privateMethod", null , null) ;  
  55.     }  
  56.   
  57.     /** 
  58.      * 测试获父类的各个属性名 
  59.      */  
  60.       
  61.     @Test  
  62.     public void testGetDeclaredField() {  
  63.           
  64.         Object obj = new Son() ;  
  65.           
  66.         //获        Field publicField = ReflectionUtils.getDeclaredField(obj, "publicField") ;  
  67.         System.out.println(publicField.getName());  
  68.           
  69.         //获        Field defaultField = ReflectionUtils.getDeclaredField(obj, "defaultField") ;  
  70.         System.out.println(defaultField.getName());  
  71.           
  72.         //获        Field protectedField = ReflectionUtils.getDeclaredField(obj, "protectedField") ;  
  73.         System.out.println(protectedField.getName());  
  74.           
  75.         //获        Field privateField = ReflectionUtils.getDeclaredField(obj, "privateField") ;  
  76.         System.out.println(privateField.getName());  
  77.           
  78.     }  
  79.   
  80.     @Test  
  81.     public void testSetFieldValue() {  
  82.           
  83.         Object obj = new Son() ;  
  84.           
  85.         System.out.println("原来的各个属性的值: ");  
  86.         System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));  
  87.         System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));  
  88.         System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));  
  89.         System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));  
  90.           
  91.         ReflectionUtils.setFieldValue(obj, "publicField", "a") ;  
  92.         ReflectionUtils.setFieldValue(obj, "defaultField", "b") ;  
  93.         ReflectionUtils.setFieldValue(obj, "protectedField", "c") ;  
  94.         ReflectionUtils.setFieldValue(obj, "privateField", "d") ;  
  95.           
  96.         System.out.println("***********************************************************");  
  97.           
  98.         System.out.println("将属性值改变后的各个属性值: ");  
  99.         System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));  
  100.         System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));  
  101.         System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));  
  102.         System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));  
  103.           
  104.     }  
  105.   
  106.     @Test  
  107.     public void testGetFieldValue() {  
  108.           
  109.         Object obj = new Son() ;  
  110.           
  111.         System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));  
  112.         System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));  
  113.         System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));  
  114.         System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));  
  115.     }  
  116.   

原文地址:https://www.cnblogs.com/toSeeMyDream/p/5680007.html