Skip to content

Commit 5105fdf

Browse files
committed
Filter candidate methods by name first (for more efficient sorting)
Closes gh-28377 (cherry picked from commit 0599320)
1 parent 687676e commit 5105fdf

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

+35-36
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public MethodExecutor resolve(EvaluationContext context, Object targetObject, St
117117
TypeConverter typeConverter = context.getTypeConverter();
118118
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
119119
ArrayList<Method> methods = new ArrayList<>(getMethods(type, targetObject));
120+
methods.removeIf(method -> !method.getName().equals(name));
120121

121122
// If a filter is registered for this type, call it
122123
MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
@@ -160,48 +161,46 @@ else if (m1.isVarArgs() && !m2.isVarArgs()) {
160161
boolean multipleOptions = false;
161162

162163
for (Method method : methodsToIterate) {
163-
if (method.getName().equals(name)) {
164-
int paramCount = method.getParameterCount();
165-
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
166-
for (int i = 0; i < paramCount; i++) {
167-
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
168-
}
169-
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
170-
if (method.isVarArgs() && argumentTypes.size() >= (paramCount - 1)) {
171-
// *sigh* complicated
172-
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
173-
}
174-
else if (paramCount == argumentTypes.size()) {
175-
// Name and parameter number match, check the arguments
176-
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
164+
int paramCount = method.getParameterCount();
165+
List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
166+
for (int i = 0; i < paramCount; i++) {
167+
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
168+
}
169+
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
170+
if (method.isVarArgs() && argumentTypes.size() >= (paramCount - 1)) {
171+
// *sigh* complicated
172+
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
173+
}
174+
else if (paramCount == argumentTypes.size()) {
175+
// Name and parameter number match, check the arguments
176+
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
177+
}
178+
if (matchInfo != null) {
179+
if (matchInfo.isExactMatch()) {
180+
return new ReflectiveMethodExecutor(method, type);
177181
}
178-
if (matchInfo != null) {
179-
if (matchInfo.isExactMatch()) {
180-
return new ReflectiveMethodExecutor(method, type);
181-
}
182-
else if (matchInfo.isCloseMatch()) {
183-
if (this.useDistance) {
184-
int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
185-
if (closeMatch == null || matchDistance < closeMatchDistance) {
186-
// This is a better match...
187-
closeMatch = method;
188-
closeMatchDistance = matchDistance;
189-
}
190-
}
191-
else {
192-
// Take this as a close match if there isn't one already
193-
if (closeMatch == null) {
194-
closeMatch = method;
195-
}
182+
else if (matchInfo.isCloseMatch()) {
183+
if (this.useDistance) {
184+
int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
185+
if (closeMatch == null || matchDistance < closeMatchDistance) {
186+
// This is a better match...
187+
closeMatch = method;
188+
closeMatchDistance = matchDistance;
196189
}
197190
}
198-
else if (matchInfo.isMatchRequiringConversion()) {
199-
if (matchRequiringConversion != null) {
200-
multipleOptions = true;
191+
else {
192+
// Take this as a close match if there isn't one already
193+
if (closeMatch == null) {
194+
closeMatch = method;
201195
}
202-
matchRequiringConversion = method;
203196
}
204197
}
198+
else if (matchInfo.isMatchRequiringConversion()) {
199+
if (matchRequiringConversion != null) {
200+
multipleOptions = true;
201+
}
202+
matchRequiringConversion = method;
203+
}
205204
}
206205
}
207206
if (closeMatch != null) {

0 commit comments

Comments
 (0)