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

@@ -44,8 +44,7 @@ public class ReflectiveConstructorExecutor implements ConstructorExecutor {
public ReflectiveConstructorExecutor(Constructor<?> ctor) {
this.ctor = ctor;
if (ctor.isVarArgs()) {
Class<?>[] paramTypes = ctor.getParameterTypes();
this.varargsPosition = paramTypes.length - 1;
this.varargsPosition = ctor.getParameterCount() - 1;
}
else {
this.varargsPosition = null;

View File

@@ -69,13 +69,13 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
Constructor<?> matchRequiringConversion = null;
for (Constructor<?> ctor : ctors) {
Class<?>[] paramTypes = ctor.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++) {
int paramCount = ctor.getParameterCount();
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
for (int i = 0; i < paramCount; i++) {
paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
}
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
if (ctor.isVarArgs() && argumentTypes.size() >= paramCount - 1) {
// *sigh* complicated
// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
// being provided should be
@@ -84,7 +84,7 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
// we are supplied does match exactly (it is an array already).
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
}
else if (paramTypes.length == argumentTypes.size()) {
else if (paramCount == argumentTypes.size()) {
// worth a closer look
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
}

View File

@@ -61,8 +61,7 @@ public class ReflectiveMethodExecutor implements MethodExecutor {
this.originalMethod = method;
this.methodToInvoke = ClassUtils.getInterfaceMethodIfPossible(method);
if (method.isVarArgs()) {
Class<?>[] paramTypes = method.getParameterTypes();
this.varargsPosition = paramTypes.length - 1;
this.varargsPosition = method.getParameterCount() - 1;
}
else {
this.varargsPosition = null;

View File

@@ -160,17 +160,17 @@ public class ReflectiveMethodResolver implements MethodResolver {
for (Method method : methodsToIterate) {
if (method.getName().equals(name)) {
Class<?>[] paramTypes = method.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++) {
int paramCount = method.getParameterCount();
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
for (int i = 0; i < paramCount; i++) {
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
}
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
if (method.isVarArgs() && argumentTypes.size() >= (paramTypes.length - 1)) {
if (method.isVarArgs() && argumentTypes.size() >= (paramCount - 1)) {
// *sigh* complicated
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
}
else if (paramTypes.length == argumentTypes.size()) {
else if (paramCount == argumentTypes.size()) {
// Name and parameter number match, check the arguments
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
}