Consistent SpelEvaluationException messages in findAccessorForMethod
Issue: SPR-16762
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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) {
|
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.message = message;
|
||||||
this.inserts = inserts;
|
this.inserts = inserts;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,13 +45,13 @@ public enum SpelMessage {
|
|||||||
"A problem occurred whilst attempting to construct an object of type ''{0}'' using arguments ''{1}''"),
|
"A problem occurred whilst attempting to construct an object of type ''{0}'' using arguments ''{1}''"),
|
||||||
|
|
||||||
METHOD_NOT_FOUND(Kind.ERROR, 1004,
|
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_NOT_FOUND(Kind.ERROR, 1005,
|
||||||
"Type cannot be found ''{0}''"),
|
"Type cannot be found ''{0}''"),
|
||||||
|
|
||||||
FUNCTION_NOT_DEFINED(Kind.ERROR, 1006,
|
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_NOT_READABLE_ON_NULL(Kind.ERROR, 1007,
|
||||||
"Property or field ''{0}'' cannot be found on null"),
|
"Property or field ''{0}'' cannot be found on null"),
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
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
|
* @author Andy Clement
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ public class MethodReference extends SpelNodeImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// either there was no accessor or it no longer existed
|
// 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(
|
this.cachedExecutor = new CachedMethodExecutor(
|
||||||
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
|
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
|
||||||
try {
|
try {
|
||||||
@@ -196,33 +196,40 @@ public class MethodReference extends SpelNodeImpl {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
|
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
|
||||||
Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {
|
EvaluationContext evaluationContext) throws SpelEvaluationException {
|
||||||
|
|
||||||
|
AccessException accessException = null;
|
||||||
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
|
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
|
||||||
for (MethodResolver methodResolver : methodResolvers) {
|
for (MethodResolver methodResolver : methodResolvers) {
|
||||||
try {
|
try {
|
||||||
MethodExecutor methodExecutor = methodResolver.resolve(
|
MethodExecutor methodExecutor = methodResolver.resolve(
|
||||||
evaluationContext, targetObject, name, argumentTypes);
|
evaluationContext, targetObject, this.name, argumentTypes);
|
||||||
if (methodExecutor != null) {
|
if (methodExecutor != null) {
|
||||||
return methodExecutor;
|
return methodExecutor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (AccessException ex) {
|
catch (AccessException ex) {
|
||||||
throw new SpelEvaluationException(getStartPosition(), ex,
|
accessException = ex;
|
||||||
SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
|
String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
|
||||||
FormatHelper.formatMethodForMessage(name, argumentTypes),
|
String className = FormatHelper.formatClassNameForMessage(
|
||||||
FormatHelper.formatClassNameForMessage(
|
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
|
||||||
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
|
* Decode the AccessException, throwing a lightweight evaluation exception or,
|
||||||
* cause was a RuntimeException, throw the RuntimeException directly.
|
* if the cause was a RuntimeException, throw the RuntimeException directly.
|
||||||
*/
|
*/
|
||||||
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
|
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
|
||||||
if (ex.getCause() instanceof InvocationTargetException) {
|
if (ex.getCause() instanceof InvocationTargetException) {
|
||||||
|
|||||||
Reference in New Issue
Block a user