add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.pay.util;
 
import com.dobbinsoft.fw.pay.anntation.MatrixIgnoreCopy;
 
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
 
public class MatrixBeanUtils {
 
    public static final Set<Class> IGNORE_PARAM_LIST = new HashSet();
 
    static {
        IGNORE_PARAM_LIST.add(Boolean.TYPE);
        IGNORE_PARAM_LIST.add(Byte.TYPE);
        IGNORE_PARAM_LIST.add(Character.TYPE);
        IGNORE_PARAM_LIST.add(Short.TYPE);
        IGNORE_PARAM_LIST.add(Integer.TYPE);
        IGNORE_PARAM_LIST.add(Long.TYPE);
        IGNORE_PARAM_LIST.add(Float.TYPE);
        IGNORE_PARAM_LIST.add(Double.TYPE);
        IGNORE_PARAM_LIST.add(Byte.class);
        IGNORE_PARAM_LIST.add(Character.class);
        IGNORE_PARAM_LIST.add(Short.class);
        IGNORE_PARAM_LIST.add(Integer.class);
        IGNORE_PARAM_LIST.add(Long.class);
        IGNORE_PARAM_LIST.add(String.class);
        IGNORE_PARAM_LIST.add(Float.class);
        IGNORE_PARAM_LIST.add(Double.class);
        IGNORE_PARAM_LIST.add(Boolean.class);
        IGNORE_PARAM_LIST.add(List.class);
    }
 
    /**
     * 深度拷贝对象 ,并将 wx -> matrix
     * @param source
     * @param target
     */
    public static void copyWxProperties(Object source, Object target) {
        if (source == null) {
            throw new NullPointerException("Source must not be null");
        }
        if (target == null) {
            throw new NullPointerException("Target must not be null");
        }
        if (source instanceof Collection || target instanceof Collection) {
            throw new RuntimeException("Collection not support");
        }
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();
        List<Field> sourceFields = new ArrayList<>();
        for (Field f : sourceClass.getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                sourceFields.add(f);
            }
        }
        for (Field f : sourceClass.getSuperclass().getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                sourceFields.add(f);
            }
        }
        List<Field> targetFields = new ArrayList<>();
        for (Field f : targetClass.getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                targetFields.add(f);
            }
        }
        for (Field f : targetClass.getSuperclass().getDeclaredFields()) {
            if (!Modifier.isStatic(f.getModifiers())) {
                targetFields.add(f);
            }
        }
        Map<String, Field> targetMap = targetFields.stream().collect(Collectors.toMap(Field::getName, v -> v));
        for (Field sourceField : sourceFields) {
            if (sourceField.getAnnotation(MatrixIgnoreCopy.class) != null) {
                continue;
            }
            String name = sourceField.getName();
            Field targetField = targetMap.get(name);
            if (targetField != null) {
                try {
                    Class<?> targetType = targetField.getType();
                    Class<?> sourceType = sourceField.getType();
                    String getterName = getMethodName(name, "get");
                    Method getter = sourceClass.getMethod(getterName);
                    Object res = getter.invoke(source);
                    String setterName = getMethodName(name, "set");
                    Method setter = targetClass.getMethod(setterName, targetType);
                    if (targetType == sourceType) {
                        // 同类型,直接复制
                        setter.invoke(target, res);
                    } else if (!IGNORE_PARAM_LIST.contains(targetType)) {
                        // 自定义参数
                        if (res == null) {
                            // 直接设置空参
                            setter.invoke(target, null);
                        } else {
                            // 创建对象
                            Object o = targetType.newInstance();
                            // 递归赋值子对象
                            copyWxProperties(res, o);
                            setter.invoke(target, o);
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
    }
 
    public static String getMethodName(String fieldName, String prefix) {
        char[] dst = new char[fieldName.length() + 3];
        prefix.getChars(0, 3, dst, 0);
        fieldName.getChars(0, fieldName.length(), dst, 3);
        if ('a' <= dst[3] && 'z' >= dst[3]) {
            dst[3] = (char)(dst[3] - 32);
        }
 
        return new String(dst);
    }
 
}