mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add null check
This commit is contained in:
parent
586b8d2c85
commit
b5619fefbd
@ -685,6 +685,9 @@ public class BeanUtil {
|
|||||||
* @return 目标对象
|
* @return 目标对象
|
||||||
*/
|
*/
|
||||||
public static <T> T copyProperties(final Object source, final Class<T> tClass, final String... ignoreProperties) {
|
public static <T> T copyProperties(final Object source, final Class<T> tClass, final String... ignoreProperties) {
|
||||||
|
if(null == source){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
final T target = ConstructorUtil.newInstanceIfPossible(tClass);
|
final T target = ConstructorUtil.newInstanceIfPossible(tClass);
|
||||||
copyProperties(source, target, CopyOptions.create().setIgnoreProperties(ignoreProperties));
|
copyProperties(source, target, CopyOptions.create().setIgnoreProperties(ignoreProperties));
|
||||||
return target;
|
return target;
|
||||||
@ -722,6 +725,9 @@ public class BeanUtil {
|
|||||||
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
|
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
|
||||||
*/
|
*/
|
||||||
public static void copyProperties(final Object source, final Object target, final CopyOptions copyOptions) {
|
public static void copyProperties(final Object source, final Object target, final CopyOptions copyOptions) {
|
||||||
|
if(null == source || null == target){
|
||||||
|
return;
|
||||||
|
}
|
||||||
BeanCopier.create(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();
|
BeanCopier.create(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.bean.copier;
|
package cn.hutool.core.bean.copier;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.copier.Copier;
|
import cn.hutool.core.lang.copier.Copier;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -16,9 +17,8 @@ import java.util.Map;
|
|||||||
* 4. Map 转 Map
|
* 4. Map 转 Map
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author looly
|
|
||||||
*
|
|
||||||
* @param <T> 目标对象类型
|
* @param <T> 目标对象类型
|
||||||
|
* @author looly
|
||||||
* @since 3.2.3
|
* @since 3.2.3
|
||||||
*/
|
*/
|
||||||
public class BeanCopier<T> implements Copier<T>, Serializable {
|
public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||||
@ -29,9 +29,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 创建BeanCopier
|
* 创建BeanCopier
|
||||||
*
|
*
|
||||||
* @param <T> 目标Bean类型
|
* @param <T> 目标Bean类型
|
||||||
* @param source 来源对象,可以是Bean或者Map
|
* @param source 来源对象,可以是Bean或者Map
|
||||||
* @param target 目标Bean对象
|
* @param target 目标Bean对象
|
||||||
* @param copyOptions 拷贝属性选项
|
* @param copyOptions 拷贝属性选项
|
||||||
* @return BeanCopier
|
* @return BeanCopier
|
||||||
*/
|
*/
|
||||||
@ -42,10 +42,10 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 创建BeanCopier
|
* 创建BeanCopier
|
||||||
*
|
*
|
||||||
* @param <T> 目标Bean类型
|
* @param <T> 目标Bean类型
|
||||||
* @param source 来源对象,可以是Bean或者Map
|
* @param source 来源对象,可以是Bean或者Map
|
||||||
* @param target 目标Bean对象
|
* @param target 目标Bean对象
|
||||||
* @param destType 目标的泛型类型,用于标注有泛型参数的Bean对象
|
* @param destType 目标的泛型类型,用于标注有泛型参数的Bean对象
|
||||||
* @param copyOptions 拷贝属性选项
|
* @param copyOptions 拷贝属性选项
|
||||||
* @return BeanCopier
|
* @return BeanCopier
|
||||||
*/
|
*/
|
||||||
@ -56,13 +56,15 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param source 来源对象,可以是Bean或者Map
|
* @param source 来源对象,可以是Bean或者Map,不能为{@code null}
|
||||||
* @param target 目标Bean对象
|
* @param target 目标Bean对象,不能为{@code null}
|
||||||
* @param targetType 目标的泛型类型,用于标注有泛型参数的Bean对象
|
* @param targetType 目标的泛型类型,用于标注有泛型参数的Bean对象
|
||||||
* @param copyOptions 拷贝属性选项
|
* @param copyOptions 拷贝属性选项
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public BeanCopier(final Object source, final T target, final Type targetType, final CopyOptions copyOptions) {
|
public BeanCopier(final Object source, final T target, final Type targetType, final CopyOptions copyOptions) {
|
||||||
|
Assert.notNull(source, "Source bean must be not null!");
|
||||||
|
Assert.notNull(target, "Target bean must be not null!");
|
||||||
final Copier<T> copier;
|
final Copier<T> copier;
|
||||||
if (source instanceof Map) {
|
if (source instanceof Map) {
|
||||||
if (target instanceof Map) {
|
if (target instanceof Map) {
|
||||||
@ -70,7 +72,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
} else {
|
} else {
|
||||||
copier = new MapToBeanCopier<>((Map<?, ?>) source, target, targetType, copyOptions);
|
copier = new MapToBeanCopier<>((Map<?, ?>) source, target, targetType, copyOptions);
|
||||||
}
|
}
|
||||||
}else if(source instanceof ValueProvider){
|
} else if (source instanceof ValueProvider) {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
copier = new ValueProviderToBeanCopier<>((ValueProvider<String>) source, target, targetType, copyOptions);
|
copier = new ValueProviderToBeanCopier<>((ValueProvider<String>) source, target, targetType, copyOptions);
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,7 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
|||||||
* @param source 来源Map
|
* @param source 来源Map
|
||||||
* @param target 目标Bean对象
|
* @param target 目标Bean对象
|
||||||
* @param targetType 目标泛型类型
|
* @param targetType 目标泛型类型
|
||||||
* @param copyOptions 拷贝选项
|
* @param copyOptions 拷贝选项,{@code null}使用默认配置
|
||||||
*/
|
*/
|
||||||
public MapToMapCopier(final Map source, final Map target, final Type targetType, final CopyOptions copyOptions) {
|
public MapToMapCopier(final Map source, final Map target, final Type targetType, final CopyOptions copyOptions) {
|
||||||
super(source, target, copyOptions);
|
super(source, target, copyOptions);
|
||||||
|
@ -567,6 +567,11 @@ public class BeanUtilTest {
|
|||||||
Assert.assertNull(newFood.getCode());
|
Assert.assertNull(newFood.getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyNullTest() {
|
||||||
|
Assert.assertNull(BeanUtil.copyProperties(null, Food.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void copyBeanPropertiesFilterTest() {
|
public void copyBeanPropertiesFilterTest() {
|
||||||
final Food info = new Food();
|
final Food info = new Food();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user