BeanExtUtils.java
16.5 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package com.skua.tool.util;
import com.skua.tool.annotation.BeanAnno;
import com.skua.tool.callback.IBeanConvertCallback;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author sonin
* @date 2021/10/2 13:31
* bean类型转换
*/
public class BeanExtUtils {
/**
* Bean => Bean
*
* @param src
* @param targetClass
* @throws Exception
*/
public static <S, T> T bean2Bean(S src, Class<T> targetClass) throws Exception {
// 判空
if (src == null || targetClass == null) {
return null;
}
// 获取Method[]
Method[] srcMethods = src.getClass().getMethods();
Method[] targetMethods = targetClass.getMethods();
// 创建目标对象
T target = targetClass.newInstance();
// 创建 fieldName => Method 缓存
Map<String, Method> srcMap = new HashMap<>(srcMethods.length >> 1);
// 缓存src
for (Method srcMethod : srcMethods) {
String srcMethodName = srcMethod.getName();
if (srcMethodName.startsWith("get") && !"getClass".equals(srcMethodName)) {
srcMap.put(srcMethodName.replaceFirst("get", ""), srcMethod);
}
}
// 根据缓存的src创建target
for (Method targetMethod : targetMethods) {
String targetMethodName = targetMethod.getName();
if (targetMethodName.startsWith("set")) {
Method srcMethod = srcMap.get(targetMethodName.replaceFirst("set", ""));
if (srcMethod == null) {
continue;
}
Object fieldVal = srcMethod.invoke(src);
targetMethod.invoke(target, fieldVal);
}
}
// 清理缓存
srcMap.clear();
// 返回目标对象
return target;
}
/**
* List<S> => List<T>
*
* @param srcList
* @param targetClass
* @param <S>
* @param <T>
* @return
* @throws Exception
*/
public static <S, T> List<T> beans2Beans(List<S> srcList, Class<T> targetClass) throws Exception {
// 判空
if (srcList == null || targetClass == null) {
return null;
}
// 判空
if (srcList.isEmpty()) {
return new ArrayList<>();
}
// 获取Method[]
Method[] srcMethods = srcList.get(0).getClass().getMethods();
Method[] targetMethods = targetClass.getMethods();
// 创建目标对象
List<T> targetList = new ArrayList<>();
// 创建 fieldName => Method 缓存
Map<String, Method> srcMap = new HashMap<>(srcMethods.length >> 1);
Map<String, Method> targetMap = new HashMap<>(targetMethods.length >> 1);
// 遍历
for (S src : srcList) {
T target = targetClass.newInstance();
if (srcMap.isEmpty() && targetMap.isEmpty()) {
for (Method srcMethod : srcMethods) {
String srcMethodName = srcMethod.getName();
if (srcMethodName.startsWith("get") && !"getClass".equals(srcMethodName)) {
srcMap.put(srcMethodName.replaceFirst("get", ""), srcMethod);
}
}
for (Method targetMethod : targetMethods) {
String targetMethodName = targetMethod.getName();
if (targetMethodName.startsWith("set")) {
Method srcMethod = srcMap.get(targetMethodName.replaceFirst("set", ""));
if (srcMethod == null) {
continue;
}
Object fieldVal = srcMethod.invoke(src);
targetMethod.invoke(target, fieldVal);
targetMap.put(targetMethodName.replaceFirst("set", ""), targetMethod);
}
}
} else {
for (Map.Entry<String, Method> item : targetMap.entrySet()) {
String fieldName = item.getKey();
Method srcMethod = srcMap.get(fieldName);
if (srcMethod == null) {
continue;
}
Object fieldVal = srcMethod.invoke(src);
Method targetMethod = targetMap.get(fieldName);
targetMethod.invoke(target, fieldVal);
}
}
targetList.add(target);
}
// 清理缓存
srcMap.clear();
targetMap.clear();
// 返回目标对象
return targetList;
}
/**
* bean => bean 自定义实现值
*
* @param src
* @param targetClass
* @param iBeanConvertCallback
* @param <S>
* @param <T>
* @return
* @throws Exception
*/
public static <S, T> T bean2Bean(S src, Class<T> targetClass, IBeanConvertCallback iBeanConvertCallback) throws Exception {
// 判空
if (src == null || targetClass == null || iBeanConvertCallback == null) {
return null;
}
// 创建目标对象
T target = targetClass.newInstance();
// 缓存
Map<String, Object> srcMap = new HashMap<>();
Map<String, Object> annoMap = new HashMap<>();
// 遍历src对象属性
Class tmpSrcClass = src.getClass();
while (!"java.lang.Object".equals(tmpSrcClass.getName())) {
Field[] srcFields = tmpSrcClass.getDeclaredFields();
for (Field srcField : srcFields) {
srcField.setAccessible(true);
srcMap.put(srcField.getName(), srcField.get(src));
beanAnnoFunc(src, srcField, annoMap);
}
tmpSrcClass = tmpSrcClass.getSuperclass();
}
// 遍历target对象属性
Class tmpTargetClass = targetClass;
while (!"java.lang.Object".equals(tmpTargetClass.getName())) {
Field[] targetFields = tmpTargetClass.getDeclaredFields();
for (Field targetField : targetFields) {
targetField.setAccessible(true);
setTargetFunc(targetField, target, srcMap, annoMap, iBeanConvertCallback);
}
tmpTargetClass = tmpTargetClass.getSuperclass();
}
// 清理缓存
srcMap.clear();
annoMap.clear();
// 返回目标对象
return target;
}
/**
* beans => beans 自定义实现值
*
* @param srcList
* @param targetClass
* @param iBeanConvertCallback
* @param <S>
* @param <T>
* @return
* @throws Exception
*/
public static <S, T> List<T> beans2Beans(List<S> srcList, Class<T> targetClass, IBeanConvertCallback iBeanConvertCallback) throws Exception {
// 判空
if (srcList == null || targetClass == null) {
return null;
}
// 判空
if (srcList.isEmpty()) {
return new ArrayList<>();
}
// 创建目标对象集合
List<T> targetList = new ArrayList<>();
// src、target的Field缓存
List<Field> srcFieldList = new ArrayList<>();
List<Field> targetFieldList = new ArrayList<>();
for (S src : srcList) {
// 创建目标对象
T target = targetClass.newInstance();
// 缓存
Map<String, Object> srcMap = new HashMap<>();
Map<String, Object> annoMap = new HashMap<>();
if (srcFieldList.isEmpty() && targetFieldList.isEmpty()) {
// 遍历src对象属性
Class tmpSrcClass = src.getClass();
while (!"java.lang.Object".equals(tmpSrcClass.getName())) {
Field[] srcFields = tmpSrcClass.getDeclaredFields();
for (Field srcField : srcFields) {
srcField.setAccessible(true);
srcFieldList.add(srcField);
srcMap.put(srcField.getName(), srcField.get(src));
beanAnnoFunc(src, srcField, annoMap);
}
tmpSrcClass = tmpSrcClass.getSuperclass();
}
// 遍历target对象属性
Class tmpTargetClass = targetClass;
while (!"java.lang.Object".equals(tmpTargetClass.getName())) {
Field[] targetFields = tmpTargetClass.getDeclaredFields();
for (Field targetField : targetFields) {
targetField.setAccessible(true);
targetFieldList.add(targetField);
setTargetFunc(targetField, target, srcMap, annoMap, iBeanConvertCallback);
}
tmpTargetClass = tmpTargetClass.getSuperclass();
}
} else {
for (Field srcField : srcFieldList) {
srcField.setAccessible(true);
srcMap.put(srcField.getName(), srcField.get(src));
beanAnnoFunc(src, srcField, annoMap);
}
for (Field targetField : targetFieldList) {
targetField.setAccessible(true);
setTargetFunc(targetField, target, srcMap, annoMap, iBeanConvertCallback);
}
}
// 清理缓存
srcMap.clear();
annoMap.clear();
// 集合添加目标对象
targetList.add(target);
}
// 清理缓存
srcFieldList.clear();
targetFieldList.clear();
// 返回目标对象
return targetList;
}
/**
* 根据注解缓存annoMap
*
* @param src
* @param srcField
* @param annoMap
* @param <S>
* @throws Exception
*/
private static <S> void beanAnnoFunc(S src, Field srcField, Map<String, Object> annoMap) throws Exception {
BeanAnno beanAnno = srcField.getAnnotation(BeanAnno.class);
if (beanAnno != null) {
if (!"".equals(beanAnno.targetFieldName())) {
annoMap.put(beanAnno.targetFieldName(), srcField.get(src));
}
if (beanAnno.targetFieldNames().length > 0) {
for (String targetFieldName : beanAnno.targetFieldNames()) {
if (!"".equals(targetFieldName)) {
annoMap.put(targetFieldName, srcField.get(src));
}
}
}
}
srcField.setAccessible(false);
}
/**
* 设置target的field值
*
* @param targetField
* @param target
* @param srcMap
* @param annoMap
* @param iBeanConvertCallback
* @param <T>
* @throws Exception
*/
private static <T> void setTargetFunc(Field targetField, T target, Map<String, Object> srcMap, Map<String, Object> annoMap, IBeanConvertCallback iBeanConvertCallback) throws Exception {
if (annoMap.get(targetField.getName()) != null) {
Object targetFieldVal = iBeanConvertCallback.doBeanConvert(targetField.getName(), annoMap.get(targetField.getName()));
targetField.set(target, targetFieldVal);
} else {
targetField.set(target, srcMap.get(targetField.getName()));
}
targetField.setAccessible(false);
}
/**
* Map => Bean
*
* @param map
* @param targetClass
* @param <T>
* @return
*/
public static <T> T map2Bean(Map<String, Object> map, Class<T> targetClass) throws Exception {
// 判空
if (map == null || targetClass == null) {
return null;
}
// 判空
if (map.isEmpty()) {
return targetClass.newInstance();
}
// 构建对象
T target = targetClass.newInstance();
// 获取所有方法
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().startsWith("set")) {
// 截取属性名
String fieldName = method.getName();
fieldName = fieldName.toLowerCase().charAt(3) + fieldName.substring(4);
if (map.containsKey(fieldName)) {
method.invoke(target, map.get(fieldName));
}
}
}
return target;
}
/**
* Maps => Beans
*
* @param mapList
* @param targetClass
* @param <T>
* @return
*/
public static <T> List<T> maps2Beans(List<Map<String, Object>> mapList, Class<T> targetClass) throws Exception {
// 判空
if (mapList == null || targetClass == null) {
return null;
}
// 判空
if (mapList.isEmpty()) {
return new ArrayList<>();
}
// 构建对象集合
List<T> targetList = new ArrayList<>();
// fieldName:method 缓存
Map<String, Method> methodMap = new HashMap<>();
// 遍历存储
for (Map<String, Object> map : mapList) {
T target = targetClass.newInstance();
if (methodMap.isEmpty()) {
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().startsWith("set")) {
// 截取属性名
String fieldName = method.getName();
fieldName = fieldName.toLowerCase().charAt(3) + fieldName.substring(4);
if (map.containsKey(fieldName)) {
method.invoke(target, map.get(fieldName));
methodMap.put(fieldName, method);
}
}
}
} else {
for (Map.Entry<String, Method> item : methodMap.entrySet()) {
String fieldName = item.getKey();
Method method = item.getValue();
method.invoke(target, map.get(fieldName));
}
}
targetList.add(target);
}
// 清理缓存
methodMap.clear();
return targetList;
}
/**
* Bean => Map
*
* @param src
* @param <S>
* @return
*/
public static <S> Map<String, Object> bean2Map(S src) throws Exception {
// 判空
if (src == null) {
return new HashMap<>();
}
Map<String, Object> map = new HashMap<>();
Class srcClass = src.getClass();
while (srcClass != null && !"java.lang.Object".equals(srcClass.getName())) {
Field[] srcFields = srcClass.getDeclaredFields();
for (Field srcField : srcFields) {
srcField.setAccessible(true);
map.put(srcField.getName(), srcField.get(src));
srcField.setAccessible(false);
}
srcClass = srcClass.getSuperclass();
}
return map;
}
/**
* Beans => Maps
*
* @param srcList
* @param <S>
* @return
*/
public static <S> List<Map<String, Object>> beans2Maps(List<S> srcList) throws Exception {
// 判空
if (srcList == null) {
return null;
}
// 判空
if (srcList.isEmpty()) {
return new ArrayList<>();
}
List<Map<String, Object>> mapList = new ArrayList<>();
// 字段属性缓存
List<Field> fieldList = new ArrayList<>();
// 遍历存储
for (S src : srcList) {
Map<String, Object> map = new HashMap<>();
if (fieldList.isEmpty()) {
Class srcClass = src.getClass();
while (srcClass != null && !"java.lang.Object".equals(srcClass.getName())) {
Field[] srcFields = srcClass.getDeclaredFields();
for (Field srcField : srcFields) {
srcField.setAccessible(true);
map.put(srcField.getName(), srcField.get(src));
fieldList.add(srcField);
srcField.setAccessible(false);
}
srcClass = srcClass.getSuperclass();
}
} else {
for (Field srcField : fieldList) {
srcField.setAccessible(true);
map.put(srcField.getName(), srcField.get(src));
srcField.setAccessible(false);
}
}
mapList.add(map);
}
// 清理缓存
fieldList.clear();
return mapList;
}
}