From f295def2a85e8fe574ec26b1b5b3d775cace476e Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 11 Feb 2024 13:51:58 +0100 Subject: [PATCH] Include function name in SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION Closes gh-32239 --- .../expression/spel/SpelMessage.java | 2 +- .../expression/spel/ast/FunctionReference.java | 4 ++-- .../expression/spel/VariableAndFunctionTests.java | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) 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 692ac346b7..0c0747011c 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 @@ -77,7 +77,7 @@ public enum SpelMessage { "Cannot compare instances of {0} and {1}"), INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION(Kind.ERROR, 1014, - "Incorrect number of arguments for function, {0} supplied but function takes {1}"), + "Incorrect number of arguments for function ''{0}'': {1} supplied but function takes {2}"), INVALID_TYPE_FOR_SELECTION(Kind.ERROR, 1015, "Cannot perform selection on input data of type ''{0}''"), diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java index ba7dda43d0..19c145d0cc 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java @@ -121,7 +121,7 @@ public class FunctionReference extends SpelNodeImpl { int declaredParamCount = method.getParameterCount(); if (declaredParamCount != functionArgs.length) { throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, - functionArgs.length, declaredParamCount); + this.name, functionArgs.length, declaredParamCount); } } if (!Modifier.isStatic(method.getModifiers())) { @@ -183,7 +183,7 @@ public class FunctionReference extends SpelNodeImpl { // incorrect number, including more arguments and not a vararg // perhaps a subset of arguments was provided but the MethodHandle wasn't bound? throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, - functionArgs.length, declaredParamCount); + this.name, functionArgs.length, declaredParamCount); } // simplest case: the MethodHandle is fully bound or represents a static method with no params: diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java index 14ad7205ca..0dfa472aea 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java @@ -48,14 +48,14 @@ class VariableAndFunctionTests extends AbstractExpressionTests { @Test void functionInvocationWithIncorrectNumberOfArguments() { - // Method: reverseInt() expects 3 ints - evaluateAndCheckError("#reverseInt()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 0, 3); - evaluateAndCheckError("#reverseInt(1,2)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 2, 3); - evaluateAndCheckError("#reverseInt(1,2,3,4)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 4, 3); + // Method: #reverseInt(int, int, int) + evaluateAndCheckError("#reverseInt()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, "reverseInt", 0, 3); + evaluateAndCheckError("#reverseInt(1,2)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, "reverseInt", 2, 3); + evaluateAndCheckError("#reverseInt(1,2,3,4)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, "reverseInt", 4, 3); - // MethodHandle: message() maps to java.lang.String.format(String, Object...) - evaluateAndCheckError("#message()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 0, 2); - evaluateAndCheckError("#message('%s')", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 1, 2); + // MethodHandle: #message(template, args...) + evaluateAndCheckError("#message()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, "message", 0, 2); + evaluateAndCheckError("#message('%s')", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, "message", 1, 2); } @Test