Skip to content

Commit 36d60fe

Browse files
committed
简化行为判断,方便阅读
1 parent dae4e78 commit 36d60fe

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/main/java/cn/winggon/ObjUtils.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void deepCopy(Object source, Object target) {
5656
} else if (target instanceof StringBuilder) {
5757
((StringBuilder) target).append(source);
5858
} else if (target != null) {
59-
copy(source, target, ObjUtils::checkTypeClone);
59+
copy(source, target, true);
6060
}
6161
}
6262

@@ -71,16 +71,16 @@ public static void shallowCopy(Object sourceObj, Object targetObj) {
7171
} else if (targetObj instanceof StringBuilder) {
7272
((StringBuilder) targetObj).append(sourceObj);
7373
} else if (targetObj != null) {
74-
copy(sourceObj, targetObj, v -> v);
74+
copy(sourceObj, targetObj, false);
7575
}
7676
}
7777

7878
/**
7979
* 复制对象
8080
*
81-
* @param fun 值传播方式
81+
* @param deep 是否对值进行深度克隆
8282
*/
83-
private static void copy(Object sourceObj, Object targetObj, Function<Object, Object> fun) {
83+
private static void copy(Object sourceObj, Object targetObj, boolean deep) {
8484
Map<String, Field> a = getFieldMapping(sourceObj.getClass());
8585
Map<String, Field> b = getFieldMapping(targetObj.getClass());
8686
Map<String, Field> targetF;
@@ -100,7 +100,7 @@ private static void copy(Object sourceObj, Object targetObj, Function<Object, Ob
100100
if (sourceField != null) {
101101
try {
102102
Object value = sourceField.get(sourceObj);
103-
targetField.set(targetObj, fun.apply(value)); // 深/浅 克隆关键在于内容的钻取判断
103+
targetField.set(targetObj, deep ? checkTypeClone(value) : value); // 深/浅 克隆关键在于内容的钻取判断
104104
} catch (IllegalAccessException e) {
105105
//
106106
}

0 commit comments

Comments
 (0)