java基于反射的Map转Bean的工具类


01 import java.beans.BeanInfo;
02 import java.beans.IntrospectionException;
03 import java.beans.Introspector;
04 import java.beans.PropertyDescriptor;
05 import java.lang.reflect.InvocationTargetException;
06 import java.lang.reflect.Method;
07 import java.sql.Timestamp;
08 import java.util.Date;
09 import java.util.Map;
10  
11 /**
12  *
13  * @ClassName: Transformation
14  * @Description: 数据类型转换
15  *
16  * @author yangtao
17  * @since 2016年10月19日 上午11:36:33
18  *
19  */
20 public class Transformation {
21  
22     /**
23      *
24      * @Title: setMethodValue
25      * @Description: 调用object的set方法,对obj的属性赋值
26      *
27      * @author yangtao
28      * @since 2016年10月19日 上午11:37:35
29      *
30      * @param obj
31      * @param method
32      * @param objects
33      * @throws IllegalAccessException
34      * @throws IllegalArgumentException
35      * @throws InvocationTargetException void
36      */
37     private static void setMethodValue(Object obj, Method method, Object objects) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
38         if (Timestamp.class.getName().equals(method.getParameterTypes()[0].getName())) {
39             if (null != objects && !"null".equals(objects)) {
40                 method.invoke(obj, new Timestamp((long) objects));
41             }
42         else if (Date.class.getName().equals(method.getParameterTypes()[0].getName())) {
43             if (null != objects && !"null".equals(objects)) {
44                 method.invoke(obj, new Timestamp((long) objects));
45             }
46         else if (java.sql.Date.class.getName().equals(method.getParameterTypes()[0].getName())) {
47             if (null != objects && !"null".equals(objects)) {
48                 method.invoke(obj, new Timestamp((long) objects));
49             }
50         else {
51             method.invoke(obj, objects);
52         }
53     }
54  
55     /**
56      *
57      * @Title: convertMap
58      * @Description: 将一个 Map 对象转化为一个 JavaBean
59      *
60      * @author yangtao
61      * @since 2016年10月19日 上午11:37:24
62      *
63      * @param type
64      * @param map
65      * @return
66      * @throws IntrospectionException
67      * @throws IllegalAccessException
68      * @throws InstantiationException
69      * @throws InvocationTargetException T
70      */
71     @SuppressWarnings("rawtypes")
72     public static <T> T convertMap(Class<T> type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
73         BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
74         T instance = type.newInstance(); // 创建 JavaBean 对象
75  
76         // 给 JavaBean 对象的属性赋值
77         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
78         for (int i = 0; i < propertyDescriptors.length; i++) {
79             PropertyDescriptor descriptor = propertyDescriptors[i];
80             String propertyName = descriptor.getName();
81  
82             if (map.containsKey(propertyName)) {
83                 // 下面的 try起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
84                 Object value = map.get(propertyName);
85  
86                 Object[] args = new Object[1];
87                 args[0] = value;
88  
89                 setMethodValue(instance, descriptor.getWriteMethod(), value);
90             }
91         }
92         return instance;
93     }
94 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值