修复TypeUtil.getClass方法强转报错问题

This commit is contained in:
Looly 2023-10-12 21:29:03 +08:00
parent 3fdfe356bd
commit f13b09fff6
2 changed files with 23 additions and 1 deletions

View File

@ -52,7 +52,11 @@ public class TypeUtil {
} else if (type instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) type).getRawType();
} else if (type instanceof TypeVariable) {
return (Class<?>) ((TypeVariable<?>) type).getBounds()[0];
//return (Class<?>) ((TypeVariable<?>) type).getBounds()[0];
final Type[] bounds = ((TypeVariable<?>) type).getBounds();
if (bounds.length == 1) {
return getClass(bounds[0]);
}
} else if (type instanceof WildcardType) {
final Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (upperBounds.length == 1) {

View File

@ -82,6 +82,19 @@ public class TypeUtilTest {
Assertions.assertEquals(Long.class, idType);
}
@Test
public void getClasses() {
Method method = MethodUtil.getMethod(Parent.class, "getLevel");
Type returnType = TypeUtil.getReturnType(method);
Class<?> clazz = TypeUtil.getClass(returnType);
Assertions.assertEquals(Level1.class, clazz);
method = MethodUtil.getMethod(Level1.class, "getId");
returnType = TypeUtil.getReturnType(method);
clazz = TypeUtil.getClass(returnType);
Assertions.assertEquals(Object.class, clazz);
}
public static class Level3 extends Level2<Level3>{
}
@ -95,4 +108,9 @@ public class TypeUtilTest {
private T id;
}
@Data
public static class Parent<T extends Level1<B>, B extends Long> {
private T level;
}
}