我的家園

          我的家園

          本文給出兩個(gè)函數(shù):

             BeanUtils.getFieldValue(object,propertyName);//取出object對(duì)象中的propertyName屬性的值.propertyName只能是object所在類中定義的,而不是其基類定義的

                     BeanUtils.getFieldValueInAllSuper(object,propertyName);//);//取出object對(duì)象中的propertyName屬性的值.propertyName包括在object所在類的基類中定義的屬性

           

            看代碼:

             

          1. public class BeanUtils {  
          2.   
          3.     
          4.     /** 
          5.      * 獲取當(dāng)前類聲明的private/protected變量 
          6.      */  
          7.     static public Object getFieldValue(Object object, String propertyName)  
          8.             throws IllegalAccessException, NoSuchFieldException {  
          9.         Assert.notNull(object);  
          10.         Assert.hasText(propertyName);  
          11.         Field field = object.getClass().getDeclaredField(propertyName);  
          12.         field.setAccessible(true);  
          13.   
          14.         return field.get(object);  
          15.     }  
          16.     /** 
          17.      * zhangpf :因?yàn)間etFieldValue()方法,無(wú)法讀取super class的屬性的值; 
          18.      *                  所以本方法做出擴(kuò)展,允許讀取super class的屬性的值; 
          19.      * @param object 
          20.      * @param propertyName 
          21.      * @return 
          22.      * @throws IllegalAccessException 
          23.      * @throws NoSuchFieldException 
          24.      */  
          25.     public static Object getFieldValueInAllSuper(Object object, String propertyName)  
          26.             throws IllegalAccessException, NoSuchFieldException {  
          27.         Assert.notNull(object);  
          28.         Assert.hasText(propertyName);  
          29.         Class claszz=object.getClass();  
          30.         Field field = null;  
          31.           
          32.         do  
          33.         {     
          34.             try{  
          35.                 field = claszz.getDeclaredField(propertyName);  
          36.             }  
          37.             catch(NoSuchFieldException e)  
          38.             {  
          39.                 //e.printStackTrace();  
          40.                 field=null;  
          41.             }  
          42.             claszz=claszz.getSuperclass();  
          43.         }  
          44.         while(field==null&&claszz!=null);  
          45.           
          46.         if(field==nullreturn null;  
          47.    
          48.         field.setAccessible(true);  
          49.         return field.get(object);  
          50.     }  
          51. }  

           


           


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 石屏县| 兴国县| 灌阳县| 崇州市| 巨野县| 金沙县| 明水县| 孟津县| 西昌市| 昌平区| 承德市| 桦甸市| 巴林右旗| 含山县| 漠河县| 辉县市| 潮安县| 苍梧县| 光泽县| 原平市| 惠水县| 平原县| 嘉定区| 刚察县| 正阳县| 大石桥市| 道孚县| 新乡县| 湾仔区| 宣城市| 三原县| 巴林右旗| 乡宁县| 八宿县| 丰镇市| 黎川县| 东源县| 巫溪县| 惠来县| 娄底市| 海南省|