This commit is contained in:
Looly
2022-09-08 01:46:07 +08:00
parent a06c1e65a1
commit bb99d52a87
13 changed files with 190 additions and 54 deletions

View File

@@ -98,20 +98,22 @@ public class CompositeConverter extends RegisterConverter {
*/
@SuppressWarnings("unchecked")
public <T> T convert(Type type, final Object value, final T defaultValue, final boolean isCustomFirst) throws ConvertException {
if (TypeUtil.isUnknown(type) && null == defaultValue) {
// 对于用户不指定目标类型的情况,返回原值
return (T) value;
}
if (ObjUtil.isNull(value)) {
return defaultValue;
}
if (TypeUtil.isUnknown(type)) {
// 对于用户不指定目标类型的情况,返回原值
if(null == defaultValue){
throw new ConvertException("Unsupported convert to unKnow type: {}", type);
return (T) value;
}
type = defaultValue.getClass();
}
// value本身实现了Converter接口直接调用
if(value instanceof Converter){
return ((Converter) value).convert(type, value, defaultValue);
}
if (type instanceof TypeReference) {
type = ((TypeReference<?>) type).getType();
}

View File

@@ -57,6 +57,11 @@ public class BeanConverter implements Converter, Serializable {
return null;
}
// value本身实现了Converter接口直接调用
if(value instanceof Converter){
return ((Converter) value).convert(targetType, value);
}
Class<?> targetClass = TypeUtil.getClass(targetType);
Assert.notNull(targetClass, "Target type is not a class!");
@@ -79,6 +84,6 @@ public class BeanConverter implements Converter, Serializable {
return SerializeUtil.deserialize((byte[]) value);
}
throw new ConvertException("Unsupported source type: {}", value.getClass());
throw new ConvertException("Unsupported source type: [{}] to [{}]", value.getClass(), targetType);
}
}

View File

@@ -67,7 +67,7 @@ public class DateConverter extends AbstractConverter {
return wrap(targetClass, DateUtil.date((TemporalAccessor) value));
} else if (value instanceof Calendar) {
return wrap(targetClass, DateUtil.date((Calendar) value));
} else if (value instanceof Number) {
} else if (null == this.format && value instanceof Number) {
return wrap(targetClass, ((Number) value).longValue());
} else {
// 统一按照字符串处理