mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
951e34d28e
commit
1473d4625f
@ -1,11 +1,12 @@
|
|||||||
package cn.hutool.core.convert;
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
import cn.hutool.core.reflect.ClassUtil;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.reflect.TypeUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.CharUtil;
|
import cn.hutool.core.util.CharUtil;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,58 +14,30 @@ import java.util.Map;
|
|||||||
* 转换器不会抛出转换异常,转换失败时会返回{@code null}
|
* 转换器不会抛出转换异常,转换失败时会返回{@code null}
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractConverter<T> implements Converter<T>, Serializable {
|
public abstract class AbstractConverter implements TypeConverter, Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
|
||||||
* 不抛异常转换<br>
|
|
||||||
* 当转换失败时返回默认值
|
|
||||||
*
|
|
||||||
* @param value 被转换的值
|
|
||||||
* @param defaultValue 默认值
|
|
||||||
* @return 转换后的值
|
|
||||||
* @since 4.5.7
|
|
||||||
*/
|
|
||||||
public T convertQuietly(final Object value, final T defaultValue) {
|
|
||||||
try {
|
|
||||||
return convert(value, defaultValue);
|
|
||||||
} catch (final Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
public Object convert(Type targetType, final Object value) {
|
||||||
public T convert(final Object value, final T defaultValue) {
|
Assert.notNull(targetType);
|
||||||
Class<T> targetType = getTargetType();
|
|
||||||
if (null == targetType && null == defaultValue) {
|
|
||||||
throw new NullPointerException(StrUtil.format("[type] and [defaultValue] are both null for Converter [{}], we can not know what type to convert !", this.getClass().getName()));
|
|
||||||
}
|
|
||||||
if (null == targetType) {
|
|
||||||
// 目标类型不确定时使用默认值的类型
|
|
||||||
targetType = (Class<T>) defaultValue.getClass();
|
|
||||||
}
|
|
||||||
if (null == value) {
|
if (null == value) {
|
||||||
return defaultValue;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null == defaultValue || targetType.isInstance(defaultValue)) {
|
Class<?> targetClass = TypeUtil.getClass(targetType);
|
||||||
if (targetType.isInstance(value) && false == Map.class.isAssignableFrom(targetType)) {
|
Assert.notNull(targetClass, "Target type is not a class!");
|
||||||
// 除Map外,已经是目标类型,不需要转换(Map类型涉及参数类型,需要单独转换)
|
|
||||||
return targetType.cast(value);
|
// 尝试强转
|
||||||
}
|
if (targetClass.isInstance(value) && false == Map.class.isAssignableFrom(targetClass)) {
|
||||||
final T result = convertInternal(value);
|
// 除Map外,已经是目标类型,不需要转换(Map类型涉及参数类型,需要单独转换)
|
||||||
return ((null == result) ? defaultValue : result);
|
return CastUtil.castTo(targetClass, value);
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
StrUtil.format("Default value [{}]({}) is not the instance of [{}]", defaultValue, defaultValue.getClass(), targetType));
|
|
||||||
}
|
}
|
||||||
|
return convertInternal(targetClass, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 内部转换器,被 {@link AbstractConverter#convert(Object, Object)} 调用,实现基本转换逻辑<br>
|
* 内部转换器,被 {@link AbstractConverter#convert(Type, Object)} 调用,实现基本转换逻辑<br>
|
||||||
* 内部转换器转换后如果转换失败可以做如下操作,处理结果都为返回默认值:
|
* 内部转换器转换后如果转换失败可以做如下操作,处理结果都为返回默认值:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -72,10 +45,11 @@ public abstract class AbstractConverter<T> implements Converter<T>, Serializable
|
|||||||
* 2、抛出一个{@link RuntimeException}异常
|
* 2、抛出一个{@link RuntimeException}异常
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
|
* @param targetClass 目标类型
|
||||||
* @param value 值
|
* @param value 值
|
||||||
* @return 转换后的类型
|
* @return 转换后的类型
|
||||||
*/
|
*/
|
||||||
protected abstract T convertInternal(Object value);
|
protected abstract Object convertInternal(Class<?> targetClass, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 值转为String,用于内部转换中需要使用String中转的情况<br>
|
* 值转为String,用于内部转换中需要使用String中转的情况<br>
|
||||||
@ -98,20 +72,10 @@ public abstract class AbstractConverter<T> implements Converter<T>, Serializable
|
|||||||
return value.toString();
|
return value.toString();
|
||||||
} else if (ArrayUtil.isArray(value)) {
|
} else if (ArrayUtil.isArray(value)) {
|
||||||
return ArrayUtil.toString(value);
|
return ArrayUtil.toString(value);
|
||||||
} else if(CharUtil.isChar(value)) {
|
} else if (CharUtil.isChar(value)) {
|
||||||
//对于ASCII字符使用缓存加速转换,减少空间创建
|
//对于ASCII字符使用缓存加速转换,减少空间创建
|
||||||
return CharUtil.toString((char)value);
|
return CharUtil.toString((char) value);
|
||||||
}
|
}
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得此类实现类的泛型类型
|
|
||||||
*
|
|
||||||
* @return 此类的泛型类型,可能为{@code null}
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Class<T> getTargetType() {
|
|
||||||
return (Class<T>) ClassUtil.getTypeArgument(getClass());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.hutool.core.convert;
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -13,6 +15,18 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class CastUtil {
|
public class CastUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将指定对象强制转换为指定类型
|
||||||
|
*
|
||||||
|
* @param <T> 目标类型
|
||||||
|
* @param targetType 指定目标类型
|
||||||
|
* @param value 被转换的对象
|
||||||
|
* @return 转换后的对象
|
||||||
|
*/
|
||||||
|
public static <T> T castTo(Class<T> targetType, final Object value) {
|
||||||
|
return Assert.notNull(targetType).cast(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 泛型集合向上转型。例如将Collection<Integer>转换为Collection<Number>
|
* 泛型集合向上转型。例如将Collection<Integer>转换为Collection<Number>
|
||||||
*
|
*
|
||||||
|
@ -18,7 +18,7 @@ public interface TypeConverter {
|
|||||||
* @param targetType 目标Type,非泛型类使用
|
* @param targetType 目标Type,非泛型类使用
|
||||||
* @param value 原始值
|
* @param value 原始值
|
||||||
* @return 转换后的值
|
* @return 转换后的值
|
||||||
* @throws IllegalArgumentException 无法确定目标类型,且默认值为{@code null},无法确定类型
|
* @throws ConvertException 转换无法正常完成或转换异常时抛出此异常
|
||||||
*/
|
*/
|
||||||
Object convert(Type targetType, Object value);
|
Object convert(Type targetType, Object value) throws ConvertException;
|
||||||
}
|
}
|
||||||
|
@ -21,15 +21,9 @@ import java.util.List;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class ArrayConverter extends AbstractConverter<Object> {
|
public class ArrayConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<?> targetType;
|
|
||||||
/**
|
|
||||||
* 目标元素类型
|
|
||||||
*/
|
|
||||||
private final Class<?> targetComponentType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否忽略元素转换错误
|
* 是否忽略元素转换错误
|
||||||
*/
|
*/
|
||||||
@ -37,46 +31,32 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
|
||||||
* @param targetType 目标数组类型
|
|
||||||
*/
|
*/
|
||||||
public ArrayConverter(final Class<?> targetType) {
|
public ArrayConverter() {
|
||||||
this(targetType, false);
|
this(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param targetType 目标数组类型
|
|
||||||
* @param ignoreElementError 是否忽略元素转换错误
|
* @param ignoreElementError 是否忽略元素转换错误
|
||||||
*/
|
*/
|
||||||
public ArrayConverter(Class<?> targetType, final boolean ignoreElementError) {
|
public ArrayConverter(final boolean ignoreElementError) {
|
||||||
if (null == targetType) {
|
|
||||||
// 默认Object数组
|
|
||||||
targetType = Object[].class;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetType.isArray()) {
|
|
||||||
this.targetType = targetType;
|
|
||||||
this.targetComponentType = targetType.getComponentType();
|
|
||||||
} else {
|
|
||||||
//用户传入类为非数组时,按照数组元素类型对待
|
|
||||||
this.targetComponentType = targetType;
|
|
||||||
this.targetType = ArrayUtil.getArrayType(targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.ignoreElementError = ignoreElementError;
|
this.ignoreElementError = ignoreElementError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object convertInternal(final Object value) {
|
protected Object convertInternal(Class<?> targetClass, final Object value) {
|
||||||
return value.getClass().isArray() ? convertArrayToArray(value) : convertObjectToArray(value);
|
final Class<?> targetComponentType;
|
||||||
}
|
if (targetClass.isArray()) {
|
||||||
|
targetComponentType = targetClass.getComponentType();
|
||||||
|
} else {
|
||||||
|
//用户传入类为非数组时,按照数组元素类型对待
|
||||||
|
targetComponentType = targetClass;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
return value.getClass().isArray() ? convertArrayToArray(targetComponentType, value)
|
||||||
@Override
|
: convertObjectToArray(targetComponentType, value);
|
||||||
public Class getTargetType() {
|
|
||||||
return this.targetType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,7 +77,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
* @param array 被转换的数组值
|
* @param array 被转换的数组值
|
||||||
* @return 转换后的数组
|
* @return 转换后的数组
|
||||||
*/
|
*/
|
||||||
private Object convertArrayToArray(final Object array) {
|
private Object convertArrayToArray(final Class<?> targetComponentType, final Object array) {
|
||||||
final Class<?> valueComponentType = ArrayUtil.getComponentType(array);
|
final Class<?> valueComponentType = ArrayUtil.getComponentType(array);
|
||||||
|
|
||||||
if (valueComponentType == targetComponentType) {
|
if (valueComponentType == targetComponentType) {
|
||||||
@ -108,7 +88,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
final Object result = Array.newInstance(targetComponentType, len);
|
final Object result = Array.newInstance(targetComponentType, len);
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
Array.set(result, i, convertComponentType(Array.get(array, i)));
|
Array.set(result, i, convertComponentType(targetComponentType, Array.get(array, i)));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -116,13 +96,14 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
/**
|
/**
|
||||||
* 非数组对数组转换
|
* 非数组对数组转换
|
||||||
*
|
*
|
||||||
|
* @param targetComponentType 目标单个节点类型
|
||||||
* @param value 被转换值
|
* @param value 被转换值
|
||||||
* @return 转换后的数组
|
* @return 转换后的数组
|
||||||
*/
|
*/
|
||||||
private Object convertObjectToArray(final Object value) {
|
private Object convertObjectToArray(final Class<?> targetComponentType, final Object value) {
|
||||||
if (value instanceof CharSequence) {
|
if (value instanceof CharSequence) {
|
||||||
if (targetComponentType == char.class || targetComponentType == Character.class) {
|
if (targetComponentType == char.class || targetComponentType == Character.class) {
|
||||||
return convertArrayToArray(value.toString().toCharArray());
|
return convertArrayToArray(targetComponentType, value.toString().toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
//issue#2365
|
//issue#2365
|
||||||
@ -137,7 +118,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
|
|
||||||
// 单纯字符串情况下按照逗号分隔后劈开
|
// 单纯字符串情况下按照逗号分隔后劈开
|
||||||
final String[] strings = StrUtil.splitToArray(value.toString(), CharUtil.COMMA);
|
final String[] strings = StrUtil.splitToArray(value.toString(), CharUtil.COMMA);
|
||||||
return convertArrayToArray(strings);
|
return convertArrayToArray(targetComponentType, strings);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Object result;
|
final Object result;
|
||||||
@ -146,7 +127,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
final List<?> list = (List<?>) value;
|
final List<?> list = (List<?>) value;
|
||||||
result = Array.newInstance(targetComponentType, list.size());
|
result = Array.newInstance(targetComponentType, list.size());
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
Array.set(result, i, convertComponentType(list.get(i)));
|
Array.set(result, i, convertComponentType(targetComponentType, list.get(i)));
|
||||||
}
|
}
|
||||||
} else if (value instanceof Collection) {
|
} else if (value instanceof Collection) {
|
||||||
// 集合转数组
|
// 集合转数组
|
||||||
@ -155,7 +136,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (final Object element : collection) {
|
for (final Object element : collection) {
|
||||||
Array.set(result, i, convertComponentType(element));
|
Array.set(result, i, convertComponentType(targetComponentType, element));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
} else if (value instanceof Iterable) {
|
} else if (value instanceof Iterable) {
|
||||||
@ -163,14 +144,14 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
final List<?> list = IterUtil.toList((Iterable<?>) value);
|
final List<?> list = IterUtil.toList((Iterable<?>) value);
|
||||||
result = Array.newInstance(targetComponentType, list.size());
|
result = Array.newInstance(targetComponentType, list.size());
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
Array.set(result, i, convertComponentType(list.get(i)));
|
Array.set(result, i, convertComponentType(targetComponentType, list.get(i)));
|
||||||
}
|
}
|
||||||
} else if (value instanceof Iterator) {
|
} else if (value instanceof Iterator) {
|
||||||
// 可循环对象转数组,可循环对象无法获取长度,因此先转为List后转为数组
|
// 可循环对象转数组,可循环对象无法获取长度,因此先转为List后转为数组
|
||||||
final List<?> list = IterUtil.toList((Iterator<?>) value);
|
final List<?> list = IterUtil.toList((Iterator<?>) value);
|
||||||
result = Array.newInstance(targetComponentType, list.size());
|
result = Array.newInstance(targetComponentType, list.size());
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
Array.set(result, i, convertComponentType(list.get(i)));
|
Array.set(result, i, convertComponentType(targetComponentType, list.get(i)));
|
||||||
}
|
}
|
||||||
}else if (value instanceof Number && byte.class == targetComponentType) {
|
}else if (value instanceof Number && byte.class == targetComponentType) {
|
||||||
// 用户可能想序列化指定对象
|
// 用户可能想序列化指定对象
|
||||||
@ -180,7 +161,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
result = ObjUtil.serialize(value);
|
result = ObjUtil.serialize(value);
|
||||||
} else {
|
} else {
|
||||||
// everything else:
|
// everything else:
|
||||||
result = convertToSingleElementArray(value);
|
result = convertToSingleElementArray(targetComponentType, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -192,9 +173,9 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
* @param value 被转换的值
|
* @param value 被转换的值
|
||||||
* @return 数组,只包含一个元素
|
* @return 数组,只包含一个元素
|
||||||
*/
|
*/
|
||||||
private Object[] convertToSingleElementArray(final Object value) {
|
private Object[] convertToSingleElementArray(final Class<?> targetComponentType, final Object value) {
|
||||||
final Object[] singleElementArray = ArrayUtil.newArray(targetComponentType, 1);
|
final Object[] singleElementArray = ArrayUtil.newArray(targetComponentType, 1);
|
||||||
singleElementArray[0] = convertComponentType(value);
|
singleElementArray[0] = convertComponentType(targetComponentType, value);
|
||||||
return singleElementArray;
|
return singleElementArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,8 +186,8 @@ public class ArrayConverter extends AbstractConverter<Object> {
|
|||||||
* @return 转换后的值,转换失败若{@link #ignoreElementError}为true,返回null,否则抛出异常
|
* @return 转换后的值,转换失败若{@link #ignoreElementError}为true,返回null,否则抛出异常
|
||||||
* @since 5.4.3
|
* @since 5.4.3
|
||||||
*/
|
*/
|
||||||
private Object convertComponentType(final Object value) {
|
private Object convertComponentType(final Class<?> targetComponentType, final Object value) {
|
||||||
return Convert.convertWithCheck(this.targetComponentType, value, null, this.ignoreElementError);
|
return Convert.convertWithCheck(targetComponentType, value, null, this.ignoreElementError);
|
||||||
}
|
}
|
||||||
// -------------------------------------------------------------------------------------- Private method end
|
// -------------------------------------------------------------------------------------- Private method end
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,25 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link AtomicBoolean}转换器
|
* {@link AtomicBoolean}转换器
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
public class AtomicBooleanConverter extends AbstractConverter<AtomicBoolean> {
|
public class AtomicBooleanConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AtomicBoolean convertInternal(final Object value) {
|
protected AtomicBoolean convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if (value instanceof Boolean) {
|
if (value instanceof Boolean) {
|
||||||
return new AtomicBoolean((Boolean) value);
|
return new AtomicBoolean((Boolean) value);
|
||||||
}
|
}
|
||||||
final String valueStr = convertToStr(value);
|
final String valueStr = convertToStr(value);
|
||||||
return new AtomicBoolean(BooleanUtil.toBoolean(valueStr));
|
return new AtomicBoolean(BooleanUtil.toBoolean(valueStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,11 @@ import java.util.concurrent.atomic.AtomicIntegerArray;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.4.5
|
* @since 5.4.5
|
||||||
*/
|
*/
|
||||||
public class AtomicIntegerArrayConverter extends AbstractConverter<AtomicIntegerArray> {
|
public class AtomicIntegerArrayConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AtomicIntegerArray convertInternal(final Object value) {
|
protected AtomicIntegerArray convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return new AtomicIntegerArray(Convert.convert(int[].class, value));
|
return new AtomicIntegerArray(Convert.convert(int[].class, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,11 +11,11 @@ import java.util.concurrent.atomic.AtomicLongArray;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.4.5
|
* @since 5.4.5
|
||||||
*/
|
*/
|
||||||
public class AtomicLongArrayConverter extends AbstractConverter<AtomicLongArray> {
|
public class AtomicLongArrayConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AtomicLongArray convertInternal(final Object value) {
|
protected AtomicLongArray convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return new AtomicLongArray(Convert.convert(long[].class, value));
|
return new AtomicLongArray(Convert.convert(long[].class, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,24 +1,23 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.convert.ConverterRegistry;
|
import cn.hutool.core.convert.ConverterRegistry;
|
||||||
import cn.hutool.core.reflect.TypeUtil;
|
import cn.hutool.core.reflect.TypeUtil;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link AtomicReference}转换器
|
* {@link AtomicReference}转换器
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("rawtypes")
|
public class AtomicReferenceConverter extends AbstractConverter {
|
||||||
public class AtomicReferenceConverter extends AbstractConverter<AtomicReference> {
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AtomicReference<?> convertInternal(final Object value) {
|
protected AtomicReference<?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
|
|
||||||
//尝试将值转换为Reference泛型的类型
|
//尝试将值转换为Reference泛型的类型
|
||||||
Object targetValue = null;
|
Object targetValue = null;
|
||||||
|
@ -26,7 +26,7 @@ import java.util.Map;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 4.0.2
|
* @since 4.0.2
|
||||||
*/
|
*/
|
||||||
public class BeanConverter<T> extends AbstractConverter<T> {
|
public class BeanConverter<T> extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Type beanType;
|
private final Type beanType;
|
||||||
@ -65,7 +65,7 @@ public class BeanConverter<T> extends AbstractConverter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected T convertInternal(final Object value) {
|
protected T convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if(value instanceof Map ||
|
if(value instanceof Map ||
|
||||||
value instanceof ValueProvider ||
|
value instanceof ValueProvider ||
|
||||||
BeanUtil.isBean(value.getClass())) {
|
BeanUtil.isBean(value.getClass())) {
|
||||||
@ -83,9 +83,4 @@ public class BeanConverter<T> extends AbstractConverter<T> {
|
|||||||
|
|
||||||
throw new ConvertException("Unsupported source type: {}", value.getClass());
|
throw new ConvertException("Unsupported source type: {}", value.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<T> getTargetType() {
|
|
||||||
return this.beanClass;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,11 @@ import cn.hutool.core.util.BooleanUtil;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class BooleanConverter extends AbstractConverter<Boolean> {
|
public class BooleanConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean convertInternal(final Object value) {
|
protected Boolean convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if (value instanceof Number) {
|
if (value instanceof Number) {
|
||||||
// 0为false,其它数字为true
|
// 0为false,其它数字为true
|
||||||
return 0 != ((Number) value).doubleValue();
|
return 0 != ((Number) value).doubleValue();
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日期转换器
|
* 日期转换器
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CalendarConverter extends AbstractConverter<Calendar> {
|
public class CalendarConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/** 日期格式化 */
|
/** 日期格式化 */
|
||||||
@ -38,7 +38,7 @@ public class CalendarConverter extends AbstractConverter<Calendar> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Calendar convertInternal(final Object value) {
|
protected Calendar convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
// Handle Date
|
// Handle Date
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return DateUtil.calendar((Date)value);
|
return DateUtil.calendar((Date)value);
|
||||||
|
@ -10,19 +10,13 @@ import cn.hutool.core.convert.ConvertException;
|
|||||||
* @param <T> 强制转换到的类型
|
* @param <T> 强制转换到的类型
|
||||||
* @since 4.0.2
|
* @since 4.0.2
|
||||||
*/
|
*/
|
||||||
public class CastConverter<T> extends AbstractConverter<T> {
|
public class CastConverter<T> extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private Class<T> targetType;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected T convertInternal(final Object value) {
|
protected T convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
// 由于在AbstractConverter中已经有类型判断并强制转换,因此当在上一步强制转换失败时直接抛出异常
|
// 由于在AbstractConverter中已经有类型判断并强制转换,因此当在上一步强制转换失败时直接抛出异常
|
||||||
throw new ConvertException("Can not cast value to [{}]", this.targetType);
|
throw new ConvertException("Can not cast value to [{}]", targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<T> getTargetType() {
|
|
||||||
return this.targetType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,11 @@ import cn.hutool.core.text.StrUtil;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CharacterConverter extends AbstractConverter<Character> {
|
public class CharacterConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Character convertInternal(final Object value) {
|
protected Character convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if (value instanceof Boolean) {
|
if (value instanceof Boolean) {
|
||||||
return BooleanUtil.toCharacter((Boolean) value);
|
return BooleanUtil.toCharacter((Boolean) value);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编码对象转换器
|
* 编码对象转换器
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CharsetConverter extends AbstractConverter<Charset>{
|
public class CharsetConverter extends AbstractConverter{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Charset convertInternal(final Object value) {
|
protected Charset convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return CharsetUtil.charset(convertToStr(value));
|
return CharsetUtil.charset(convertToStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import cn.hutool.core.classloader.ClassLoaderUtil;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class ClassConverter extends AbstractConverter<Class<?>> {
|
public class ClassConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final boolean isInitialized;
|
private final boolean isInitialized;
|
||||||
@ -32,7 +32,7 @@ public class ClassConverter extends AbstractConverter<Class<?>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?> convertInternal(final Object value) {
|
protected Class<?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return ClassLoaderUtil.loadClass(convertToStr(value), isInitialized);
|
return ClassLoaderUtil.loadClass(convertToStr(value), isInitialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.convert.Converter;
|
import cn.hutool.core.convert.TypeConverter;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
|
||||||
import cn.hutool.core.reflect.TypeUtil;
|
import cn.hutool.core.reflect.TypeUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@ -14,7 +13,7 @@ import java.util.Collection;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
public class CollectionConverter implements Converter<Collection<?>> {
|
public class CollectionConverter implements TypeConverter {
|
||||||
|
|
||||||
/** 集合类型 */
|
/** 集合类型 */
|
||||||
private final Type collectionType;
|
private final Type collectionType;
|
||||||
@ -60,9 +59,8 @@ public class CollectionConverter implements Converter<Collection<?>> {
|
|||||||
// ---------------------------------------------------------------------------------------------- Constractor end
|
// ---------------------------------------------------------------------------------------------- Constractor end
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<?> convert(final Object value, final Collection<?> defaultValue) throws IllegalArgumentException {
|
public Collection<?> convert(final Type targetType, final Object value) {
|
||||||
final Collection<?> result = convertInternal(value);
|
return convertInternal(value);
|
||||||
return ObjUtil.defaultIfNull(result, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.Currency;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
|
import java.util.Currency;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 货币{@link Currency} 转换器
|
* 货币{@link Currency} 转换器
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
public class CurrencyConverter extends AbstractConverter<Currency> {
|
public class CurrencyConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Currency convertInternal(final Object value) {
|
protected Currency convertInternal(Class<?> targetClass, final Object value) {
|
||||||
return Currency.getInstance(convertToStr(value));
|
return Currency.getInstance(convertToStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,10 +14,9 @@ import java.util.Calendar;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class DateConverter extends AbstractConverter<java.util.Date> {
|
public class DateConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<? extends java.util.Date> targetType;
|
|
||||||
/**
|
/**
|
||||||
* 日期格式化
|
* 日期格式化
|
||||||
*/
|
*/
|
||||||
@ -26,20 +25,9 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
|||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param targetType 目标类型
|
|
||||||
*/
|
|
||||||
public DateConverter(final Class<? extends java.util.Date> targetType) {
|
|
||||||
this.targetType = targetType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param targetType 目标类型
|
|
||||||
* @param format 日期格式
|
* @param format 日期格式
|
||||||
*/
|
*/
|
||||||
public DateConverter(final Class<? extends java.util.Date> targetType, final String format) {
|
public DateConverter(final String format) {
|
||||||
this.targetType = targetType;
|
|
||||||
this.format = format;
|
this.format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,16 +50,16 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected java.util.Date convertInternal(final Object value) {
|
protected java.util.Date convertInternal(Class<?> targetClass, final Object value) {
|
||||||
if (value == null || (value instanceof CharSequence && StrUtil.isBlank(value.toString()))) {
|
if (value == null || (value instanceof CharSequence && StrUtil.isBlank(value.toString()))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (value instanceof TemporalAccessor) {
|
if (value instanceof TemporalAccessor) {
|
||||||
return wrap(DateUtil.date((TemporalAccessor) value));
|
return wrap(targetClass, DateUtil.date((TemporalAccessor) value));
|
||||||
} else if (value instanceof Calendar) {
|
} else if (value instanceof Calendar) {
|
||||||
return wrap(DateUtil.date((Calendar) value));
|
return wrap(targetClass, DateUtil.date((Calendar) value));
|
||||||
} else if (value instanceof Number) {
|
} else if (value instanceof Number) {
|
||||||
return wrap(((Number) value).longValue());
|
return wrap(targetClass, ((Number) value).longValue());
|
||||||
} else {
|
} else {
|
||||||
// 统一按照字符串处理
|
// 统一按照字符串处理
|
||||||
final String valueStr = convertToStr(value);
|
final String valueStr = convertToStr(value);
|
||||||
@ -79,11 +67,11 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
|||||||
? DateUtil.parse(valueStr) //
|
? DateUtil.parse(valueStr) //
|
||||||
: DateUtil.parse(valueStr, this.format);
|
: DateUtil.parse(valueStr, this.format);
|
||||||
if (null != dateTime) {
|
if (null != dateTime) {
|
||||||
return wrap(dateTime);
|
return wrap(targetClass, dateTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ConvertException("Can not convert {}:[{}] to {}", value.getClass().getName(), value, this.targetType.getName());
|
throw new ConvertException("Can not convert {}:[{}] to {}", value.getClass().getName(), value, targetClass.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,25 +80,25 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
|||||||
* @param date Date
|
* @param date Date
|
||||||
* @return 目标类型对象
|
* @return 目标类型对象
|
||||||
*/
|
*/
|
||||||
private java.util.Date wrap(final DateTime date) {
|
private java.util.Date wrap(Class<?> targetClass, final DateTime date) {
|
||||||
// 返回指定类型
|
// 返回指定类型
|
||||||
if (java.util.Date.class == targetType) {
|
if (java.util.Date.class == targetClass) {
|
||||||
return date.toJdkDate();
|
return date.toJdkDate();
|
||||||
}
|
}
|
||||||
if (DateTime.class == targetType) {
|
if (DateTime.class == targetClass) {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
if (java.sql.Date.class == targetType) {
|
if (java.sql.Date.class == targetClass) {
|
||||||
return date.toSqlDate();
|
return date.toSqlDate();
|
||||||
}
|
}
|
||||||
if (java.sql.Time.class == targetType) {
|
if (java.sql.Time.class == targetClass) {
|
||||||
return new java.sql.Time(date.getTime());
|
return new java.sql.Time(date.getTime());
|
||||||
}
|
}
|
||||||
if (java.sql.Timestamp.class == targetType) {
|
if (java.sql.Timestamp.class == targetClass) {
|
||||||
return date.toTimestamp();
|
return date.toTimestamp();
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", this.targetType.getName()));
|
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", targetClass.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,30 +107,24 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
|||||||
* @param mills Date
|
* @param mills Date
|
||||||
* @return 目标类型对象
|
* @return 目标类型对象
|
||||||
*/
|
*/
|
||||||
private java.util.Date wrap(final long mills) {
|
private java.util.Date wrap(Class<?> targetClass, final long mills) {
|
||||||
// 返回指定类型
|
// 返回指定类型
|
||||||
if (java.util.Date.class == targetType) {
|
if (java.util.Date.class == targetClass) {
|
||||||
return new java.util.Date(mills);
|
return new java.util.Date(mills);
|
||||||
}
|
}
|
||||||
if (DateTime.class == targetType) {
|
if (DateTime.class == targetClass) {
|
||||||
return DateUtil.date(mills);
|
return DateUtil.date(mills);
|
||||||
}
|
}
|
||||||
if (java.sql.Date.class == targetType) {
|
if (java.sql.Date.class == targetClass) {
|
||||||
return new java.sql.Date(mills);
|
return new java.sql.Date(mills);
|
||||||
}
|
}
|
||||||
if (java.sql.Time.class == targetType) {
|
if (java.sql.Time.class == targetClass) {
|
||||||
return new java.sql.Time(mills);
|
return new java.sql.Time(mills);
|
||||||
}
|
}
|
||||||
if (java.sql.Timestamp.class == targetType) {
|
if (java.sql.Timestamp.class == targetClass) {
|
||||||
return new java.sql.Timestamp(mills);
|
return new java.sql.Timestamp(mills);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", this.targetType.getName()));
|
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", targetClass.getName()));
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public Class<java.util.Date> getTargetType() {
|
|
||||||
return (Class<java.util.Date>) this.targetType;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,11 @@ import java.time.temporal.TemporalAmount;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.0.0
|
* @since 5.0.0
|
||||||
*/
|
*/
|
||||||
public class DurationConverter extends AbstractConverter<Duration> {
|
public class DurationConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Duration convertInternal(final Object value) {
|
protected Duration convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if(value instanceof TemporalAmount){
|
if(value instanceof TemporalAmount){
|
||||||
return Duration.from((TemporalAmount) value);
|
return Duration.from((TemporalAmount) value);
|
||||||
} else if(value instanceof Long){
|
} else if(value instanceof Long){
|
||||||
|
@ -22,7 +22,7 @@ import java.util.stream.Collectors;
|
|||||||
* @since 4.0.2
|
* @since 4.0.2
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public class EnumConverter extends AbstractConverter<Object> {
|
public class EnumConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final WeakConcurrentMap<Class<?>, Map<Class<?>, Method>> VALUE_OF_METHOD_CACHE = new WeakConcurrentMap<>();
|
private static final WeakConcurrentMap<Class<?>, Map<Class<?>, Method>> VALUE_OF_METHOD_CACHE = new WeakConcurrentMap<>();
|
||||||
@ -39,7 +39,7 @@ public class EnumConverter extends AbstractConverter<Object> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object convertInternal(final Object value) {
|
protected Object convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
Enum enumValue = tryConvertEnum(value, this.enumClass);
|
Enum enumValue = tryConvertEnum(value, this.enumClass);
|
||||||
if (null == enumValue && false == value instanceof String) {
|
if (null == enumValue && false == value instanceof String) {
|
||||||
// 最后尝试先将value转String,再valueOf转换
|
// 最后尝试先将value转String,再valueOf转换
|
||||||
@ -53,11 +53,6 @@ public class EnumConverter extends AbstractConverter<Object> {
|
|||||||
throw new ConvertException("Can not convert {} to {}", value, this.enumClass);
|
throw new ConvertException("Can not convert {} to {}", value, this.enumClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class getTargetType() {
|
|
||||||
return this.enumClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 尝试转换,转换规则为:
|
* 尝试转换,转换规则为:
|
||||||
* <ul>
|
* <ul>
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* {@link Locale}对象转换器<br>
|
* {@link Locale}对象转换器<br>
|
||||||
@ -13,11 +13,11 @@ import cn.hutool.core.text.StrUtil;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public class LocaleConverter extends AbstractConverter<Locale> {
|
public class LocaleConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Locale convertInternal(final Object value) {
|
protected Locale convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
try {
|
try {
|
||||||
final String str = convertToStr(value);
|
final String str = convertToStr(value);
|
||||||
if (StrUtil.isEmpty(str)) {
|
if (StrUtil.isEmpty(str)) {
|
||||||
|
@ -17,7 +17,7 @@ import java.util.Objects;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
public class MapConverter extends AbstractConverter<Map<?, ?>> {
|
public class MapConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/** Map类型 */
|
/** Map类型 */
|
||||||
@ -51,7 +51,7 @@ public class MapConverter extends AbstractConverter<Map<?, ?>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
protected Map<?, ?> convertInternal(final Object value) {
|
protected Map<?, ?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
Map map;
|
Map map;
|
||||||
if (value instanceof Map) {
|
if (value instanceof Map) {
|
||||||
final Class<?> valueClass = value.getClass();
|
final Class<?> valueClass = value.getClass();
|
||||||
@ -70,7 +70,7 @@ public class MapConverter extends AbstractConverter<Map<?, ?>> {
|
|||||||
} else if (BeanUtil.isBean(value.getClass())) {
|
} else if (BeanUtil.isBean(value.getClass())) {
|
||||||
map = BeanUtil.beanToMap(value);
|
map = BeanUtil.beanToMap(value);
|
||||||
// 二次转换,转换键值类型
|
// 二次转换,转换键值类型
|
||||||
map = convertInternal(map);
|
map = convertInternal(targetClass, map);
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
|
throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
|
||||||
}
|
}
|
||||||
@ -91,10 +91,4 @@ public class MapConverter extends AbstractConverter<Map<?, ?>> {
|
|||||||
targetMap.put(key, value);
|
targetMap.put(key, value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Class<Map<?, ?>> getTargetType() {
|
|
||||||
return (Class<Map<?, ?>>) TypeUtil.getClass(this.mapType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ import java.util.function.Function;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class NumberConverter extends AbstractConverter<Number> {
|
public class NumberConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<? extends Number> targetType;
|
private final Class<? extends Number> targetType;
|
||||||
@ -55,13 +55,7 @@ public class NumberConverter extends AbstractConverter<Number> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
protected Number convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
public Class<Number> getTargetType() {
|
|
||||||
return (Class<Number>) this.targetType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Number convertInternal(final Object value) {
|
|
||||||
return convert(value, this.targetType, this::convertToStr);
|
return convert(value, this.targetType, this::convertToStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ import cn.hutool.core.lang.Opt;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.7.16
|
* @since 5.7.16
|
||||||
*/
|
*/
|
||||||
public class OptConverter extends AbstractConverter<Opt<?>> {
|
public class OptConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Opt<?> convertInternal(final Object value) {
|
protected Opt<?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return Opt.ofNullable(value);
|
return Opt.ofNullable(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,11 +11,11 @@ import java.util.Optional;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.0.0
|
* @since 5.0.0
|
||||||
*/
|
*/
|
||||||
public class OptionalConverter extends AbstractConverter<Optional<?>> {
|
public class OptionalConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Optional<?> convertInternal(final Object value) {
|
protected Optional<?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return Optional.ofNullable(value);
|
return Optional.ofNullable(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字符串转换器
|
* 字符串转换器
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class PathConverter extends AbstractConverter<Path>{
|
public class PathConverter extends AbstractConverter{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Path convertInternal(final Object value) {
|
protected Path convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
try {
|
try {
|
||||||
if(value instanceof URI){
|
if(value instanceof URI){
|
||||||
return Paths.get((URI)value);
|
return Paths.get((URI)value);
|
||||||
|
@ -12,11 +12,11 @@ import java.time.temporal.TemporalAmount;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.0.0
|
* @since 5.0.0
|
||||||
*/
|
*/
|
||||||
public class PeriodConverter extends AbstractConverter<Period> {
|
public class PeriodConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Period convertInternal(final Object value) {
|
protected Period convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if(value instanceof TemporalAmount){
|
if(value instanceof TemporalAmount){
|
||||||
return Period.from((TemporalAmount) value);
|
return Period.from((TemporalAmount) value);
|
||||||
}else if(value instanceof Integer){
|
}else if(value instanceof Integer){
|
||||||
|
@ -24,29 +24,20 @@ import java.util.function.Function;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class PrimitiveConverter extends AbstractConverter<Object> {
|
public class PrimitiveConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<?> targetType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造<br>
|
* 构造<br>
|
||||||
*
|
*
|
||||||
* @param clazz 需要转换的原始
|
|
||||||
* @throws IllegalArgumentException 传入的转换类型非原始类型时抛出
|
* @throws IllegalArgumentException 传入的转换类型非原始类型时抛出
|
||||||
*/
|
*/
|
||||||
public PrimitiveConverter(final Class<?> clazz) {
|
public PrimitiveConverter() {
|
||||||
if (null == clazz) {
|
|
||||||
throw new NullPointerException("PrimitiveConverter not allow null target type!");
|
|
||||||
} else if (false == clazz.isPrimitive()) {
|
|
||||||
throw new IllegalArgumentException("[" + clazz + "] is not a primitive class!");
|
|
||||||
}
|
|
||||||
this.targetType = clazz;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object convertInternal(final Object value) {
|
protected Object convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return PrimitiveConverter.convert(value, this.targetType, this::convertToStr);
|
return PrimitiveConverter.convert(value, targetClass, this::convertToStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,12 +45,6 @@ public class PrimitiveConverter extends AbstractConverter<Object> {
|
|||||||
return StrUtil.trim(super.convertToStr(value));
|
return StrUtil.trim(super.convertToStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Class<Object> getTargetType() {
|
|
||||||
return (Class<Object>) this.targetType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定值转换为原始类型的值
|
* 将指定值转换为原始类型的值
|
||||||
* @param value 值
|
* @param value 值
|
||||||
|
@ -17,7 +17,7 @@ import java.lang.reflect.Type;
|
|||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public class ReferenceConverter extends AbstractConverter<Reference> {
|
public class ReferenceConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<? extends Reference> targetType;
|
private final Class<? extends Reference> targetType;
|
||||||
@ -32,7 +32,7 @@ public class ReferenceConverter extends AbstractConverter<Reference> {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
protected Reference<?> convertInternal(final Object value) {
|
protected Reference<?> convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
|
|
||||||
//尝试将值转换为Reference泛型的类型
|
//尝试将值转换为Reference泛型的类型
|
||||||
Object targetValue = null;
|
Object targetValue = null;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link StackTraceElement} 转换器<br>
|
* {@link StackTraceElement} 转换器<br>
|
||||||
* 只支持Map方式转换
|
* 只支持Map方式转换
|
||||||
@ -13,11 +13,11 @@ import cn.hutool.core.util.ObjUtil;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
public class StackTraceElementConverter extends AbstractConverter<StackTraceElement> {
|
public class StackTraceElementConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected StackTraceElement convertInternal(final Object value) {
|
protected StackTraceElement convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if (value instanceof Map) {
|
if (value instanceof Map) {
|
||||||
final Map<?, ?> map = (Map<?, ?>) value;
|
final Map<?, ?> map = (Map<?, ?>) value;
|
||||||
|
|
||||||
|
@ -19,11 +19,11 @@ import java.util.TimeZone;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class StringConverter extends AbstractConverter<String> {
|
public class StringConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String convertInternal(final Object value) {
|
protected String convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
if (value instanceof TimeZone) {
|
if (value instanceof TimeZone) {
|
||||||
return ((TimeZone) value).getID();
|
return ((TimeZone) value).getID();
|
||||||
} else if (value instanceof org.w3c.dom.Node) {
|
} else if (value instanceof org.w3c.dom.Node) {
|
||||||
|
@ -37,10 +37,9 @@ import java.util.Objects;
|
|||||||
* @author looly
|
* @author looly
|
||||||
* @since 5.0.0
|
* @since 5.0.0
|
||||||
*/
|
*/
|
||||||
public class TemporalAccessorConverter extends AbstractConverter<TemporalAccessor> {
|
public class TemporalAccessorConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Class<?> targetType;
|
|
||||||
/**
|
/**
|
||||||
* 日期格式化
|
* 日期格式化
|
||||||
*/
|
*/
|
||||||
@ -49,20 +48,9 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param targetType 目标类型
|
|
||||||
*/
|
|
||||||
public TemporalAccessorConverter(final Class<?> targetType) {
|
|
||||||
this(targetType, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param targetType 目标类型
|
|
||||||
* @param format 日期格式
|
* @param format 日期格式
|
||||||
*/
|
*/
|
||||||
public TemporalAccessorConverter(final Class<?> targetType, final String format) {
|
public TemporalAccessorConverter(final String format) {
|
||||||
this.targetType = targetType;
|
|
||||||
this.format = format;
|
this.format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,26 +72,20 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
this.format = format;
|
this.format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public Class<TemporalAccessor> getTargetType() {
|
protected TemporalAccessor convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return (Class<TemporalAccessor>) this.targetType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected TemporalAccessor convertInternal(final Object value) {
|
|
||||||
if (value instanceof Long) {
|
if (value instanceof Long) {
|
||||||
return parseFromLong((Long) value);
|
return parseFromLong(targetClass, (Long) value);
|
||||||
} else if (value instanceof TemporalAccessor) {
|
} else if (value instanceof TemporalAccessor) {
|
||||||
return parseFromTemporalAccessor((TemporalAccessor) value);
|
return parseFromTemporalAccessor(targetClass, (TemporalAccessor) value);
|
||||||
} else if (value instanceof Date) {
|
} else if (value instanceof Date) {
|
||||||
final DateTime dateTime = DateUtil.date((Date) value);
|
final DateTime dateTime = DateUtil.date((Date) value);
|
||||||
return parseFromInstant(dateTime.toInstant(), dateTime.getZoneId());
|
return parseFromInstant(targetClass, dateTime.toInstant(), dateTime.getZoneId());
|
||||||
} else if (value instanceof Calendar) {
|
} else if (value instanceof Calendar) {
|
||||||
final Calendar calendar = (Calendar) value;
|
final Calendar calendar = (Calendar) value;
|
||||||
return parseFromInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
|
return parseFromInstant(targetClass, calendar.toInstant(), calendar.getTimeZone().toZoneId());
|
||||||
} else {
|
} else {
|
||||||
return parseFromCharSequence(convertToStr(value));
|
return parseFromCharSequence(targetClass, convertToStr(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +95,7 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
* @param value 字符串值
|
* @param value 字符串值
|
||||||
* @return 日期对象
|
* @return 日期对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromCharSequence(final CharSequence value) {
|
private TemporalAccessor parseFromCharSequence(final Class<?> targetClass, final CharSequence value) {
|
||||||
if (StrUtil.isBlank(value)) {
|
if (StrUtil.isBlank(value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -129,17 +111,18 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
instant = Objects.requireNonNull(dateTime).toInstant();
|
instant = Objects.requireNonNull(dateTime).toInstant();
|
||||||
zoneId = dateTime.getZoneId();
|
zoneId = dateTime.getZoneId();
|
||||||
}
|
}
|
||||||
return parseFromInstant(instant, zoneId);
|
return parseFromInstant(targetClass, instant, zoneId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将Long型时间戳转换为java.time中的对象
|
* 将Long型时间戳转换为java.time中的对象
|
||||||
*
|
*
|
||||||
|
* @param targetClass 目标类型
|
||||||
* @param time 时间戳
|
* @param time 时间戳
|
||||||
* @return java.time中的对象
|
* @return java.time中的对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromLong(final Long time) {
|
private TemporalAccessor parseFromLong(final Class<?> targetClass, final Long time) {
|
||||||
return parseFromInstant(Instant.ofEpochMilli(time), null);
|
return parseFromInstant(targetClass, Instant.ofEpochMilli(time), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,16 +131,16 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
* @param temporalAccessor TemporalAccessor对象
|
* @param temporalAccessor TemporalAccessor对象
|
||||||
* @return java.time中的对象
|
* @return java.time中的对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromTemporalAccessor(final TemporalAccessor temporalAccessor) {
|
private TemporalAccessor parseFromTemporalAccessor(final Class<?> targetClass, final TemporalAccessor temporalAccessor) {
|
||||||
TemporalAccessor result = null;
|
TemporalAccessor result = null;
|
||||||
if (temporalAccessor instanceof LocalDateTime) {
|
if (temporalAccessor instanceof LocalDateTime) {
|
||||||
result = parseFromLocalDateTime((LocalDateTime) temporalAccessor);
|
result = parseFromLocalDateTime(targetClass, (LocalDateTime) temporalAccessor);
|
||||||
} else if (temporalAccessor instanceof ZonedDateTime) {
|
} else if (temporalAccessor instanceof ZonedDateTime) {
|
||||||
result = parseFromZonedDateTime((ZonedDateTime) temporalAccessor);
|
result = parseFromZonedDateTime(targetClass, (ZonedDateTime) temporalAccessor);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null == result) {
|
if (null == result) {
|
||||||
result = parseFromInstant(DateUtil.toInstant(temporalAccessor), null);
|
result = parseFromInstant(targetClass, DateUtil.toInstant(temporalAccessor), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -166,26 +149,27 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
/**
|
/**
|
||||||
* 将TemporalAccessor型时间戳转换为java.time中的对象
|
* 将TemporalAccessor型时间戳转换为java.time中的对象
|
||||||
*
|
*
|
||||||
|
* @param targetClass 目标类
|
||||||
* @param localDateTime {@link LocalDateTime}对象
|
* @param localDateTime {@link LocalDateTime}对象
|
||||||
* @return java.time中的对象
|
* @return java.time中的对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromLocalDateTime(final LocalDateTime localDateTime) {
|
private TemporalAccessor parseFromLocalDateTime(final Class<?> targetClass, final LocalDateTime localDateTime) {
|
||||||
if (Instant.class.equals(this.targetType)) {
|
if (Instant.class.equals(targetClass)) {
|
||||||
return DateUtil.toInstant(localDateTime);
|
return DateUtil.toInstant(localDateTime);
|
||||||
}
|
}
|
||||||
if (LocalDate.class.equals(this.targetType)) {
|
if (LocalDate.class.equals(targetClass)) {
|
||||||
return localDateTime.toLocalDate();
|
return localDateTime.toLocalDate();
|
||||||
}
|
}
|
||||||
if (LocalTime.class.equals(this.targetType)) {
|
if (LocalTime.class.equals(targetClass)) {
|
||||||
return localDateTime.toLocalTime();
|
return localDateTime.toLocalTime();
|
||||||
}
|
}
|
||||||
if (ZonedDateTime.class.equals(this.targetType)) {
|
if (ZonedDateTime.class.equals(targetClass)) {
|
||||||
return localDateTime.atZone(ZoneId.systemDefault());
|
return localDateTime.atZone(ZoneId.systemDefault());
|
||||||
}
|
}
|
||||||
if (OffsetDateTime.class.equals(this.targetType)) {
|
if (OffsetDateTime.class.equals(targetClass)) {
|
||||||
return localDateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime();
|
return localDateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime();
|
||||||
}
|
}
|
||||||
if (OffsetTime.class.equals(this.targetType)) {
|
if (OffsetTime.class.equals(targetClass)) {
|
||||||
return localDateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime().toOffsetTime();
|
return localDateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime().toOffsetTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,23 +182,23 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
* @param zonedDateTime {@link ZonedDateTime}对象
|
* @param zonedDateTime {@link ZonedDateTime}对象
|
||||||
* @return java.time中的对象
|
* @return java.time中的对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromZonedDateTime(final ZonedDateTime zonedDateTime) {
|
private TemporalAccessor parseFromZonedDateTime(final Class<?> targetClass, final ZonedDateTime zonedDateTime) {
|
||||||
if (Instant.class.equals(this.targetType)) {
|
if (Instant.class.equals(targetClass)) {
|
||||||
return DateUtil.toInstant(zonedDateTime);
|
return DateUtil.toInstant(zonedDateTime);
|
||||||
}
|
}
|
||||||
if (LocalDateTime.class.equals(this.targetType)) {
|
if (LocalDateTime.class.equals(targetClass)) {
|
||||||
return zonedDateTime.toLocalDateTime();
|
return zonedDateTime.toLocalDateTime();
|
||||||
}
|
}
|
||||||
if (LocalDate.class.equals(this.targetType)) {
|
if (LocalDate.class.equals(targetClass)) {
|
||||||
return zonedDateTime.toLocalDate();
|
return zonedDateTime.toLocalDate();
|
||||||
}
|
}
|
||||||
if (LocalTime.class.equals(this.targetType)) {
|
if (LocalTime.class.equals(targetClass)) {
|
||||||
return zonedDateTime.toLocalTime();
|
return zonedDateTime.toLocalTime();
|
||||||
}
|
}
|
||||||
if (OffsetDateTime.class.equals(this.targetType)) {
|
if (OffsetDateTime.class.equals(targetClass)) {
|
||||||
return zonedDateTime.toOffsetDateTime();
|
return zonedDateTime.toOffsetDateTime();
|
||||||
}
|
}
|
||||||
if (OffsetTime.class.equals(this.targetType)) {
|
if (OffsetTime.class.equals(targetClass)) {
|
||||||
return zonedDateTime.toOffsetDateTime().toOffsetTime();
|
return zonedDateTime.toOffsetDateTime().toOffsetTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,25 +212,25 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
* @param zoneId 时区ID,null表示当前系统默认的时区
|
* @param zoneId 时区ID,null表示当前系统默认的时区
|
||||||
* @return java.time中的对象
|
* @return java.time中的对象
|
||||||
*/
|
*/
|
||||||
private TemporalAccessor parseFromInstant(final Instant instant, ZoneId zoneId) {
|
private TemporalAccessor parseFromInstant(final Class<?> targetClass, final Instant instant, ZoneId zoneId) {
|
||||||
if (Instant.class.equals(this.targetType)) {
|
if (Instant.class.equals(targetClass)) {
|
||||||
return instant;
|
return instant;
|
||||||
}
|
}
|
||||||
|
|
||||||
zoneId = ObjUtil.defaultIfNull(zoneId, ZoneId::systemDefault);
|
zoneId = ObjUtil.defaultIfNull(zoneId, ZoneId::systemDefault);
|
||||||
|
|
||||||
TemporalAccessor result = null;
|
TemporalAccessor result = null;
|
||||||
if (LocalDateTime.class.equals(this.targetType)) {
|
if (LocalDateTime.class.equals(targetClass)) {
|
||||||
result = LocalDateTime.ofInstant(instant, zoneId);
|
result = LocalDateTime.ofInstant(instant, zoneId);
|
||||||
} else if (LocalDate.class.equals(this.targetType)) {
|
} else if (LocalDate.class.equals(targetClass)) {
|
||||||
result = instant.atZone(zoneId).toLocalDate();
|
result = instant.atZone(zoneId).toLocalDate();
|
||||||
} else if (LocalTime.class.equals(this.targetType)) {
|
} else if (LocalTime.class.equals(targetClass)) {
|
||||||
result = instant.atZone(zoneId).toLocalTime();
|
result = instant.atZone(zoneId).toLocalTime();
|
||||||
} else if (ZonedDateTime.class.equals(this.targetType)) {
|
} else if (ZonedDateTime.class.equals(targetClass)) {
|
||||||
result = instant.atZone(zoneId);
|
result = instant.atZone(zoneId);
|
||||||
} else if (OffsetDateTime.class.equals(this.targetType)) {
|
} else if (OffsetDateTime.class.equals(targetClass)) {
|
||||||
result = OffsetDateTime.ofInstant(instant, zoneId);
|
result = OffsetDateTime.ofInstant(instant, zoneId);
|
||||||
} else if (OffsetTime.class.equals(this.targetType)) {
|
} else if (OffsetTime.class.equals(targetClass)) {
|
||||||
result = OffsetTime.ofInstant(instant, zoneId);
|
result = OffsetTime.ofInstant(instant, zoneId);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.TimeZone;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TimeZone转换器
|
* TimeZone转换器
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TimeZoneConverter extends AbstractConverter<TimeZone>{
|
public class TimeZoneConverter extends AbstractConverter{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TimeZone convertInternal(final Object value) {
|
protected TimeZone convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return TimeZone.getTimeZone(convertToStr(value));
|
return TimeZone.getTimeZone(convertToStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* URI对象转换器
|
* URI对象转换器
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class URIConverter extends AbstractConverter<URI>{
|
public class URIConverter extends AbstractConverter{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected URI convertInternal(final Object value) {
|
protected URI convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
try {
|
try {
|
||||||
if(value instanceof File){
|
if(value instanceof File){
|
||||||
return ((File)value).toURI();
|
return ((File)value).toURI();
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* URL对象转换器
|
* URL对象转换器
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class URLConverter extends AbstractConverter<URL>{
|
public class URLConverter extends AbstractConverter{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected URL convertInternal(final Object value) {
|
protected URL convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
try {
|
try {
|
||||||
if(value instanceof File){
|
if(value instanceof File){
|
||||||
return ((File)value).toURI().toURL();
|
return ((File)value).toURI().toURL();
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UUID对象转换器转换器
|
* UUID对象转换器转换器
|
||||||
*
|
*
|
||||||
@ -11,11 +11,13 @@ import cn.hutool.core.convert.AbstractConverter;
|
|||||||
* @since 4.0.10
|
* @since 4.0.10
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class UUIDConverter extends AbstractConverter<UUID> {
|
public class UUIDConverter extends AbstractConverter {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected UUID convertInternal(final Object value) {
|
protected UUID convertInternal(final Class<?> targetClass, final Object value) {
|
||||||
return UUID.fromString(convertToStr(value));
|
return UUID.fromString(convertToStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ package cn.hutool.core.lang.ansi;
|
|||||||
* 生成ANSI格式的编码输出
|
* 生成ANSI格式的编码输出
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
*/
|
||||||
public abstract class AnsiEncoder {
|
public abstract class AnsiEncoder {
|
||||||
|
|
||||||
|
@ -41,7 +41,9 @@ public class ReflectUtil {
|
|||||||
* <li>{@code ReflectUtil.getDescriptor(Object.class.getMethod("equals", Object.class)) // "(Ljava/lang/Object;)Z"}</li>
|
* <li>{@code ReflectUtil.getDescriptor(Object.class.getMethod("equals", Object.class)) // "(Ljava/lang/Object;)Z"}</li>
|
||||||
* <li>{@code ReflectUtil.getDescriptor(ReflectUtil.class.getDeclaredMethod("appendDescriptor", Class.clas, StringBuilder.class)) // "(Ljava/lang/Class;Ljava/lang/StringBuilder;)V"}</li>
|
* <li>{@code ReflectUtil.getDescriptor(ReflectUtil.class.getDeclaredMethod("appendDescriptor", Class.clas, StringBuilder.class)) // "(Ljava/lang/Class;Ljava/lang/StringBuilder;)V"}</li>
|
||||||
* <li>{@code ReflectUtil.getDescriptor(ArrayUtil.class.getMethod("isEmpty", Object[].class)) // "([Ljava/lang/Object;)Z"}</li>
|
* <li>{@code ReflectUtil.getDescriptor(ArrayUtil.class.getMethod("isEmpty", Object[].class)) // "([Ljava/lang/Object;)Z"}</li>
|
||||||
* </ul>Object.class.getMethod("hashCode")
|
* </ul>
|
||||||
|
*
|
||||||
|
* Object.class.getMethod("hashCode")
|
||||||
*/
|
*/
|
||||||
public static String getDescriptor(Executable executable) {
|
public static String getDescriptor(Executable executable) {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
@ -75,7 +77,7 @@ public class ReflectUtil {
|
|||||||
currentClass = currentClass.getComponentType()) {
|
currentClass = currentClass.getComponentType()) {
|
||||||
stringBuilder.append('[');
|
stringBuilder.append('[');
|
||||||
}
|
}
|
||||||
if (ClassUtil.isBasicType(currentClass)) {
|
if (currentClass.isPrimitive()) {
|
||||||
stringBuilder.append(getDescriptorChar(currentClass));
|
stringBuilder.append(getDescriptorChar(currentClass));
|
||||||
} else {
|
} else {
|
||||||
stringBuilder.append('L').append(currentClass.getName().replace('.', '/')).append(';');
|
stringBuilder.append('L').append(currentClass.getName().replace('.', '/')).append(';');
|
||||||
|
@ -16,6 +16,7 @@ import java.util.function.Function;
|
|||||||
* @since 5.8.0
|
* @since 5.8.0
|
||||||
*/
|
*/
|
||||||
public class ConsoleColorLog extends ConsoleLog {
|
public class ConsoleColorLog extends ConsoleLog {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 控制台打印类名的颜色代码
|
* 控制台打印类名的颜色代码
|
||||||
|
Loading…
x
Reference in New Issue
Block a user