yangys
2024-03-29 410eed616ce86a76ecfbd272be0a4463ac54a517
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
package com.qianwen.core.tool.utils;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.qianwen.core.tool.beans.BeanProperty;
import com.qianwen.core.tool.beans.BladeBeanCopier;
import com.qianwen.core.tool.beans.BladeBeanMap;
import com.qianwen.core.tool.convert.BladeConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.lang.Nullable;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/BeanUtil.class */
public class BeanUtil extends BeanUtils {
    public static <T> T newInstance(Class<?> clazz) {
        return (T) instantiateClass(clazz);
    }
 
    public static <T> T newInstance(String clazzStr) {
        try {
            Class<?> clazz = ClassUtil.forName(clazzStr, null);
            return (T) newInstance(clazz);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
 
    @Nullable
    public static Object getProperty(@Nullable Object bean, String propertyName) {
        if (bean == null) {
            return null;
        }
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
        return beanWrapper.getPropertyValue(propertyName);
    }
 
    public static void setProperty(Object bean, String propertyName, Object value) {
        Objects.requireNonNull(bean, "bean Could not null");
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
        beanWrapper.setPropertyValue(propertyName, value);
    }
 
    @Nullable
    public static <T> T clone(@Nullable T source) {
        if (source == null) {
            return null;
        }
        return (T) copy((Object) source, (Class<Object>) source.getClass());
    }
 
    @Nullable
    public static <T> T copy(@Nullable Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        return (T) copy(source, source.getClass(), clazz);
    }
 
    @Nullable
    public static <T> T copy(@Nullable Object source, Class sourceClazz, Class<T> targetClazz) {
        if (source == null) {
            return null;
        }
        BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, false);
        T to = (T) newInstance((Class<?>) targetClazz);
        copier.copy(source, to, null);
        return to;
    }
 
    public static <T> List<T> copy(@Nullable Collection<?> sourceList, Class<T> targetClazz) {
        if (sourceList == null || sourceList.isEmpty()) {
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList(sourceList.size());
        Class<?> sourceClazz = null;
        for (Object source : sourceList) {
            if (source != null) {
                if (sourceClazz == null) {
                    sourceClazz = source.getClass();
                }
                arrayList.add(copy(source, sourceClazz, targetClazz));
            }
        }
        return arrayList;
    }
 
    public static void copy(@Nullable Object source, @Nullable Object targetBean) {
        if (source == null || targetBean == null) {
            return;
        }
        BladeBeanCopier copier = BladeBeanCopier.create(source.getClass(), targetBean.getClass(), false);
        copier.copy(source, targetBean, null);
    }
 
    public static void copyNonNull(@Nullable Object source, @Nullable Object targetBean) {
        if (source == null || targetBean == null) {
            return;
        }
        BladeBeanCopier copier = BladeBeanCopier.create(source.getClass(), targetBean.getClass(), false, true);
        copier.copy(source, targetBean, null);
    }
 
    @Nullable
    public static <T> T copyWithConvert(@Nullable Object source, Class<T> targetClazz) {
        if (source == null) {
            return null;
        }
        return (T) copyWithConvert(source, source.getClass(), targetClazz);
    }
 
    @Nullable
    public static <T> T copyWithConvert(@Nullable Object source, Class<?> sourceClazz, Class<T> targetClazz) {
        if (source == null) {
            return null;
        }
        BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, true);
        T to = (T) newInstance((Class<?>) targetClazz);
        copier.copy(source, to, new BladeConverter(sourceClazz, targetClazz));
        return to;
    }
 
    public static <T> List<T> copyWithConvert(@Nullable Collection<?> sourceList, Class<T> targetClazz) {
        if (sourceList == null || sourceList.isEmpty()) {
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList(sourceList.size());
        Class<?> sourceClazz = null;
        for (Object source : sourceList) {
            if (source != null) {
                if (sourceClazz == null) {
                    sourceClazz = source.getClass();
                }
                arrayList.add(copyWithConvert(source, sourceClazz, targetClazz));
            }
        }
        return arrayList;
    }
 
    @Nullable
    public static <T> T copyProperties(@Nullable Object source, Class<T> targetClazz) throws BeansException {
        if (source == null) {
            return null;
        }
        T to = (T) newInstance((Class<?>) targetClazz);
        copyProperties(source, to);
        return to;
    }
 
    public static <T> List<T> copyProperties(@Nullable Collection<?> sourceList, Class<T> targetClazz) throws BeansException {
        if (sourceList == null || sourceList.isEmpty()) {
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList(sourceList.size());
        for (Object source : sourceList) {
            if (source != null) {
                arrayList.add(copyProperties(source, targetClazz));
            }
        }
        return arrayList;
    }
 
    public static Map<String, Object> toMap(@Nullable Object bean) {
        if (bean == null) {
            return new HashMap(0);
        }
        return BladeBeanMap.create(bean);
    }
 
    public static <T> T toBean(Map<String, Object> beanMap, Class<T> valueType) {
        Objects.requireNonNull(beanMap, "beanMap Could not null");
        T to = (T) newInstance((Class<?>) valueType);
        if (beanMap.isEmpty()) {
            return to;
        }
        copy(beanMap, to);
        return to;
    }
 
    @Nullable
    public static Object generator(@Nullable Object superBean, BeanProperty... props) {
        if (superBean == null) {
            return null;
        }
        Class<?> superclass = superBean.getClass();
        Object genBean = generator(superclass, props);
        copy(superBean, genBean);
        return genBean;
    }
 
    public static Object generator(Class<?> superclass, BeanProperty... props) {
        BeanGenerator generator = new BeanGenerator();
        generator.setSuperclass(superclass);
        generator.setUseCache(true);
        for (BeanProperty prop : props) {
            generator.addProperty(prop.getName(), prop.getType());
        }
        return generator.create();
    }
}