Skip to content

Commit 30363c8

Browse files
committed
Consistent SpelEvaluationException messages in findAccessorForMethod
Issue: SPR-16762
1 parent fa27130 commit 30363c8

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/SpelEvaluationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -48,7 +48,7 @@ public SpelEvaluationException(int position, SpelMessage message, Object... inse
4848
}
4949

5050
public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) {
51-
super(position, message.formatMessage(inserts),cause);
51+
super(position, message.formatMessage(inserts), cause);
5252
this.message = message;
5353
this.inserts = inserts;
5454
}

spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public enum SpelMessage {
4545
"A problem occurred whilst attempting to construct an object of type ''{0}'' using arguments ''{1}''"),
4646

4747
METHOD_NOT_FOUND(Kind.ERROR, 1004,
48-
"Method call: Method {0} cannot be found on {1} type"),
48+
"Method call: Method {0} cannot be found on type {1}"),
4949

5050
TYPE_NOT_FOUND(Kind.ERROR, 1005,
5151
"Type cannot be found ''{0}''"),
5252

5353
FUNCTION_NOT_DEFINED(Kind.ERROR, 1006,
54-
"The function ''{0}'' could not be found"),
54+
"Function ''{0}'' could not be found"),
5555

5656
PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL(Kind.ERROR, 1007,
5757
"Property or field ''{0}'' cannot be found on null"),

spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -23,7 +23,7 @@
2323
import org.springframework.util.ClassUtils;
2424

2525
/**
26-
* Utility methods (formatters, etc) used during parsing and evaluation.
26+
* Utility methods (formatters etc) used during parsing and evaluation.
2727
*
2828
* @author Andy Clement
2929
*/

spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private TypedValue getValueInternal(EvaluationContext evaluationContext,
131131
}
132132

133133
// either there was no accessor or it no longer existed
134-
executorToUse = findAccessorForMethod(this.name, argumentTypes, value, evaluationContext);
134+
executorToUse = findAccessorForMethod(argumentTypes, value, evaluationContext);
135135
this.cachedExecutor = new CachedMethodExecutor(
136136
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
137137
try {
@@ -196,33 +196,40 @@ private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Ob
196196
return null;
197197
}
198198

199-
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
200-
Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {
199+
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
200+
EvaluationContext evaluationContext) throws SpelEvaluationException {
201201

202+
AccessException accessException = null;
202203
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
203204
for (MethodResolver methodResolver : methodResolvers) {
204205
try {
205206
MethodExecutor methodExecutor = methodResolver.resolve(
206-
evaluationContext, targetObject, name, argumentTypes);
207+
evaluationContext, targetObject, this.name, argumentTypes);
207208
if (methodExecutor != null) {
208209
return methodExecutor;
209210
}
210211
}
211212
catch (AccessException ex) {
212-
throw new SpelEvaluationException(getStartPosition(), ex,
213-
SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
213+
accessException = ex;
214+
break;
214215
}
215216
}
216217

217-
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
218-
FormatHelper.formatMethodForMessage(name, argumentTypes),
219-
FormatHelper.formatClassNameForMessage(
220-
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
218+
String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
219+
String className = FormatHelper.formatClassNameForMessage(
220+
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
221+
if (accessException != null) {
222+
throw new SpelEvaluationException(
223+
getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
224+
}
225+
else {
226+
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
227+
}
221228
}
222229

223230
/**
224-
* Decode the AccessException, throwing a lightweight evaluation exception or, if the
225-
* cause was a RuntimeException, throw the RuntimeException directly.
231+
* Decode the AccessException, throwing a lightweight evaluation exception or,
232+
* if the cause was a RuntimeException, throw the RuntimeException directly.
226233
*/
227234
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
228235
if (ex.getCause() instanceof InvocationTargetException) {

0 commit comments

Comments
 (0)