mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!105 根据Class创建对象 并进行属性拷贝
Merge pull request !105 from oneplustow/v5-dev
This commit is contained in:
commit
72ac89bf30
@ -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对象属性
|
||||
*
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user