思路
- 递归复制
测试代码
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ReflectUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;
public class DeepCopyPropertiesTest {
public static void main(String[] args) {
List<BeanA> beanAList = CollUtil.newArrayList();
for (int i = 0; i < 3; i++) {
BeanA beanA = new BeanA("beanA" + i, i);
beanAList.add(beanA);
}
BeanA beanA = new BeanA("beanAOutSideTheList", 30);
Map<String, BeanA> map = new HashMap<>();
map.put("aa", beanA);
BeanB beanB = new BeanB("beanB", beanAList, beanA, map);
BeanB copyBeanB = new BeanB();
BeanUtil.deepCopyProperties(beanB, copyBeanB);
Console.log(beanB.getBeanA() == copyBeanB.getBeanA());
Console.log(beanB.getBeanAList().get(1) == copyBeanB.getBeanAList().get(1));
Console.log(beanB.getMap().get("aa") == copyBeanB.getMap().get("aa"));
Console.log(beanB);
Console.log(copyBeanB);
}
}
BeanA
public class BeanA {
private String name = "beanA";
private int age = 20;
public BeanA() {
}
public BeanA(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name =