java中如何遍历实体类的属性和数据类型以及属性值

来源:互联网 发布:魔方数据恢复如何使用 编辑:程序博客网 时间:2024/05/18 03:59
[java] view plain copy
  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(01).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.  }  
0 0
原创粉丝点击