From 30363c84bd82a76f5f6fd7f4f218743f39fc4cd6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 2 May 2018 16:53:55 +0200 Subject: [PATCH] Consistent SpelEvaluationException messages in findAccessorForMethod Issue: SPR-16762 --- .../spel/SpelEvaluationException.java | 4 +-- .../expression/spel/SpelMessage.java | 4 +-- .../expression/spel/ast/FormatHelper.java | 4 +-- .../expression/spel/ast/MethodReference.java | 31 ++++++++++++------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/SpelEvaluationException.java b/spring-expression/src/main/java/org/springframework/expression/spel/SpelEvaluationException.java index 9aced9dbf59..30d5e8b7db9 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/SpelEvaluationException.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/SpelEvaluationException.java @@ -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; } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java b/spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java index 03787c84a50..42292db5246 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java @@ -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"), diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java index f271910f1b3..7c545ae0010 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FormatHelper.java @@ -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 */ diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java index 16ecbbaaa14..b6a7505ffc6 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java @@ -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 argumentTypes, - Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException { + private MethodExecutor findAccessorForMethod(List argumentTypes, Object targetObject, + EvaluationContext evaluationContext) throws SpelEvaluationException { + AccessException accessException = null; List 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) {