xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.nuvole.util;
 
import org.apache.commons.beanutils.BeanMap;
 
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
// @formatter:off
 /**
 *                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
 *            __.'  @Author     ~.   .~    代码无Bug   `.__
 *          .'//     liu.q        \./       (秘籍)      \\`.
 *        .'// [916000612@qq.com]  |   欲练神功   引刀自宫  \\`.
 *      .'// .-~"""""""~~~~-._     |    _,-~~~~"""""""~-.  \\`.
 *    .'//.-"  2019-04-07     `-.  |  .-'     19:44       "-.\\`.
 *  .'//______.============-..   \ | /   ..-============.______\\`.
 *.'______________________________\|/______________________________`.
 *
 * @Description : bean map 互相转换工具类
 */
// @formatter:on
 public class BeanUtil {
 
     public static <T> Map<String, Object> bean2Map(T bean, Map<String, Object> mp) {
         if (bean == null) {
             return mp;
         }
         try {
             BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
             for (PropertyDescriptor property : propertyDescriptors) {
                 String k = property.getName();
 
                 if (!k.equals("class")) {
                     Method getter = property.getReadMethod();// Java中提供了用来访问某个属性的
                     // getter/setter方法
                     Object value;
                     value = getter.invoke(bean);
                     mp.put(k, value);
                 }
             }
         } catch (IntrospectionException e) {
             e.printStackTrace();
         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
             e.printStackTrace();
         }
         return mp;
 
     }
 
 
     public static <T> T map2Bean(Map<String, Object> mp, Class<T> beanCls) throws IllegalAccessException, InstantiationException, InvocationTargetException, IntrospectionException {
         T t = null;
         BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);
         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
         t = beanCls.newInstance();
         for (PropertyDescriptor property : propertyDescriptors) {
             String k = property.getName();
 
             if (mp.containsKey(k)) {
                 Object value = mp.get(k);
                 Method setter = property.getWriteMethod();// Java中提供了用来访问某个属性的
                 // getter/setter方法
                 setter.invoke(t, value);
             }
         }
         return t;
     }
 
     public static  Map obj2Map(Object obj) {
         if (obj == null) {
             return new BeanMap();
         }
         return new BeanMap(obj);
     }
 }