This repository was archived by the owner on Oct 17, 2025. It is now read-only.
forked from TaleLin/lin-cms-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeanCopyUtil.java
More file actions
138 lines (128 loc) · 4.46 KB
/
BeanCopyUtil.java
File metadata and controls
138 lines (128 loc) · 4.46 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
package io.github.talelin.latticy.common.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* @author Gadfly
*/
@Slf4j
public class BeanCopyUtil extends BeanUtils {
public static void copyNonNullProperties(Object source, Object target) {
String[] properties = Arrays.stream(ReflectionUtils.getDeclaredMethods(source.getClass()))
.map(method -> {
if (method.getName().startsWith("get")) {
Object fieldValue = null;
try {
fieldValue = method.invoke(source);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if (fieldValue == null) {
String fieldName = method.getName().substring(3);
return com.baomidou.mybatisplus.core.toolkit.StringUtils.firstToLowerCase(fieldName);
}
}
return null;
})
.filter(Objects::nonNull)
.toArray(String[]::new);
copyProperties(source, target, properties);
}
/**
* 集合数据的拷贝
*
* @param source: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @return 拷贝后的集合
*/
public static <S, T> T copyProperties(S source, Supplier<T> target) {
T t = target.get();
copyProperties(source, t);
return t;
}
/**
* 集合数据的拷贝
*
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @return 拷贝后的集合
*/
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
return copyListProperties(sources, target, null);
}
/**
* 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
*
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @param callBack: 回调函数
* @return 拷贝后的集合
*/
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target,
BeanCopyUtilCallBack<S, T> callBack) {
if (CollectionUtils.isEmpty(sources)) {
return new ArrayList<>();
}
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T t = target.get();
copyProperties(source, t);
list.add(t);
if (null != callBack) {
// 回调
callBack.callBack(source, t);
}
}
return list;
}
public static <S, T> T copySingleProperties(S source, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
T t = target.get();
copyProperties(source, t);
if (null != callBack) {
// 回调
callBack.callBack(source, t);
}
return t;
}
/**
* 将集合对象中的类型转换成另一种类型
*
* @param collection 集合
* @param clazz 目标对象
* @return 转换后的集合
*/
public static <S, T> Collection<T> covertObject(Collection<S> collection, Class<T> clazz,
BeanCopyUtilCallBack<S, T> callBack) {
if (CollectionUtils.isEmpty(collection)) {
return new ArrayList<>();
}
return collection.stream().map(oldObject -> {
T instance = null;
try {
instance = clazz.getDeclaredConstructor().newInstance();
copyProperties(oldObject, instance);
if (null != callBack) {
// 回调
callBack.callBack(oldObject, instance);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return instance;
}).collect(Collectors.toList());
}
@FunctionalInterface
public interface BeanCopyUtilCallBack<S, T> {
/**
* 定义默认回调方法
*
* @param t target
* @param s source
*/
void callBack(S s, T t);
}
}