Consistent SpelEvaluationException messages in findAccessorForMethod

Issue: SPR-16762
This commit is contained in:
Juergen Hoeller 2018-05-02 16:53:55 +02:00
parent fa27130b82
commit 30363c84bd
4 changed files with 25 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -48,7 +48,7 @@ public class SpelEvaluationException extends EvaluationException {
}
public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(inserts),cause);
super(position, message.formatMessage(inserts), cause);
this.message = message;
this.inserts = inserts;
}

View File

@ -45,13 +45,13 @@ public enum SpelMessage {
"A problem occurred whilst attempting to construct an object of type ''{0}'' using arguments ''{1}''"),
METHOD_NOT_FOUND(Kind.ERROR, 1004,
"Method call: Method {0} cannot be found on {1} type"),
"Method call: Method {0} cannot be found on type {1}"),
TYPE_NOT_FOUND(Kind.ERROR, 1005,
"Type cannot be found ''{0}''"),
FUNCTION_NOT_DEFINED(Kind.ERROR, 1006,
"The function ''{0}'' could not be found"),
"Function ''{0}'' could not be found"),
PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL(Kind.ERROR, 1007,
"Property or field ''{0}'' cannot be found on null"),

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,7 +23,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
* Utility methods (formatters, etc) used during parsing and evaluation.
* Utility methods (formatters etc) used during parsing and evaluation.
*
* @author Andy Clement
*/

View File

@ -131,7 +131,7 @@ public class MethodReference extends SpelNodeImpl {
}
// either there was no accessor or it no longer existed
executorToUse = findAccessorForMethod(this.name, argumentTypes, value, evaluationContext);
executorToUse = findAccessorForMethod(argumentTypes, value, evaluationContext);
this.cachedExecutor = new CachedMethodExecutor(
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
try {
@ -196,33 +196,40 @@ public class MethodReference extends SpelNodeImpl {
return null;
}
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
EvaluationContext evaluationContext) throws SpelEvaluationException {
AccessException accessException = null;
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
for (MethodResolver methodResolver : methodResolvers) {
try {
MethodExecutor methodExecutor = methodResolver.resolve(
evaluationContext, targetObject, name, argumentTypes);
evaluationContext, targetObject, this.name, argumentTypes);
if (methodExecutor != null) {
return methodExecutor;
}
}
catch (AccessException ex) {
throw new SpelEvaluationException(getStartPosition(), ex,
SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
accessException = ex;
break;
}
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
FormatHelper.formatMethodForMessage(name, argumentTypes),
FormatHelper.formatClassNameForMessage(
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
String className = FormatHelper.formatClassNameForMessage(
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
if (accessException != null) {
throw new SpelEvaluationException(
getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
}
else {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
}
}
/**
* Decode the AccessException, throwing a lightweight evaluation exception or, if the
* cause was a RuntimeException, throw the RuntimeException directly.
* Decode the AccessException, throwing a lightweight evaluation exception or,
* if the cause was a RuntimeException, throw the RuntimeException directly.
*/
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
if (ex.getCause() instanceof InvocationTargetException) {