Skip to content

Commit 9f4127f

Browse files
hpoettkerfmbenhassine
authored andcommitted
Fix HippyMethodInvoker by not considering unsuitable candidates
Resolves spring-projects#3794
1 parent 324a769 commit 9f4127f

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/HippyMethodInvoker.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2010 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,41 +36,35 @@ public class HippyMethodInvoker extends MethodInvoker {
3636
protected Method findMatchingMethod() {
3737
String targetMethod = getTargetMethod();
3838
Object[] arguments = getArguments();
39-
int argCount = arguments.length;
4039

4140
Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
4241
int minTypeDiffWeight = Integer.MAX_VALUE;
4342
Method matchingMethod = null;
4443

4544
Object[] transformedArguments = null;
46-
int transformedArgumentCount = 0;
4745

48-
for (int i = 0; i < candidates.length; i++) {
49-
Method candidate = candidates[i];
46+
for (Method candidate : candidates) {
5047
if (candidate.getName().equals(targetMethod)) {
5148
Class<?>[] paramTypes = candidate.getParameterTypes();
5249
Object[] candidateArguments = new Object[paramTypes.length];
5350
int assignedParameterCount = 0;
54-
boolean assigned = paramTypes.length==0;
55-
for (int j = 0; j < arguments.length; j++) {
56-
for (int k = 0; k < paramTypes.length; k++) {
51+
for (Object argument : arguments) {
52+
for (int i = 0; i < paramTypes.length; i++) {
5753
// Pick the first assignable of the right type that
5854
// matches this slot and hasn't already been filled...
59-
if (ClassUtils.isAssignableValue(paramTypes[k], arguments[j]) && candidateArguments[k] == null) {
60-
candidateArguments[k] = arguments[j];
55+
if (ClassUtils.isAssignableValue(paramTypes[i], argument) && candidateArguments[i] == null) {
56+
candidateArguments[i] = argument;
6157
assignedParameterCount++;
62-
assigned = true;
6358
break;
6459
}
6560
}
6661
}
67-
if (assigned && paramTypes.length <= argCount) {
62+
if (paramTypes.length == assignedParameterCount) {
6863
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, candidateArguments);
6964
if (typeDiffWeight < minTypeDiffWeight) {
7065
minTypeDiffWeight = typeDiffWeight;
7166
matchingMethod = candidate;
7267
transformedArguments = candidateArguments;
73-
transformedArgumentCount = assignedParameterCount;
7468
}
7569
}
7670
}
@@ -80,11 +74,6 @@ protected Method findMatchingMethod() {
8074
throw new IllegalArgumentException("No matching arguments found for method: " + targetMethod);
8175
}
8276

83-
if (transformedArgumentCount < transformedArguments.length) {
84-
throw new IllegalArgumentException("Only " + transformedArgumentCount + " out of "
85-
+ transformedArguments.length + " arguments could be assigned.");
86-
}
87-
8877
setArguments(transformedArguments);
8978
return matchingMethod;
9079

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/HippyMethodInvokerTests.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2012 the original author or authors.
2+
* Copyright 2010-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public void testTwoArgsOfSameTypeWithInexactMatch() throws Exception {
7070
HippyMethodInvoker invoker = new HippyMethodInvoker();
7171
invoker.setTargetMethod("duplicate");
7272
invoker.setTargetObject(new PlainPojo());
73-
invoker.setArguments(new Object[] { "2", "foo" });
73+
invoker.setArguments("2", "foo");
7474
invoker.prepare();
7575
assertEquals("foo.2", invoker.invoke());
7676
}
@@ -95,12 +95,39 @@ public Class<?> foo(Set<?> arg) {
9595
assertEquals(target.foo(arg), Set.class);
9696

9797
invoker.setTargetObject(target);
98-
invoker.setArguments(new Object[] { arg });
98+
invoker.setArguments(arg);
9999
invoker.prepare();
100100
assertEquals(invoker.invoke(), Set.class);
101101

102102
}
103103

104+
@Test
105+
public void testOverloadedMethodWithTwoArgumentsAndOneExactMatch() throws Exception {
106+
107+
HippyMethodInvoker invoker = new HippyMethodInvoker();
108+
invoker.setTargetMethod("foo");
109+
@SuppressWarnings("unused")
110+
class OverloadingPojo {
111+
public Class<?> foo(String arg1, Number arg2) {
112+
return Number.class;
113+
}
114+
public Class<?> foo(String arg1, List<?> arg2) {
115+
return List.class;
116+
}
117+
}
118+
119+
String exactArg = "string";
120+
Integer inexactArg = 0;
121+
OverloadingPojo target = new OverloadingPojo();
122+
assertEquals(target.foo(exactArg, inexactArg), Number.class);
123+
124+
invoker.setTargetObject(target);
125+
invoker.setArguments(exactArg, inexactArg);
126+
invoker.prepare();
127+
assertEquals(invoker.invoke(), Number.class);
128+
129+
}
130+
104131
public static class PlainPojo {
105132
public String handle(double value, String input) {
106133
return value + "." + input;

0 commit comments

Comments
 (0)