mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复ClassUtil.getTypeArgument() 获取泛型存在null问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.ParameterizedTypeImpl;
|
||||
import cn.hutool.core.lang.reflect.ActualTypeMapperPool;
|
||||
|
||||
@@ -309,7 +310,6 @@ public class TypeUtil {
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 泛型父类或接口数组
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static ParameterizedType[] getGenerics(final Class<?> clazz) {
|
||||
final List<ParameterizedType> result = new ArrayList<>();
|
||||
@@ -326,8 +326,9 @@ public class TypeUtil {
|
||||
final Type[] genericInterfaces = clazz.getGenericInterfaces();
|
||||
if (ArrayUtil.isNotEmpty(genericInterfaces)) {
|
||||
for (final Type genericInterface : genericInterfaces) {
|
||||
if (genericInterface instanceof ParameterizedType) {
|
||||
result.add((ParameterizedType) genericInterface);
|
||||
final ParameterizedType parameterizedType = toParameterizedType(genericInterface);
|
||||
if(null != parameterizedType){
|
||||
result.add(parameterizedType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,36 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Issue3516Test {
|
||||
|
||||
@Test
|
||||
public void getTypeArgumentTest() {
|
||||
final Type typeArgument = TypeUtil.getTypeArgument(Demo.class, 0);
|
||||
Assert.assertEquals(B.class, typeArgument);
|
||||
}
|
||||
|
||||
static class Demo implements A2B{
|
||||
@Override
|
||||
public A apply(B arg0) {
|
||||
final A a = new A();
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
static class A {
|
||||
private String name;
|
||||
}
|
||||
|
||||
static class B {
|
||||
private String name;
|
||||
}
|
||||
|
||||
interface A2B extends Function<B, A> {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user