mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
增加copyProperties重载,实现根据Class创建对象 并进行属性拷贝
This commit is contained in:
parent
4f3ade54a2
commit
6038c50bb9
@ -573,6 +573,18 @@ public class BeanUtil {
|
|||||||
|
|
||||||
// --------------------------------------------------------------------------------------------- copyProperties
|
// --------------------------------------------------------------------------------------------- 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对象属性
|
* 复制Bean对象属性
|
||||||
*
|
*
|
||||||
|
@ -202,6 +202,20 @@ public class BeanUtilTest {
|
|||||||
Assert.assertTrue(set.contains("subName"));
|
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
|
@Test
|
||||||
public void copyPropertiesHasBooleanTest() {
|
public void copyPropertiesHasBooleanTest() {
|
||||||
SubPerson p1 = new SubPerson();
|
SubPerson p1 = new SubPerson();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user