liuxiaolong
2019-05-06 a7bed6b4cfecd61ec153818945f982c5bb796b98
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.cloud.common.utils;
 
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.DateConverter;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * @author xxx
 * @date
 */
public class BeanUtil {
 
    /**
     * bean 转换为map
     *
     * @param bean
     * @return
     */
    public static Map toMap(Object bean) {
        Map<String, Object> objectAsMap = null;
        try {
            objectAsMap = new ObjectMapper().convertValue(bean, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectAsMap;
    }
 
    /**
     * 把一个bean,构建另外一个bean
     *
     * @param requiredType
     * @param source
     * @return
     */
    public static <T> T copy(Class<T> requiredType, Object source) {
        T dest = null;
        try {
            dest = requiredType.newInstance();
        } catch (Exception e1) {
        }
        try {
            PropertyUtils.copyProperties(dest, source);
        } catch (Exception e) {
        }
        return dest;
    }
 
    /**
     * 把一个bean转换成JSON字符串
     *
     * @param obj
     * @return
     */
    public static String getJsonString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullBooleanAsFalse
//                ,
//                SerializerFeature.PrettyFormat
        );
    }
 
    /**
     * 把JSON字符串转换成bean
     *
     * @param requiredType
     * @param jsonData
     * @return
     */
    public static <T> T getBean(Class<T> requiredType, String jsonData) {
        if (jsonData != null) {
            try {
                return JSON.parseObject(jsonData, requiredType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        T t = null;
        try {
            t = requiredType.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
 
    /**
     * 把map改成bean
     *
     * @param requiredType
     * @param map
     * @return
     */
    public static <T> T toBean(Class<T> requiredType, Map map) {
        T t = null;
        String sLong = "java.lang.Long";
        String sString = "java.lang.String";
        try {
            t = requiredType.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //处理时间格式
            DateConverter dateConverter = new DateConverter();
            //设置日期格式
            dateConverter.setPatterns(new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", null});
            //注册格式
            ConvertUtils.register(dateConverter, Date.class);
            BeanUtils.populate(t, map);
        } catch (Exception e) {
            Set<Map.Entry> entrySet = map.entrySet();
            for (Map.Entry entry : entrySet) {
                try {
                    String key = entry.getKey() + "";
                    Object value = entry.getValue();
                    key = key.substring(0, 1).toUpperCase() + key.substring(1);
                    String methodName = "set" + key;
                    Method[] declaredMethods = t.getClass().getDeclaredMethods();
                    for (Method method : declaredMethods) {
                        if (method.getName().equals(methodName)) {
                            Class<?> parameterType = method.getParameterTypes()[0];
                            String parameterTypeName = parameterType.getName();
                            Object parameterValue = null;
 
                            if (parameterTypeName.equals(sLong)) {
                                parameterValue = Long.parseLong(value + "");
                            }
                            if (parameterTypeName.equals(sString)) {
                                parameterValue = value + "";
                            }
                            method.invoke(t, parameterValue);
                        }
                    }
 
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return t;
    }
 
}