Use Method::getParameterCount where possible

This commit is contained in:
stsypanov
2019-11-07 17:52:01 +02:00
committed by Juergen Hoeller
parent 55f3f128c9
commit f5ae3c77c6
14 changed files with 68 additions and 46 deletions

View File

@@ -149,10 +149,10 @@ public final class BridgeMethodResolver {
*/
private static boolean isResolvedTypeMatch(Method genericMethod, Method candidateMethod, Class<?> declaringClass) {
Type[] genericParameters = genericMethod.getGenericParameterTypes();
Class<?>[] candidateParameters = candidateMethod.getParameterTypes();
if (genericParameters.length != candidateParameters.length) {
if (genericParameters.length != candidateMethod.getParameterCount()) {
return false;
}
Class<?>[] candidateParameters = candidateMethod.getParameterTypes();
for (int i = 0; i < candidateParameters.length; i++) {
ResolvableType genericParameter = ResolvableType.forMethodParameter(genericMethod, i, declaringClass);
Class<?> candidateParameter = candidateParameters[i];
@@ -235,6 +235,7 @@ public final class BridgeMethodResolver {
return true;
}
return (bridgeMethod.getReturnType().equals(bridgedMethod.getReturnType()) &&
bridgeMethod.getParameterCount() == bridgedMethod.getParameterCount() &&
Arrays.equals(bridgeMethod.getParameterTypes(), bridgedMethod.getParameterTypes()));
}

View File

@@ -225,8 +225,8 @@ public class MethodInvoker {
for (Method candidate : candidates) {
if (candidate.getName().equals(targetMethod)) {
Class<?>[] paramTypes = candidate.getParameterTypes();
if (paramTypes.length == argCount) {
if (candidate.getParameterCount() == argCount) {
Class<?>[] paramTypes = candidate.getParameterTypes();
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
if (typeDiffWeight < minTypeDiffWeight) {
minTypeDiffWeight = typeDiffWeight;

View File

@@ -240,7 +240,7 @@ public abstract class ReflectionUtils {
getDeclaredMethods(searchType, false);
for (Method method : methods) {
if (name.equals(method.getName()) &&
(paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
(paramTypes == null || hasSameParams(method, paramTypes))) {
return method;
}
}
@@ -249,6 +249,13 @@ public abstract class ReflectionUtils {
return null;
}
private static boolean hasSameParams(Method method, @Nullable Class<?>[] paramTypes) {
if (paramTypes.length != method.getParameterCount()) {
return false;
}
return Arrays.equals(paramTypes, method.getParameterTypes());
}
/**
* Invoke the specified {@link Method} against the supplied target object with no arguments.
* The target object can be {@code null} when invoking a static {@link Method}.
@@ -413,6 +420,7 @@ public abstract class ReflectionUtils {
Method methodBeingOverriddenWithCovariantReturnType = null;
for (Method existingMethod : methods) {
if (method.getName().equals(existingMethod.getName()) &&
method.getParameterCount() == existingMethod.getParameterCount() &&
Arrays.equals(method.getParameterTypes(), existingMethod.getParameterTypes())) {
// Is this a covariant return type situation?
if (existingMethod.getReturnType() != method.getReturnType() &&
@@ -504,8 +512,10 @@ public abstract class ReflectionUtils {
if (method == null || !method.getName().equals("equals")) {
return false;
}
Class<?>[] paramTypes = method.getParameterTypes();
return (paramTypes.length == 1 && paramTypes[0] == Object.class);
if (method.getParameterCount() != 1) {
return false;
}
return method.getParameterTypes()[0] == Object.class;
}
/**