修复JsonUtil.toBean泛型数组类型丢失问题(pr#3876@Github)

This commit is contained in:
Looly 2025-03-03 12:05:26 +08:00
parent 1c3e8634c8
commit 2046809264
2 changed files with 20 additions and 8 deletions

View File

@ -39,14 +39,14 @@ public class ActualTypeMapperPool {
* @return 泛型对应关系Map * @return 泛型对应关系Map
* @since 5.7.16 * @since 5.7.16
*/ */
public static Map<String, Type> getStrKeyMap(Type type){ public static Map<String, Type> getStrKeyMap(Type type) {
return Convert.toMap(String.class, Type.class, get(type)); return Convert.toMap(String.class, Type.class, get(type));
} }
/** /**
* 获得泛型变量对应的泛型实际类型如果此变量没有对应的实际类型返回null * 获得泛型变量对应的泛型实际类型如果此变量没有对应的实际类型返回null
* *
* @param type * @param type
* @param typeVariable 泛型变量例如T等 * @param typeVariable 泛型变量例如T等
* @return 实际类型可能为Class等 * @return 实际类型可能为Class等
*/ */
@ -59,6 +59,14 @@ public class ActualTypeMapperPool {
return result; return result;
} }
/**
* 获得泛型变量对应的泛型实际类型如果此变量没有对应的实际类型返回null
*
* @param type
* @param genericArrayType 泛型数组类型
* @return 实际类型可能为Class等
* @since 5.8.37
*/
public static Type getActualType(Type type, GenericArrayType genericArrayType) { public static Type getActualType(Type type, GenericArrayType genericArrayType) {
final Map<Type, Type> typeTypeMap = get(type); final Map<Type, Type> typeTypeMap = get(type);
Type actualType = typeTypeMap.get(genericArrayType); Type actualType = typeTypeMap.get(genericArrayType);
@ -79,7 +87,7 @@ public class ActualTypeMapperPool {
* 获取指定泛型变量对应的真实类型<br> * 获取指定泛型变量对应的真实类型<br>
* 由于子类中泛型参数实现和父类接口中泛型定义位置是一一对应的因此可以通过对应关系找到泛型实现类型<br> * 由于子类中泛型参数实现和父类接口中泛型定义位置是一一对应的因此可以通过对应关系找到泛型实现类型<br>
* *
* @param type 真实类型所在类此类中记录了泛型参数对应的实际类型 * @param type 真实类型所在类此类中记录了泛型参数对应的实际类型
* @param typeVariables 泛型变量需要的实际类型对应的泛型参数 * @param typeVariables 泛型变量需要的实际类型对应的泛型参数
* @return 给定泛型参数对应的实际类型如果无对应类型对应位置返回null * @return 给定泛型参数对应的实际类型如果无对应类型对应位置返回null
*/ */
@ -88,8 +96,8 @@ public class ActualTypeMapperPool {
final Type[] result = new Type[typeVariables.length]; final Type[] result = new Type[typeVariables.length];
for (int i = 0; i < typeVariables.length; i++) { for (int i = 0; i < typeVariables.length; i++) {
result[i] = (typeVariables[i] instanceof TypeVariable) result[i] = (typeVariables[i] instanceof TypeVariable)
? getActualType(type, (TypeVariable<?>) typeVariables[i]) ? getActualType(type, (TypeVariable<?>) typeVariables[i])
: typeVariables[i]; : typeVariables[i];
} }
return result; return result;
} }
@ -112,7 +120,7 @@ public class ActualTypeMapperPool {
// 如果传入的非Class例如TypeReference获取到泛型参数中实际的泛型对象类继续按照类处理 // 如果传入的非Class例如TypeReference获取到泛型参数中实际的泛型对象类继续按照类处理
while (null != type) { while (null != type) {
final ParameterizedType parameterizedType = TypeUtil.toParameterizedType(type); final ParameterizedType parameterizedType = TypeUtil.toParameterizedType(type);
if(null == parameterizedType){ if (null == parameterizedType) {
break; break;
} }
final Type[] typeArguments = parameterizedType.getActualTypeArguments(); final Type[] typeArguments = parameterizedType.getActualTypeArguments();
@ -123,7 +131,7 @@ public class ActualTypeMapperPool {
for (int i = 0; i < typeParameters.length; i++) { for (int i = 0; i < typeParameters.length; i++) {
value = typeArguments[i]; value = typeArguments[i];
// 跳过泛型变量对应泛型变量的情况 // 跳过泛型变量对应泛型变量的情况
if(false == value instanceof TypeVariable){ if (false == value instanceof TypeVariable) {
typeMap.put(typeParameters[i], value); typeMap.put(typeParameters[i], value);
} }
} }

View File

@ -408,7 +408,11 @@ public class TypeUtil {
return ActualTypeMapperPool.getActualType(type, (TypeVariable<?>) typeVariable); return ActualTypeMapperPool.getActualType(type, (TypeVariable<?>) typeVariable);
} }
if (typeVariable instanceof GenericArrayType) { if (typeVariable instanceof GenericArrayType) {
return ActualTypeMapperPool.getActualType(type, (GenericArrayType) typeVariable); //return ActualTypeMapperPool.getActualType(type, (GenericArrayType) typeVariable);
final Type actualType = ActualTypeMapperPool.getActualType(type, (GenericArrayType) typeVariable);
if(null != actualType){
return actualType;
}
} }
// 没有需要替换的泛型变量原样输出 // 没有需要替换的泛型变量原样输出