遍歷獲取javabeam中屬性和值

原文鏈接:http://blog.csdn.net/tonytfjing/article/details/39755345

原文的代碼如下

[java] view plain copy
 print?
  1. /** 
  2.  * 遍歷實體類的屬性和數據類型以及屬性值 
  3.  * @param model 
  4.  * @throws NoSuchMethodException 
  5.  * @throws IllegalAccessException 
  6.  * @throws IllegalArgumentException 
  7.  * @throws InvocationTargetException 
  8.  */  
  9. public static void reflectTest(Object model) throws NoSuchMethodException,  
  10.                 IllegalAccessException, IllegalArgumentException,  
  11.                 InvocationTargetException {  
  12.     // 獲取實體類的所有屬性,返回Field數組  
  13.     Field[] field = model.getClass().getDeclaredFields();  
  14.     // 遍歷所有屬性  
  15.     for (int j = 0; j < field.length; j++) {  
  16.             // 獲取屬性的名字  
  17.             String name = field[j].getName();  
  18.             // 將屬性的首字符大寫,方便構造get,set方法  
  19.             name = name.substring(01).toUpperCase() + name.substring(1);  
  20.             // 獲取屬性的類型  
  21.             String type = field[j].getGenericType().toString();  
  22.             // 如果type是類類型,則前面包含"class ",後面跟類名  
  23.             System.out.println("屬性爲:" + name);  
  24.             if (type.equals("class java.lang.String")) {  
  25.                     Method m = model.getClass().getMethod("get" + name);  
  26.                     // 調用getter方法獲取屬性值  
  27.                     String value = (String) m.invoke(model);  
  28.                     System.out.println("數據類型爲:String");  
  29.                     if (value != null) {  
  30.                             System.out.println("屬性值爲:" + value);  
  31.                     } else {  
  32.                             System.out.println("屬性值爲:空");  
  33.                     }  
  34.             }  
  35.             if (type.equals("class java.lang.Integer")) {  
  36.                     Method m = model.getClass().getMethod("get" + name);  
  37.                     Integer value = (Integer) m.invoke(model);  
  38.                     System.out.println("數據類型爲:Integer");  
  39.                     if (value != null) {  
  40.                             System.out.println("屬性值爲:" + value);  
  41.                     } else {  
  42.                             System.out.println("屬性值爲:空");  
  43.                     }  
  44.             }  
  45.             if (type.equals("class java.lang.Short")) {  
  46.                     Method m = model.getClass().getMethod("get" + name);  
  47.                     Short value = (Short) m.invoke(model);  
  48.                     System.out.println("數據類型爲:Short");  
  49.                     if (value != null) {  
  50.                             System.out.println("屬性值爲:" + value);  
  51.                     } else {  
  52.                             System.out.println("屬性值爲:空");  
  53.                     }  
  54.             }  
  55.             if (type.equals("class java.lang.Double")) {  
  56.                     Method m = model.getClass().getMethod("get" + name);  
  57.                     Double value = (Double) m.invoke(model);  
  58.                     System.out.println("數據類型爲:Double");  
  59.                     if (value != null) {  
  60.                             System.out.println("屬性值爲:" + value);  
  61.                     } else {  
  62.                             System.out.println("屬性值爲:空");  
  63.                     }  
  64.             }  
  65.             if (type.equals("class java.lang.Boolean")) {  
  66.                     Method m = model.getClass().getMethod("get" + name);  
  67.                     Boolean value = (Boolean) m.invoke(model);  
  68.                     System.out.println("數據類型爲:Boolean");  
  69.                     if (value != null) {  
  70.                             System.out.println("屬性值爲:" + value);  
  71.                     } else {  
  72.                             System.out.println("屬性值爲:空");  
  73.                     }  
  74.             }  
  75.             if (type.equals("class java.util.Date")) {  
  76.                     Method m = model.getClass().getMethod("get" + name);  
  77.                     Date value = (Date) m.invoke(model);  
  78.                     System.out.println("數據類型爲:Date");  
  79.                     if (value != null) {  
  80.                             System.out.println("屬性值爲:" + value);  
  81.                     } else {  
  82.                             System.out.println("屬性值爲:空");  
  83.                     }  
  84.             }  
  85.     }  
  86. }  
發佈了7 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章