修复Convert#toMap默认转成HashMap的问题

This commit is contained in:
Looly 2022-07-29 22:13:46 +08:00
parent d4d8522cd4
commit 52dec0c301
2 changed files with 93 additions and 89 deletions

View File

@ -43,6 +43,7 @@
* 【core 】 修复UrlBuilder无法配置末尾追加“/”问题issue#2459@Github * 【core 】 修复UrlBuilder无法配置末尾追加“/”问题issue#2459@Github
* 【core 】 修复SystemPropsUtil.getBoolean方法应该只有值为true时才返回true其他情况都应该返回falsepr#717@Gitee * 【core 】 修复SystemPropsUtil.getBoolean方法应该只有值为true时才返回true其他情况都应该返回falsepr#717@Gitee
* 【core 】 修复isBase64判断不准确的问题pr#727@Gitee * 【core 】 修复isBase64判断不准确的问题pr#727@Gitee
* 【core 】 修复Convert#toMap默认转成HashMap的问题pr#729@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -33,7 +33,6 @@ import java.util.concurrent.TimeUnit;
* 类型转换器 * 类型转换器
* *
* @author xiaoleilu * @author xiaoleilu
*
*/ */
public class Convert { public class Convert {
@ -614,7 +613,7 @@ public class Convert {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value) { public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value) {
if (value instanceof Map) { if (value instanceof Map) {
return toMap((Class<? extends Map>) value.getClass(), keyType, valueType, value); return toMap((Class<? extends Map<?, ?>>) value.getClass(), keyType, valueType, value);
} else { } else {
return toMap(HashMap.class, keyType, valueType, value); return toMap(HashMap.class, keyType, valueType, value);
} }
@ -631,7 +630,7 @@ public class Convert {
* @param value 被转换的值 * @param value 被转换的值
* @return {@link Map} * @return {@link Map}
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "rawtypes"})
public static <K, V> Map<K, V> toMap(Class<? extends Map> mapType, Class<K> keyType, Class<V> valueType, Object value) { public static <K, V> Map<K, V> toMap(Class<? extends Map> mapType, Class<K> keyType, Class<V> valueType, Object value) {
return (Map<K, V>) new MapConverter(mapType, keyType, valueType).convert(value, null); return (Map<K, V>) new MapConverter(mapType, keyType, valueType).convert(value, null);
} }
@ -643,10 +642,10 @@ public class Convert {
* @param className 类的字符串表示 * @param className 类的字符串表示
* @param value * @param value
* @return 转换后的值 * @return 转换后的值
* @since 4.0.7
* @throws ConvertException 转换器不存在 * @throws ConvertException 转换器不存在
* @since 4.0.7
*/ */
public static <T> T convertByClassName(String className, Object value) throws ConvertException{ public static <T> T convertByClassName(String className, Object value) throws ConvertException {
return convert(ClassUtil.loadClass(className), value); return convert(ClassUtil.loadClass(className), value);
} }
@ -657,11 +656,11 @@ public class Convert {
* @param type 类型 * @param type 类型
* @param value * @param value
* @return 转换后的值 * @return 转换后的值
* @since 4.0.0
* @throws ConvertException 转换器不存在 * @throws ConvertException 转换器不存在
* @since 4.0.0
*/ */
public static <T> T convert(Class<T> type, Object value) throws ConvertException{ public static <T> T convert(Class<T> type, Object value) throws ConvertException {
return convert((Type)type, value); return convert((Type) type, value);
} }
/** /**
@ -673,7 +672,7 @@ public class Convert {
* @return 转换后的值 * @return 转换后的值
* @throws ConvertException 转换器不存在 * @throws ConvertException 转换器不存在
*/ */
public static <T> T convert(TypeReference<T> reference, Object value) throws ConvertException{ public static <T> T convert(TypeReference<T> reference, Object value) throws ConvertException {
return convert(reference.getType(), value, null); return convert(reference.getType(), value, null);
} }
@ -686,7 +685,7 @@ public class Convert {
* @return 转换后的值 * @return 转换后的值
* @throws ConvertException 转换器不存在 * @throws ConvertException 转换器不存在
*/ */
public static <T> T convert(Type type, Object value) throws ConvertException{ public static <T> T convert(Type type, Object value) throws ConvertException {
return convert(type, value, null); return convert(type, value, null);
} }
@ -702,7 +701,7 @@ public class Convert {
* @since 4.0.0 * @since 4.0.0
*/ */
public static <T> T convert(Class<T> type, Object value, T defaultValue) throws ConvertException { public static <T> T convert(Class<T> type, Object value, T defaultValue) throws ConvertException {
return convert((Type)type, value, defaultValue); return convert((Type) type, value, defaultValue);
} }
/** /**
@ -765,7 +764,7 @@ public class Convert {
try { try {
return registry.convert(type, value, defaultValue); return registry.convert(type, value, defaultValue);
} catch (Exception e) { } catch (Exception e) {
if(quietly){ if (quietly) {
return defaultValue; return defaultValue;
} }
throw e; throw e;
@ -773,6 +772,7 @@ public class Convert {
} }
// ----------------------------------------------------------------------- 全角半角转换 // ----------------------------------------------------------------------- 全角半角转换
/** /**
* 半角转全角{@code null}返回{@code null} * 半角转全角{@code null}返回{@code null}
* *
@ -791,7 +791,7 @@ public class Convert {
* @return 全角字符串{@code null}返回{@code null} * @return 全角字符串{@code null}返回{@code null}
*/ */
public static String toSBC(String input, Set<Character> notConvertSet) { public static String toSBC(String input, Set<Character> notConvertSet) {
if(StrUtil.isEmpty(input)){ if (StrUtil.isEmpty(input)) {
return input; return input;
} }
final char[] c = input.toCharArray(); final char[] c = input.toCharArray();
@ -828,7 +828,7 @@ public class Convert {
* @return 替换后的字符 * @return 替换后的字符
*/ */
public static String toDBC(String text, Set<Character> notConvertSet) { public static String toDBC(String text, Set<Character> notConvertSet) {
if(StrUtil.isBlank(text)) { if (StrUtil.isBlank(text)) {
return text; return text;
} }
final char[] c = text.toCharArray(); final char[] c = text.toCharArray();
@ -850,6 +850,7 @@ public class Convert {
} }
// --------------------------------------------------------------------- hex // --------------------------------------------------------------------- hex
/** /**
* 字符串转换成十六进制字符串结果为小写 * 字符串转换成十六进制字符串结果为小写
* *
@ -952,13 +953,14 @@ public class Convert {
} }
// --------------------------------------------------------------- 原始包装类型转换 // --------------------------------------------------------------- 原始包装类型转换
/** /**
* 原始类转为包装类非原始类返回原类 * 原始类转为包装类非原始类返回原类
* *
* @see BasicType#wrap(Class)
* @param clazz 原始类 * @param clazz 原始类
* @return 包装类 * @return 包装类
* @see BasicType#wrap(Class) * @see BasicType#wrap(Class)
* @see BasicType#wrap(Class)
*/ */
public static Class<?> wrap(Class<?> clazz) { public static Class<?> wrap(Class<?> clazz) {
return BasicType.wrap(clazz); return BasicType.wrap(clazz);
@ -967,16 +969,17 @@ public class Convert {
/** /**
* 包装类转为原始类非包装类返回原类 * 包装类转为原始类非包装类返回原类
* *
* @see BasicType#unWrap(Class)
* @param clazz 包装类 * @param clazz 包装类
* @return 原始类 * @return 原始类
* @see BasicType#unWrap(Class) * @see BasicType#unWrap(Class)
* @see BasicType#unWrap(Class)
*/ */
public static Class<?> unWrap(Class<?> clazz) { public static Class<?> unWrap(Class<?> clazz) {
return BasicType.unWrap(clazz); return BasicType.unWrap(clazz);
} }
// -------------------------------------------------------------------------- 数字和英文转换 // -------------------------------------------------------------------------- 数字和英文转换
/** /**
* 将阿拉伯数字转为英文表达方式 * 将阿拉伯数字转为英文表达方式
* *
@ -1026,7 +1029,7 @@ public class Convert {
* @return 数字 * @return 数字
* @since 5.6.0 * @since 5.6.0
*/ */
public static int chineseToNumber(String number){ public static int chineseToNumber(String number) {
return NumberChineseFormatter.chineseToNumber(number); return NumberChineseFormatter.chineseToNumber(number);
} }
@ -1038,15 +1041,14 @@ public class Convert {
* @since 3.2.3 * @since 3.2.3
*/ */
public static String digitToChinese(Number n) { public static String digitToChinese(Number n) {
if(null == n) { if (null == n) {
return ""; return "";
} }
return NumberChineseFormatter.format(n.doubleValue(), true, true); return NumberChineseFormatter.format(n.doubleValue(), true, true);
} }
/** /**
* 中文大写数字金额转换为数字返回结果以元为单位的BigDecimal类型数字 * 中文大写数字金额转换为数字返回结果以元为单位的BigDecimal类型数字<br>
*
* *
* 陆万柒仟伍佰伍拾陆元叁角贰分返回67556.32 * 陆万柒仟伍佰伍拾陆元叁角贰分返回67556.32
* 叁角贰分返回0.32 * 叁角贰分返回0.32
@ -1055,11 +1057,12 @@ public class Convert {
* @return 返回结果以元为单位的BigDecimal类型数字 * @return 返回结果以元为单位的BigDecimal类型数字
* @since 5.8.5 * @since 5.8.5
*/ */
public static BigDecimal chineseMoneyToNumber(String chineseMoneyAmount){ public static BigDecimal chineseMoneyToNumber(String chineseMoneyAmount) {
return NumberChineseFormatter.chineseMoneyToNumber(chineseMoneyAmount); return NumberChineseFormatter.chineseMoneyToNumber(chineseMoneyAmount);
} }
// -------------------------------------------------------------------------- 数字转换 // -------------------------------------------------------------------------- 数字转换
/** /**
* int转byte * int转byte
* *