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

原址:点击打开链接


  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. }  

  1. if (type.equals("double")) {  
  2.     Method m = model.getClass().getMethod("get" + name);  
  3.     double value = (double) m.invoke(model);  
  4.     System.out.println("数据类型为:double");  
  5.     if (value >0) {  
  6.             System.out.println("属性值为:" + value);  
  7.     } else {  
  8.             System.out.println("属性值为:空");  
  9.     }  
  10. }  
  11. System.out.println("属性类型为:"+type);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值