修复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

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