mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -465,36 +465,6 @@ public class BeanUtil {
|
||||
return toBean(source, clazz, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象或Map转Bean,忽略字段转换时发生的异常
|
||||
*
|
||||
* @param <T> 转换的Bean类型
|
||||
* @param source Bean对象或Map
|
||||
* @param clazz 目标的Bean类型
|
||||
* @return Bean对象
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public static <T> T toBeanIgnoreError(final Object source, final Class<T> clazz) {
|
||||
return toBean(source, clazz, CopyOptions.create().setIgnoreError(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象或Map转Bean,忽略字段转换时发生的异常
|
||||
*
|
||||
* @param <T> 转换的Bean类型
|
||||
* @param source Bean对象或Map
|
||||
* @param clazz 目标的Bean类型
|
||||
* @param ignoreError 是否忽略注入错误
|
||||
* @return Bean对象
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public static <T> T toBeanIgnoreCase(final Object source, final Class<T> clazz, final boolean ignoreError) {
|
||||
return toBean(source, clazz,
|
||||
CopyOptions.create()
|
||||
.setIgnoreCase(true)
|
||||
.setIgnoreError(ignoreError));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象或Map转Bean
|
||||
*
|
||||
@@ -513,7 +483,7 @@ public class BeanUtil {
|
||||
* 对象或Map转Bean
|
||||
*
|
||||
* @param <T> 转换的Bean类型
|
||||
* @param source Bean对象或Map
|
||||
* @param source Bean对象或Map或{@link ValueProvider}
|
||||
* @param targetSupplier 目标的Bean创建器
|
||||
* @param options 属性拷贝选项
|
||||
* @return Bean对象
|
||||
@@ -528,22 +498,6 @@ public class BeanUtil {
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* ServletRequest 参数转Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param beanClass Bean Class
|
||||
* @param valueProvider 值提供者
|
||||
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
|
||||
* @return Bean
|
||||
*/
|
||||
public static <T> T toBean(final Class<T> beanClass, final ValueProvider<String> valueProvider, final CopyOptions copyOptions) {
|
||||
if (null == beanClass || null == valueProvider) {
|
||||
return null;
|
||||
}
|
||||
return fillBean(ConstructorUtil.newInstanceIfPossible(beanClass), valueProvider, copyOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充Bean的核心方法
|
||||
*
|
||||
@@ -560,7 +514,6 @@ public class BeanUtil {
|
||||
|
||||
return BeanCopier.create(valueProvider, bean, copyOptions).copy();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------- beanToMap
|
||||
|
||||
/**
|
||||
@@ -575,7 +528,7 @@ public class BeanUtil {
|
||||
public static Map<String, Object> beanToMap(final Object bean, final String... properties) {
|
||||
int mapSize = 16;
|
||||
Editor<String> keyEditor = null;
|
||||
if(ArrayUtil.isNotEmpty(properties)){
|
||||
if (ArrayUtil.isNotEmpty(properties)) {
|
||||
mapSize = properties.length;
|
||||
final Set<String> propertiesSet = SetUtil.of(properties);
|
||||
keyEditor = property -> propertiesSet.contains(property) ? property : null;
|
||||
@@ -685,7 +638,7 @@ public class BeanUtil {
|
||||
* @return 目标对象
|
||||
*/
|
||||
public static <T> T copyProperties(final Object source, final Class<T> tClass, final String... ignoreProperties) {
|
||||
if(null == source){
|
||||
if (null == source) {
|
||||
return null;
|
||||
}
|
||||
final T target = ConstructorUtil.newInstanceIfPossible(tClass);
|
||||
@@ -725,7 +678,7 @@ public class BeanUtil {
|
||||
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
|
||||
*/
|
||||
public static void copyProperties(final Object source, final Object target, final CopyOptions copyOptions) {
|
||||
if(null == source || null == target){
|
||||
if (null == source || null == target) {
|
||||
return;
|
||||
}
|
||||
BeanCopier.create(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.SetUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
@@ -246,7 +247,7 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
|
||||
* @return vo
|
||||
*/
|
||||
public <T> T toBeanIgnoreCase(final Class<T> clazz) {
|
||||
return BeanUtil.toBeanIgnoreCase(this, clazz, false);
|
||||
return BeanUtil.toBean(this, clazz, CopyOptions.create().setIgnoreCase(true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -116,7 +116,7 @@ public class BeanUtilTest {
|
||||
// 错误的类型,此处忽略
|
||||
map.put("age", "aaaaaa");
|
||||
|
||||
final Person person = BeanUtil.toBeanIgnoreError(map, Person.class);
|
||||
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreError(true));
|
||||
Assert.assertEquals("Joe", person.getName());
|
||||
// 错误的类型,不copy这个字段,使用对象创建的默认值
|
||||
Assert.assertEquals(0, person.getAge());
|
||||
@@ -128,7 +128,7 @@ public class BeanUtilTest {
|
||||
map.put("Name", "Joe");
|
||||
map.put("aGe", 12);
|
||||
|
||||
final Person person = BeanUtil.toBeanIgnoreCase(map, Person.class, false);
|
||||
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreCase(true));
|
||||
Assert.assertEquals("Joe", person.getName());
|
||||
Assert.assertEquals(12, person.getAge());
|
||||
}
|
||||
|
Reference in New Issue
Block a user