add null check

This commit is contained in:
Looly 2022-05-20 11:33:56 +08:00
parent 586b8d2c85
commit b5619fefbd
4 changed files with 27 additions and 14 deletions

View File

@ -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();
} }

View File

@ -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 {

View File

@ -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);

View File

@ -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();