用JAVA Bean 反射得到set,get方法

Java代碼  收藏代碼
  1. /**    
  2.  
  3.  * java反射bean的get方法    
  4.  
  5.  *     
  6.  
  7.  * @param objectClass    
  8.  
  9.  * @param fieldName    
  10.  
  11.  * @return    
  12.  
  13.  */       
  14.   
  15. @SuppressWarnings("unchecked")       
  16.   
  17. public static Method getGetMethod(Class objectClass, String fieldName) {       
  18.   
  19.     StringBuffer sb = new StringBuffer();       
  20.   
  21.     sb.append("get");       
  22.   
  23.     sb.append(fieldName.substring(01).toUpperCase());       
  24.   
  25.     sb.append(fieldName.substring(1));       
  26.   
  27.     try {       
  28.   
  29.         return objectClass.getMethod(sb.toString());       
  30.   
  31.     } catch (Exception e) {       
  32.   
  33.     }       
  34.   
  35.     return null;       
  36.   
  37. }       
  38.   
  39.        
  40.   
  41. /**    
  42.  
  43.  * java反射bean的set方法    
  44.  
  45.  *     
  46.  
  47.  * @param objectClass    
  48.  
  49.  * @param fieldName    
  50.  
  51.  * @return    
  52.  
  53.  */       
  54.   
  55. @SuppressWarnings("unchecked")       
  56.   
  57. public static Method getSetMethod(Class objectClass, String fieldName) {       
  58.   
  59.     try {       
  60.   
  61.         Class[] parameterTypes = new Class[1];       
  62.   
  63.         Field field = objectClass.getDeclaredField(fieldName);       
  64.   
  65.         parameterTypes[0] = field.getType();       
  66.   
  67.         StringBuffer sb = new StringBuffer();       
  68.   
  69.         sb.append("set");       
  70.   
  71.         sb.append(fieldName.substring(01).toUpperCase());       
  72.   
  73.         sb.append(fieldName.substring(1));       
  74.   
  75.         Method method = objectClass.getMethod(sb.toString(), parameterTypes);       
  76.   
  77.         return method;       
  78.   
  79.     } catch (Exception e) {       
  80.   
  81.         e.printStackTrace();       
  82.   
  83.     }       
  84.   
  85.     return null;       
  86.   
  87. }       
  88.   
  89.        
  90.   
  91. /**    
  92.  
  93.  * 執行set方法    
  94.  
  95.  *     
  96.  
  97.  * @param o執行對象    
  98.  
  99.  * @param fieldName屬性    
  100.  
  101.  * @param value值    
  102.  
  103.  */       
  104.   
  105. public static void invokeSet(Object o, String fieldName, Object value) {       
  106.   
  107.     Method method = getSetMethod(o.getClass(), fieldName);       
  108.   
  109.     try {       
  110.   
  111.         method.invoke(o, new Object[] { value });       
  112.   
  113.     } catch (Exception e) {       
  114.   
  115.         e.printStackTrace();       
  116.   
  117.     }       
  118.   
  119. }       
  120.   
  121.        
  122.   
  123. /**    
  124.  
  125.  * 執行get方法    
  126.  
  127.  *     
  128.  
  129.  * @param o執行對象    
  130.  
  131.  * @param fieldName屬性    
  132.  
  133.  */       
  134.   
  135. public static Object invokeGet(Object o, String fieldName) {       
  136.   
  137.     Method method = getGetMethod(o.getClass(), fieldName);       
  138.   
  139.     try {       
  140.   
  141.         return method.invoke(o, new Object[0]);       
  142.   
  143.     } catch (Exception e) {       
  144.   
  145.         e.printStackTrace();       
  146.   
  147.     }       
  148.   
  149.     return null;       
  150.   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章