!105 根据Class创建对象 并进行属性拷贝

Merge pull request !105 from oneplustow/v5-dev
This commit is contained in:
Looly 2020-03-16 20:36:07 +08:00 committed by Gitee
commit 72ac89bf30
2 changed files with 26 additions and 0 deletions

View File

@ -573,6 +573,18 @@ public class BeanUtil {
// --------------------------------------------------------------------------------------------- copyProperties
/**
* 创建对应的Class对象并复制Bean对象属性
*
* @param source 源Bean对象
* @param tClass 目标Class
*/
public static <T> T copyProperties(Object source, Class<T> tClass) {
T target = ReflectUtil.newInstance(tClass);
copyProperties(source, target, CopyOptions.create());
return target;
}
/**
* 复制Bean对象属性
*

View File

@ -202,6 +202,20 @@ public class BeanUtilTest {
Assert.assertTrue(set.contains("subName"));
}
@Test
public void copyProperties(){
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("测试A11");
person.setSubName("sub名字");
SubPerson person1 = BeanUtil.copyProperties(person, SubPerson.class);
Assert.assertEquals(14,person1.getAge());
Assert.assertEquals("11213232",person1.getOpenid());
Assert.assertEquals("测试A11",person1.getName());
Assert.assertEquals("sub名字",person1.getSubName());
}
@Test
public void copyPropertiesHasBooleanTest() {
SubPerson p1 = new SubPerson();