修复ClassUtil.getTypeArgument() 获取泛型存在null问题

This commit is contained in:
Looly 2024-03-18 09:53:15 +08:00
parent 03a92830a6
commit bf890d17f1
3 changed files with 42 additions and 4 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.27(2024-03-15)
# 5.8.27(2024-03-18)
### 🐣新特性
* 【extra 】 FreemarkerEngine修改默认版本参数
@ -17,6 +17,7 @@
* 【http 】 修复CVE-2022-22885HttpGlobalConfig可选关闭信任hostissue#2042@Github
* 【core 】 修复DateUtil.betweenYear闰年2月问题issue#I97U3J@Gitee
* 【captcha】 修复Graphics2D的资源没释放问题issue#I98PYN@Gitee
* 【core 】 修复ClassUtil.getTypeArgument() 获取泛型存在null问题issue#3516@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.26(2024-02-10)

View File

@ -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);
}
}
}

View File

@ -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> {
}
}