This commit is contained in:
Looly
2022-09-23 12:12:06 +08:00
parent 6c6b339520
commit 50cb9c6168
3 changed files with 37 additions and 7 deletions

View File

@@ -198,18 +198,20 @@ public class MethodUtil {
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 (final Method method : methods) { for (final 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()) { //排除协变桥接方法pr#1965@Github
return method; && (res == null || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
res = method;
} }
} }
} }
return null; return res;
} }
/** /**
@@ -265,17 +267,18 @@ public class MethodUtil {
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 (final Method method : methods) { for (final 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 || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
return method; res = method;
} }
} }
} }
return null; return res;
} }
/** /**

View File

@@ -115,6 +115,7 @@ public class MethodUtilTest {
Console.log(timer.getLastTaskTimeMillis()); Console.log(timer.getLastTaskTimeMillis());
} }
@SuppressWarnings("UnusedReturnValue")
public static Method getMethodWithReturnTypeCheck(final Class<?> clazz, final boolean ignoreCase, final String methodName, final Class<?>... paramTypes) throws SecurityException { public static Method getMethodWithReturnTypeCheck(final Class<?> clazz, final boolean ignoreCase, final String methodName, final Class<?>... paramTypes) throws SecurityException {
if (null == clazz || StrUtil.isBlank(methodName)) { if (null == clazz || StrUtil.isBlank(methodName)) {
return null; return null;
@@ -193,6 +194,26 @@ public class MethodUtilTest {
Assert.assertNotNull(publicSubMethod); Assert.assertNotNull(publicSubMethod);
final Method privateSubMethod = MethodUtil.getMethod(ReflectUtilTest.TestSubClass.class, "privateSubMethod"); final Method privateSubMethod = MethodUtil.getMethod(ReflectUtilTest.TestSubClass.class, "privateSubMethod");
Assert.assertNotNull(privateSubMethod); Assert.assertNotNull(privateSubMethod);
}
@Test
public void issue2625Test(){
// 内部类继承的情况下父类方法会被定义为桥接方法因此按照pr#1965@Github判断返回值的继承关系来代替判断桥接。
final Method getThis = MethodUtil.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;
}
}
} }
} }

View File

@@ -695,4 +695,10 @@ public class StrUtilTest {
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2000")); Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2000"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2001")); Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2001"));
} }
@Test
public void issue2628Test(){
final String s = StrUtil.indexedFormat("a{0,number,#}", 1234567);
Assert.assertEquals("a1234567", s);
}
} }