java中如何遍曆實體類的屬性和資料類型以及屬性值

來源:互聯網
上載者:User

標籤:get   import   col   類的屬性   pre   invoke   實體類   field   walk   

 
 1 package com.walkerjava.test;   2     3  import java.lang.reflect.Field;   4  import java.lang.reflect.InvocationTargetException;   5  import java.lang.reflect.Method;   6  import java.util.Date;   7     8  /***  9   * 遍曆實體類的屬性和資料類型以及屬性值 10   *  11   * @author LiBaozhen 12   * @date 2013-1-4 上午10:25:02 13   * @company  14   * @version v1.3 15   * @see 相關類 16   * @since 相關/版本 17   */  18  public class ReflectTest {  19          public static void reflectTest(Object model) throws NoSuchMethodException,  20                          IllegalAccessException, IllegalArgumentException,  21                          InvocationTargetException {  22                  // 擷取實體類的所有屬性,返回Field數組  23                  Field[] field = model.getClass().getDeclaredFields();  24                  // 遍曆所有屬性  25                  for (int j = 0; j < field.length; j++) {  26                          // 擷取屬性的名字  27                          String name = field[j].getName();  28                          // 將屬性的首字元大寫,方便構造get,set方法  29                          name = name.substring(0, 1).toUpperCase() + name.substring(1);  30                          // 擷取屬性的類型  31                          String type = field[j].getGenericType().toString();  32                          // 如果type是類類型,則前麵包含"class ",後面跟類名  33                          System.out.println("屬性為:" + name);  34                          if (type.equals("class java.lang.String")) {  35                                  Method m = model.getClass().getMethod("get" + name);  36                                  // 調用getter方法擷取屬性值  37                                  String value = (String) m.invoke(model);  38                                  System.out.println("資料類型為:String");  39                                  if (value != null) {  40                                          System.out.println("屬性值為:" + value);  41                                  } else {  42                                          System.out.println("屬性值為:空");  43                                  }  44                          }  45                          if (type.equals("class java.lang.Integer")) {  46                                  Method m = model.getClass().getMethod("get" + name);  47                                  Integer value = (Integer) m.invoke(model);  48                                  System.out.println("資料類型為:Integer");  49                                  if (value != null) {  50                                          System.out.println("屬性值為:" + value);  51                                  } else {  52                                          System.out.println("屬性值為:空");  53                                  }  54                          }  55                          if (type.equals("class java.lang.Short")) {  56                                  Method m = model.getClass().getMethod("get" + name);  57                                  Short value = (Short) m.invoke(model);  58                                  System.out.println("資料類型為:Short");  59                                  if (value != null) {  60                                          System.out.println("屬性值為:" + value);  61                                  } else {  62                                          System.out.println("屬性值為:空");  63                                  }  64                          }  65                          if (type.equals("class java.lang.Double")) {  66                                  Method m = model.getClass().getMethod("get" + name);  67                                  Double value = (Double) m.invoke(model);  68                                  System.out.println("資料類型為:Double");  69                                  if (value != null) {  70                                          System.out.println("屬性值為:" + value);  71                                  } else {  72                                          System.out.println("屬性值為:空");  73                                  }  74                          }  75                          if (type.equals("class java.lang.Boolean")) {  76                                  Method m = model.getClass().getMethod("get" + name);  77                                  Boolean value = (Boolean) m.invoke(model);  78                                  System.out.println("資料類型為:Boolean");  79                                  if (value != null) {  80                                          System.out.println("屬性值為:" + value);  81                                  } else {  82                                          System.out.println("屬性值為:空");  83                                  }  84                          }  85                          if (type.equals("class java.util.Date")) {  86                                  Method m = model.getClass().getMethod("get" + name);  87                                  Date value = (Date) m.invoke(model);  88                                  System.out.println("資料類型為:Date");  89                                  if (value != null) {  90                                          System.out.println("屬性值為:" + value);  91                                  } else {  92                                          System.out.println("屬性值為:空");  93                                  }  94                          }  95                  }  96          }  97  }  

 

java中如何遍曆實體類的屬性和資料類型以及屬性值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.