mirror of
https://gitee.com/dromara/hutool.git
synced 2025-11-24 08:33:22 +08:00
Merge pull request #4138 from sunshineflymeat/hutool-1120
Fix issue 4137
This commit is contained in:
@@ -34,6 +34,12 @@ public class TypeUtil {
|
|||||||
return (Class<?>) type;
|
return (Class<?>) type;
|
||||||
} else if (type instanceof ParameterizedType) {
|
} else if (type instanceof ParameterizedType) {
|
||||||
return (Class<?>) ((ParameterizedType) type).getRawType();
|
return (Class<?>) ((ParameterizedType) type).getRawType();
|
||||||
|
} else if (type instanceof GenericArrayType) {
|
||||||
|
final Type componentType = ((GenericArrayType) type).getGenericComponentType();
|
||||||
|
final Class<?> componentClass = getClass(componentType);
|
||||||
|
if (componentClass != null) {
|
||||||
|
return Array.newInstance(componentClass, 0).getClass();
|
||||||
|
}
|
||||||
} else if (type instanceof TypeVariable) {
|
} else if (type instanceof TypeVariable) {
|
||||||
Type[] bounds = ((TypeVariable<?>) type).getBounds();
|
Type[] bounds = ((TypeVariable<?>) type).getBounds();
|
||||||
if (bounds.length == 1) {
|
if (bounds.length == 1) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -47,6 +49,37 @@ public class TypeUtilTest {
|
|||||||
assertEquals(Object.class, clazz);
|
assertEquals(Object.class, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试getClass方法对泛型数组类型T[]的处理
|
||||||
|
* 验证未绑定泛型参数的数组类型会被正确解析为Object[]
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getClassForGenericArrayTypeTest() throws NoSuchFieldException {
|
||||||
|
// 获取T[]类型字段的泛型类型
|
||||||
|
Field levelField = GenericArray.class.getDeclaredField("level");
|
||||||
|
Type genericArrayType = levelField.getGenericType();
|
||||||
|
// 调用getClass方法处理GenericArrayType
|
||||||
|
Class<?> clazz = TypeUtil.getClass(genericArrayType);
|
||||||
|
// 验证返回Object[]类型
|
||||||
|
assertNotNull(clazz, "getClass方法返回null");
|
||||||
|
assertTrue(clazz.isArray(), "返回类型不是数组");
|
||||||
|
assertEquals(Object.class, clazz.getComponentType(), "数组组件类型应为Object");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试getClass方法对参数化类型数组List<String>[]的处理
|
||||||
|
* 验证数组组件类型能正确解析为原始类型
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getClassForParameterizedArrayTypeTest() {
|
||||||
|
// 创建List<String>[]类型引用
|
||||||
|
Type genericArrayType = new TypeReference<List<String>[]>() {}.getType();
|
||||||
|
// 调用getClass方法处理GenericArrayType
|
||||||
|
Class<?> clazz = TypeUtil.getClass(genericArrayType);
|
||||||
|
// 验证返回List[]类型
|
||||||
|
assertEquals(Array.newInstance(List.class, 0).getClass(), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestClass {
|
public static class TestClass {
|
||||||
public List<String> getList() {
|
public List<String> getList() {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
|
|||||||
Reference in New Issue
Block a user