修复ReflectUtil 反射方法中桥接问题

This commit is contained in:
Looly 2022-09-23 11:51:49 +08:00
parent e59548ebbf
commit 8d348b539d
3 changed files with 35 additions and 9 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.8.M1 (2022-09-22) # 5.8.8.M1 (2022-09-23)
### 🐣新特性 ### 🐣新特性
* 【core 】 StreamUtil.of方法新增对 Iterator 支持StreamUtil.of(Iterable) 方法优化pr#807@Gitee * 【core 】 StreamUtil.of方法新增对 Iterator 支持StreamUtil.of(Iterable) 方法优化pr#807@Gitee
@ -17,6 +17,7 @@
* 【core 】 修复murmur3_32实现错误pr#2616@Github * 【core 】 修复murmur3_32实现错误pr#2616@Github
* 【core 】 修复PunyCode处理域名的问题pr#2620@Github * 【core 】 修复PunyCode处理域名的问题pr#2620@Github
* 【core 】 修复ObjectUtil.defaultIfNull去掉误加的deprecatedissue#I5SIZT@Gitee * 【core 】 修复ObjectUtil.defaultIfNull去掉误加的deprecatedissue#I5SIZT@Gitee
* 【core 】 修复ReflectUtil 反射方法中桥接判断问题issue#2625@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -539,18 +539,20 @@ public class ReflectUtil {
return null; return null;
} }
Method res = null;
final Method[] methods = getMethods(clazz); final Method[] methods = getMethods(clazz);
if (ArrayUtil.isNotEmpty(methods)) { if (ArrayUtil.isNotEmpty(methods)) {
for (Method method : methods) { for (Method method : methods) {
if (StrUtil.equals(methodName, method.getName(), ignoreCase) if (StrUtil.equals(methodName, method.getName(), ignoreCase)
&& ClassUtil.isAllAssignableFrom(method.getParameterTypes(), paramTypes) && ClassUtil.isAllAssignableFrom(method.getParameterTypes(), paramTypes)
//排除桥接方法pr#1965@Github //排除协变桥接方法pr#1965@Github
&& false == method.isBridge()) { && (res == null
return method; || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
res = method;
} }
} }
} }
return null; return res;
} }
/** /**
@ -606,17 +608,19 @@ public class ReflectUtil {
return null; return null;
} }
Method res = null;
final Method[] methods = getMethods(clazz); final Method[] methods = getMethods(clazz);
if (ArrayUtil.isNotEmpty(methods)) { if (ArrayUtil.isNotEmpty(methods)) {
for (Method method : methods) { for (Method method : methods) {
if (StrUtil.equals(methodName, method.getName(), ignoreCase) if (StrUtil.equals(methodName, method.getName(), ignoreCase)
// 排除桥接方法 //排除协变桥接方法pr#1965@Github
&& false == method.isBridge()) { && (res == null
return method; || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
res = method;
} }
} }
} }
return null; return res;
} }
/** /**

View File

@ -292,4 +292,25 @@ public class ReflectUtilTest {
Assert.assertEquals(dialects, ReflectUtil.getFieldValue(JdbcDialects.class, fieldName)); Assert.assertEquals(dialects, ReflectUtil.getFieldValue(JdbcDialects.class, fieldName));
} }
@Test
public void issue2625Test(){
// 内部类继承的情况下父类方法会被定义为桥接方法因此按照pr#1965@Github判断返回值的继承关系来代替判断桥接
final Method getThis = ReflectUtil.getMethod(A.C.class, "getThis");
Assert.assertTrue(getThis.isBridge());
}
@SuppressWarnings("InnerClassMayBeStatic")
public class A{
public class C extends B{
}
protected class B{
public B getThis(){
return this;
}
}
}
} }