mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
9f41698647
commit
886a865aac
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 6.0.0.M1 (2022-06-06)
|
||||
# 6.0.0.M1 (2022-06-07)
|
||||
|
||||
### 计划实现
|
||||
* 【poi 】 PDF相关(基于PdfBox)
|
||||
@ -14,7 +14,6 @@
|
||||
* 【db 】 增加DDL封装
|
||||
* 【json 】 删除JSONNull
|
||||
* 【json 】 实现自定义的类型转换,不影响全局的转换器
|
||||
* 【core 】 Converter设计缺陷,需要改造,使用TypeConverter可以更好的兼容细化类型的转换
|
||||
|
||||
### ❌不兼容特性
|
||||
|
||||
|
@ -45,8 +45,6 @@ import java.io.Serializable;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
@ -71,13 +69,9 @@ import java.util.TimeZone;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.atomic.DoubleAdder;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
|
||||
/**
|
||||
* 转换器登记中心
|
||||
@ -125,24 +119,8 @@ public class ConverterRegistry implements Serializable {
|
||||
* 构造
|
||||
*/
|
||||
public ConverterRegistry() {
|
||||
defaultConverter();
|
||||
putCustomBySpi();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用SPI加载转换器
|
||||
*/
|
||||
private void putCustomBySpi() {
|
||||
ServiceLoaderUtil.load(Converter.class).forEach(converter -> {
|
||||
try {
|
||||
final Type type = TypeUtil.getTypeArgument(ClassUtil.getClass(converter));
|
||||
if (null != type) {
|
||||
putCustom(type, converter);
|
||||
}
|
||||
} catch (final Exception ignore) {
|
||||
// 忽略注册失败的
|
||||
}
|
||||
});
|
||||
registerDefault();
|
||||
registerCustomBySpi();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -228,6 +206,9 @@ public class ConverterRegistry implements Serializable {
|
||||
return defaultValue;
|
||||
}
|
||||
if (TypeUtil.isUnknown(type)) {
|
||||
if(null == defaultValue){
|
||||
throw new ConvertException("Unsupported convert to unknow type: {}", type);
|
||||
}
|
||||
type = defaultValue.getClass();
|
||||
}
|
||||
|
||||
@ -246,8 +227,7 @@ public class ConverterRegistry implements Serializable {
|
||||
if (null != defaultValue) {
|
||||
rowType = (Class<T>) defaultValue.getClass();
|
||||
} else {
|
||||
// 无法识别的泛型类型,按照Object处理
|
||||
return (T) value;
|
||||
throw new ConvertException("Can not get class from type: {}", type);
|
||||
}
|
||||
}
|
||||
|
||||
@ -259,7 +239,7 @@ public class ConverterRegistry implements Serializable {
|
||||
|
||||
// 尝试转Bean
|
||||
if (BeanUtil.isBean(rowType)) {
|
||||
return (T) new BeanConverter().convert(type, value);
|
||||
return (T) BeanConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// 无法转换
|
||||
@ -319,16 +299,14 @@ public class ConverterRegistry implements Serializable {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 集合转换(不可以默认强转)
|
||||
// 集合转换(含有泛型参数,不可以默认强转)
|
||||
if (Collection.class.isAssignableFrom(rowType)) {
|
||||
final CollectionConverter collectionConverter = new CollectionConverter();
|
||||
return (T) collectionConverter.convert(type, value, (Collection<?>) defaultValue);
|
||||
return (T) CollectionConverter.INSTANCE.convert(type, value, (Collection<?>) defaultValue);
|
||||
}
|
||||
|
||||
// Map类型(不可以默认强转)
|
||||
// Map类型(含有泛型参数,不可以默认强转)
|
||||
if (Map.class.isAssignableFrom(rowType)) {
|
||||
final MapConverter mapConverter = new MapConverter();
|
||||
return (T) mapConverter.convert(type, value, (Map<?, ?>) defaultValue);
|
||||
return (T) MapConverter.INSTANCE.convert(type, value, (Map<?, ?>) defaultValue);
|
||||
}
|
||||
|
||||
// 默认强转
|
||||
@ -336,6 +314,16 @@ public class ConverterRegistry implements Serializable {
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
// 原始类型转换
|
||||
if(rowType.isPrimitive()){
|
||||
return PrimitiveConverter.INSTANCE.convert(type, value, defaultValue);
|
||||
}
|
||||
|
||||
// 数字类型转换
|
||||
if(Number.class.isAssignableFrom(rowType)){
|
||||
return NumberConverter.INSTANCE.convert(type, value, defaultValue);
|
||||
}
|
||||
|
||||
// 枚举转换
|
||||
if (rowType.isEnum()) {
|
||||
return EnumConverter.INSTANCE.convert(type, value, defaultValue);
|
||||
@ -355,36 +343,13 @@ public class ConverterRegistry implements Serializable {
|
||||
*
|
||||
* @return 转换器
|
||||
*/
|
||||
private ConverterRegistry defaultConverter() {
|
||||
private ConverterRegistry registerDefault() {
|
||||
defaultConverterMap = new ConcurrentHashMap<>();
|
||||
|
||||
// 原始类型转换器
|
||||
defaultConverterMap.put(int.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(long.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(byte.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(short.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(float.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(double.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(char.class, PrimitiveConverter.INSTANCE);
|
||||
defaultConverterMap.put(boolean.class, PrimitiveConverter.INSTANCE);
|
||||
|
||||
// 包装类转换器
|
||||
defaultConverterMap.put(Number.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(Integer.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(AtomicInteger.class, NumberConverter.INSTANCE);// since 3.0.8
|
||||
defaultConverterMap.put(Long.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(LongAdder.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(AtomicLong.class, NumberConverter.INSTANCE);// since 3.0.8
|
||||
defaultConverterMap.put(Byte.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(Short.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(Float.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(Double.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(DoubleAdder.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(Character.class, new CharacterConverter());
|
||||
defaultConverterMap.put(Boolean.class, new BooleanConverter());
|
||||
defaultConverterMap.put(AtomicBoolean.class, new AtomicBooleanConverter());// since 3.0.8
|
||||
defaultConverterMap.put(BigDecimal.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(BigInteger.class, NumberConverter.INSTANCE);
|
||||
defaultConverterMap.put(CharSequence.class, new StringConverter());
|
||||
defaultConverterMap.put(String.class, new StringConverter());
|
||||
|
||||
@ -435,5 +400,21 @@ public class ConverterRegistry implements Serializable {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用SPI加载转换器
|
||||
*/
|
||||
private void registerCustomBySpi() {
|
||||
ServiceLoaderUtil.load(Converter.class).forEach(converter -> {
|
||||
try {
|
||||
final Type type = TypeUtil.getTypeArgument(ClassUtil.getClass(converter));
|
||||
if (null != type) {
|
||||
putCustom(type, converter);
|
||||
}
|
||||
} catch (final Exception ignore) {
|
||||
// 忽略注册失败的
|
||||
}
|
||||
});
|
||||
}
|
||||
// ----------------------------------------------------------- Private method end
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ import java.util.Map;
|
||||
public class BeanConverter implements Converter, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static BeanConverter INSTANCE = new BeanConverter();
|
||||
|
||||
private final CopyOptions copyOptions;
|
||||
|
||||
/**
|
||||
|
@ -16,6 +16,8 @@ import java.util.Collection;
|
||||
*/
|
||||
public class CollectionConverter implements Converter {
|
||||
|
||||
public static CollectionConverter INSTANCE = new CollectionConverter();
|
||||
|
||||
@Override
|
||||
public Collection<?> convert(Type targetType, final Object value) {
|
||||
if (targetType instanceof TypeReference) {
|
||||
|
@ -23,11 +23,7 @@ import java.util.Objects;
|
||||
public class MapConverter implements Converter, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public MapConverter() {
|
||||
}
|
||||
public static MapConverter INSTANCE = new MapConverter();
|
||||
|
||||
@Override
|
||||
public Object convert(Type targetType, Object value) throws ConvertException {
|
||||
|
@ -45,6 +45,15 @@ public class LocalDateTimeUtil {
|
||||
return LocalDateTime.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当天时间,默认时区
|
||||
*
|
||||
* @return {@link LocalDateTime}
|
||||
*/
|
||||
public static LocalDate today() {
|
||||
return LocalDate.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Instant}转{@link LocalDateTime},使用默认时区
|
||||
*
|
||||
|
@ -41,4 +41,11 @@ public class ConvertToNumberTest {
|
||||
bigDecimal = Convert.toBigDecimal("1L");
|
||||
Assert.assertEquals(1L, bigDecimal.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toNumberTest(){
|
||||
// 直接转换为抽象Number,默认使用BigDecimal实现
|
||||
final Number number = Convert.toNumber("1");
|
||||
Assert.assertEquals(BigDecimal.class, number.getClass());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user