Browse Source

Include function name in SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION

Closes gh-32239
pull/32243/head
Sam Brannen 2 years ago
parent
commit
f295def2a8
  1. 2
      spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java
  2. 4
      spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java
  3. 16
      spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java

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

@ -77,7 +77,7 @@ public enum SpelMessage { @@ -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}''"),

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

@ -121,7 +121,7 @@ public class FunctionReference extends SpelNodeImpl { @@ -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 { @@ -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:

16
spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java

@ -48,14 +48,14 @@ class VariableAndFunctionTests extends AbstractExpressionTests { @@ -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);
// 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);
// 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(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

Loading…
Cancel
Save