mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bugs
This commit is contained in:
@@ -444,8 +444,22 @@ public class BeanUtil {
|
||||
* @since 4.1.20
|
||||
*/
|
||||
public static <T> T toBean(Object source, Class<T> clazz) {
|
||||
return toBean(source, clazz, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象或Map转Bean
|
||||
*
|
||||
* @param <T> 转换的Bean类型
|
||||
* @param source Bean对象或Map
|
||||
* @param clazz 目标的Bean类型
|
||||
* @param options 属性拷贝选项
|
||||
* @return Bean对象
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static <T> T toBean(Object source, Class<T> clazz, CopyOptions options) {
|
||||
final T target = ReflectUtil.newInstance(clazz);
|
||||
copyProperties(source, target);
|
||||
copyProperties(source, target, options);
|
||||
return target;
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,15 @@
|
||||
package cn.hutool.core.bean.copier.provider;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.bean.BeanDesc.PropDesc;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Bean的值提供者
|
||||
*
|
||||
|
@@ -23,7 +23,6 @@ import java.util.UUID;
|
||||
* Bean工具单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class BeanUtilTest {
|
||||
|
||||
@@ -42,10 +41,10 @@ public class BeanUtilTest {
|
||||
@Override
|
||||
public Object value(String key, Type valueType) {
|
||||
switch (key) {
|
||||
case "name":
|
||||
return "张三";
|
||||
case "age":
|
||||
return 18;
|
||||
case "name":
|
||||
return "张三";
|
||||
case "age":
|
||||
return 18;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -203,17 +202,17 @@ public class BeanUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyProperties(){
|
||||
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());
|
||||
Assert.assertEquals(14, person1.getAge());
|
||||
Assert.assertEquals("11213232", person1.getOpenid());
|
||||
Assert.assertEquals("测试A11", person1.getName());
|
||||
Assert.assertEquals("sub名字", person1.getSubName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -319,4 +318,24 @@ public class BeanUtilTest {
|
||||
public int age;
|
||||
public String openid;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanToBeanTest(){
|
||||
// 复现对象无getter方法导致报错的问题
|
||||
Page page1=new Page();
|
||||
BeanUtil.toBean(page1, Page.class, CopyOptions.create().setIgnoreNullValue(true));
|
||||
}
|
||||
|
||||
public static class Page {
|
||||
private boolean optimizeCountSql = true;
|
||||
|
||||
public boolean optimizeCountSql() {
|
||||
return optimizeCountSql;
|
||||
}
|
||||
|
||||
public Page setOptimizeCountSql(boolean optimizeCountSql) {
|
||||
this.optimizeCountSql = optimizeCountSql;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user