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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.vci.ubcs.starter.web.util;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.*;
 
public class BeanUtilForVCI {
    private static Logger logger = LoggerFactory.getLogger(BeanUtilForVCI.class);
 
    public BeanUtilForVCI() {
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target) {
        copyPropertiesIgnoreCase(source, target, false);
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target, Map<String, String> fieldMap) {
        copyPropertiesIgnoreCase(source, target, false, fieldMap);
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull) {
        copyPropertiesIgnoreCase(source, target, ignoreNull, (Map)null);
    }
 
    public static void copyDeclaredIgnoreCase(Object source, Object target) {
        copyDeclaredIgnoreCase(source, target, false);
    }
 
    public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull) {
        copyDeclaredIgnoreCase(source, target, ignoreNull, (Map)null);
    }
 
    public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap) {
        copyDeclaredIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)null);
    }
 
    public static void copyDeclaredIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap, Collection<String> ignoreField) {
        Map<String, Field> sourceMap = CacheFieldMap.getDeclaredFieldMap(source.getClass());
        Map<String, Field> targetMap = CacheFieldMap.getDeclaredFieldMap(target.getClass());
        copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap);
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap, Collection<String> ignoreField) {
        Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
        Map<String, Field> targetMap = CacheFieldMap.getFieldMap(target.getClass());
        copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, ignoreField, sourceMap, targetMap);
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap, Collection<String> ignoreField, Map<String, Field> sourceMap, Map<String, Field> targetMap) {
        if (fieldMap == null) {
            fieldMap = new HashMap();
        }
 
        Map<String, String> finalFieldMap = fieldMap;
        targetMap.values().forEach((it) -> {
            boolean not = false;
            if (ignoreField != null && ignoreField.contains(it.getName())) {
                not = true;
            }
 
            if (!not) {
                String itFieldName = it.getName().toLowerCase().replace("_", "");
                itFieldName = (String) finalFieldMap.getOrDefault(itFieldName, itFieldName);
                Field field = (Field)sourceMap.getOrDefault(itFieldName, (Field) null);
                if (field != null) {
                    it.setAccessible(true);
                    field.setAccessible(true);
 
                    try {
                        String sourceClassName = field.getClass().getName();
                        String targetClassName = it.getClass().getName();
                        if ((sourceClassName.equalsIgnoreCase(Boolean.class.getName()) || sourceClassName.equalsIgnoreCase(Boolean.TYPE.getName())) && targetClassName.equalsIgnoreCase(String.class.getName()) || (targetClassName.equalsIgnoreCase(Boolean.class.getName()) || targetClassName.equalsIgnoreCase(Boolean.TYPE.getName())) && sourceClassName.equalsIgnoreCase(String.class.getName())) {
                            if (targetClassName.equalsIgnoreCase(String.class.getName())) {
                                it.set(target, String.valueOf((Boolean)field.get(source)));
                            } else {
                                it.set(target, Boolean.valueOf((String)field.get(source)));
                            }
                        } else if (!ignoreNull) {
                            it.set(target, field.get(source));
                        } else {
                            Object sourceValue = field.get(source);
                            if (sourceValue != null && StringUtils.isNotBlank(sourceValue.toString())) {
                                it.set(target, sourceValue);
                            }
                        }
                    } catch (IllegalAccessException var13) {
                        if (logger.isErrorEnabled()) {
                            logger.error("拷贝属性出错" + var13);
                        }
                    }
                }
            }
 
        });
    }
 
    public static void copyPropertiesIgnoreCase(Object source, Object target, boolean ignoreNull, Map<String, String> fieldMap) {
        copyPropertiesIgnoreCase(source, target, ignoreNull, fieldMap, (Collection)null);
    }
 
    private static class CacheFieldMap {
        private static Map<String, Map<String, Field>> cacheMap = new HashMap();
        private static Map<String, Map<String, Field>> declaredCacheMap = new HashMap();
 
        private CacheFieldMap() {
        }
 
        private static Map<String, Field> getFieldMap(Class clazz) {
            Map<String, Field> result = (Map)cacheMap.get(clazz.getName());
            if (result == null) {
                Class var2 = CacheFieldMap.class;
                synchronized(CacheFieldMap.class) {
                    if (result == null) {
                        Map<String, Field> fieldMap = new HashMap();
                        List<Field> allFields = VciBaseUtil.getAllFieldForObj(clazz);
                        if (!CollectionUtils.isEmpty(allFields)) {
                            allFields.stream().forEach((field) -> {
                                fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
                            });
                        }
 
                        cacheMap.put(clazz.getName(), fieldMap);
                        result = (Map)cacheMap.get(clazz.getName());
                    }
                }
            }
 
            return result;
        }
 
        private static Map<String, Field> getDeclaredFieldMap(Class clazz) {
            Map<String, Field> result = (Map)declaredCacheMap.get(clazz.getName());
            if (result == null) {
                Class var2 = CacheFieldMap.class;
                synchronized(CacheFieldMap.class) {
                    if (result == null) {
                        Map<String, Field> fieldMap = new HashMap();
                        Field[] allFields = clazz.getDeclaredFields();
                        if (allFields != null && allFields.length > 0) {
                            Arrays.stream(allFields).forEach((field) -> {
                                fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
                            });
                        }
 
                        declaredCacheMap.put(clazz.getName(), fieldMap);
                        result = (Map)declaredCacheMap.get(clazz.getName());
                    }
                }
            }
 
            return result;
        }
    }
 
    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param clazz 要转化的类型
     * @param map 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException
     *             如果分析类属性失败
     * @throws IllegalAccessException
     *             如果实例化 JavaBean 失败
     * @throws InstantiationException
     *             如果实例化 JavaBean 失败
     * @throws InvocationTargetException
     *             如果调用属性的 setter 方法失败
     */
    public static <T>T convertMap(Class<T> clazz, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz); // 获取类属性
        T obj = clazz.newInstance();
        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
        for (int i = 0; i< propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
 
            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                Object value = map.get(propertyName);
 
                Object[] args = new Object[1];
                args[0] = value;
                Field privateField = getPrivateField(propertyName, clazz);
                if (privateField == null) {
                }
                privateField.setAccessible(true);
                String type = privateField.getGenericType().toString();
                if (type.equals("class java.lang.String")) {
                    privateField.set(obj, value);
                } else if (type.equals("class java.lang.Boolean")) {
                    privateField.set(obj, Boolean.parseBoolean(String.valueOf(value)));
                } else if (type.equals("class java.lang.Long")) {
                    privateField.set(obj, Long.parseLong(String.valueOf(value)));
                } else if (type.equals("class java.lang.Integer")) {
                    privateField.set(obj, Integer.parseInt(String.valueOf(value)));
                } else if (type.equals("class java.lang.Double")) {
                    privateField.set(obj,Double.parseDouble(String.valueOf(value)));
                } else if (type.equals("class java.lang.Float")) {
                    privateField.set(obj,Float.parseFloat(String.valueOf(value)));
                } else if (type.equals("class java.math.BigDecimal")){
                    privateField.set(obj,new BigDecimal(String.valueOf(value)));
                }//可继续追加类型
            }
        }
        return obj;
    }
    /*拿到反射父类私有属性*/
    private static Field getPrivateField(String name, Class cls) {
        Field declaredField = null;
        try {
            declaredField = cls.getDeclaredField(name);
        } catch (NoSuchFieldException ex) {
 
            if (cls.getSuperclass() == null) {
                return declaredField;
            } else {
                declaredField = getPrivateField(name, cls.getSuperclass());
            }
        }
        return declaredField;
    }
 
    /**
     * 获取到对象中属性为null的属性名
     *
     * @param source
     * @return
     */
    private static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
        Set<String> emptyNames = new HashSet<>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (ObjectUtils.isEmpty(srcValue)) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
 
    /**
     * 拷贝非空对象属性值
     *
     * @param source
     * @param target
     */
    public static void copyPropertiesIgnoreNull(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
    }
 
    /**
     * 集合拷贝非空数据
     *
     * @param source       数据源
     * @param target       目标
     * @param propertyName 要匹配的属性名,例如两个集合使用id进行匹配拷贝 propertyName: "id"
     */
    public static void copyListPropertiesIgnoreNull(List<?> source, List<?> target, String propertyName) {
        if (CollectionUtils.isEmpty(source)) {
            throw new NullPointerException("copyListPropertiesIgnoreNull source源数据为空!");
        }
        Map<Object, Object> map = new HashMap<>(source.size());
        source.forEach(s -> {
            final BeanWrapper sourceBean = new BeanWrapperImpl(s);
            Object value = sourceBean.getPropertyValue(propertyName);
            if (value == null) {
                throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常");
            }
            map.put(value, s);
        });
        target.forEach(s -> {
            final BeanWrapper targetBean = new BeanWrapperImpl(s);
            Object value = targetBean.getPropertyValue(propertyName);
            if (value == null) {
                throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常");
            }
            Object o = map.get(value);
            copyPropertiesIgnoreNull(o, s);
        });
    }
 
}