mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fic null point bug
This commit is contained in:
@@ -130,7 +130,10 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
* @param bean Bean
|
||||
*/
|
||||
private void mapToBean(Map<?, ?> map, Object bean) {
|
||||
valueProviderToBean(new MapValueProvider(map, this.copyOptions.ignoreCase), bean);
|
||||
valueProviderToBean(
|
||||
new MapValueProvider(map, this.copyOptions.ignoreCase, this.copyOptions.ignoreError),
|
||||
bean
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -18,7 +18,7 @@ public interface ValueProvider<T>{
|
||||
* 返回值一般需要匹配被注入类型,如果不匹配会调用默认转换 Convert#convert(Type, Object)实现转换
|
||||
*
|
||||
* @param key Bean对象中参数名
|
||||
* @param valueType 被注入的值得类型
|
||||
* @param valueType 被注入的值的类型
|
||||
* @return 对应参数名的值
|
||||
*/
|
||||
Object value(T key, Type valueType);
|
||||
|
@@ -3,6 +3,7 @@ package cn.hutool.core.bean.copier.provider;
|
||||
import cn.hutool.core.bean.BeanDesc.PropDesc;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@@ -42,20 +43,23 @@ public class BeanValueProvider implements ValueProvider<String> {
|
||||
//boolean类型字段字段名支持两种方式
|
||||
sourcePd = sourcePdMap.get(StrUtil.upperFirstAndAddPre(key, "is"));
|
||||
}
|
||||
|
||||
|
||||
Object result = null;
|
||||
if (null != sourcePd) {
|
||||
final Method getter = sourcePd.getGetter();
|
||||
if (null != getter) {
|
||||
try {
|
||||
return getter.invoke(source);
|
||||
result = getter.invoke(source);
|
||||
} catch (Exception e) {
|
||||
if (false == ignoreError) {
|
||||
throw new UtilException(e, "Inject [{}] error!", key);
|
||||
}
|
||||
}
|
||||
|
||||
result = Convert.convertWithCheck(valueType,result, null, ignoreError);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -17,14 +17,26 @@ import java.util.Map;
|
||||
public class MapValueProvider implements ValueProvider<String> {
|
||||
|
||||
private final Map<?, ?> map;
|
||||
private final boolean ignoreError;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param map Map
|
||||
* @param ignoreCase 是否忽略key的大小写
|
||||
*/
|
||||
public MapValueProvider(Map<?, ?> map, boolean ignoreCase) {
|
||||
this(map, ignoreCase, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param map Map
|
||||
* @param ignoreCase 是否忽略key的大小写
|
||||
* @since 5.3.2
|
||||
*/
|
||||
public MapValueProvider(Map<?, ?> map, boolean ignoreCase) {
|
||||
public MapValueProvider(Map<?, ?> map, boolean ignoreCase, boolean ignoreError) {
|
||||
if(false == ignoreCase || map instanceof CaseInsensitiveMap) {
|
||||
//不忽略大小写或者提供的Map本身为CaseInsensitiveMap则无需转换
|
||||
this.map = map;
|
||||
@@ -32,6 +44,7 @@ public class MapValueProvider implements ValueProvider<String> {
|
||||
//转换为大小写不敏感的Map
|
||||
this.map = new CaseInsensitiveMap<>(map);
|
||||
}
|
||||
this.ignoreError = ignoreError;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,7 +55,7 @@ public class MapValueProvider implements ValueProvider<String> {
|
||||
value = map.get(StrUtil.toUnderlineCase(key));
|
||||
}
|
||||
|
||||
return Convert.convert(valueType, value);
|
||||
return Convert.convertWithCheck(valueType, value, null, this.ignoreError);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,14 +1,5 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.hutool.core.convert.impl.CollectionConverter;
|
||||
import cn.hutool.core.convert.impl.EnumConverter;
|
||||
import cn.hutool.core.convert.impl.MapConverter;
|
||||
@@ -20,6 +11,21 @@ import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 类型转换器
|
||||
*
|
||||
@@ -674,7 +680,7 @@ public class Convert {
|
||||
* @throws ConvertException 转换器不存在
|
||||
*/
|
||||
public static <T> T convert(Type type, Object value, T defaultValue) throws ConvertException {
|
||||
return ConverterRegistry.getInstance().convert(type, value, defaultValue);
|
||||
return convertWithCheck(type, value, defaultValue, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -703,10 +709,30 @@ public class Convert {
|
||||
* @since 4.5.10
|
||||
*/
|
||||
public static <T> T convertQuietly(Type type, Object value, T defaultValue) {
|
||||
return convertWithCheck(type, value, defaultValue, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换值为指定类型,可选是否不抛异常转换<br>
|
||||
* 当转换失败时返回默认值
|
||||
*
|
||||
* @param <T> 目标类型
|
||||
* @param type 目标类型
|
||||
* @param value 值
|
||||
* @param defaultValue 默认值
|
||||
* @param quietly 是否静默转换,true不抛异常
|
||||
* @return 转换后的值
|
||||
* @since 5.3.2
|
||||
*/
|
||||
public static <T> T convertWithCheck(Type type, Object value, T defaultValue, boolean quietly) {
|
||||
final ConverterRegistry registry = ConverterRegistry.getInstance();
|
||||
try {
|
||||
return convert(type, value, defaultValue);
|
||||
return registry.convert(type, value, defaultValue);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
if(quietly){
|
||||
return defaultValue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@ import cn.hutool.core.convert.AbstractConverter;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
@@ -107,6 +108,10 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
* @return 日期对象
|
||||
*/
|
||||
private TemporalAccessor parseFromCharSequence(CharSequence value) {
|
||||
if(StrUtil.isBlank(value)){
|
||||
return null;
|
||||
}
|
||||
|
||||
final Instant instant;
|
||||
ZoneId zoneId;
|
||||
if (null != this.format) {
|
||||
|
Reference in New Issue
Block a user