我的家園

          我的家園

          本文給出兩個(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()方法,無法讀取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. }  

           


           


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 两当县| 惠州市| 新丰县| 光泽县| 蒲江县| 容城县| 辰溪县| 泽州县| 禹州市| 荔波县| 沧州市| 乌拉特前旗| 庆云县| 尼木县| 古丈县| 江永县| 札达县| 贵州省| 鱼台县| 柘荣县| 舟山市| 福安市| 黎城县| 华阴市| 卢湾区| 民乐县| 增城市| 浦东新区| 平昌县| 浙江省| 额敏县| 南宁市| 阜平县| 芦山县| 海原县| 红桥区| 朔州市| 虞城县| 南岸区| 阳江市| 龙陵县|