mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复TypeUtil.getClass方法强转报错问题
This commit is contained in:
parent
3fdfe356bd
commit
f13b09fff6
@ -52,7 +52,11 @@ public class TypeUtil {
|
|||||||
} else if (type instanceof ParameterizedType) {
|
} else if (type instanceof ParameterizedType) {
|
||||||
return (Class<?>) ((ParameterizedType) type).getRawType();
|
return (Class<?>) ((ParameterizedType) type).getRawType();
|
||||||
} else if (type instanceof TypeVariable) {
|
} 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) {
|
} else if (type instanceof WildcardType) {
|
||||||
final Type[] upperBounds = ((WildcardType) type).getUpperBounds();
|
final Type[] upperBounds = ((WildcardType) type).getUpperBounds();
|
||||||
if (upperBounds.length == 1) {
|
if (upperBounds.length == 1) {
|
||||||
|
@ -82,6 +82,19 @@ public class TypeUtilTest {
|
|||||||
Assertions.assertEquals(Long.class, idType);
|
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>{
|
public static class Level3 extends Level2<Level3>{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -95,4 +108,9 @@ public class TypeUtilTest {
|
|||||||
private T id;
|
private T id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Parent<T extends Level1<B>, B extends Long> {
|
||||||
|
private T level;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user