From 23db8b58da08531e48ba8fe41def467b5b694a49 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Mon, 18 Aug 2008 22:43:17 +0000 Subject: [PATCH] added test and behaviour for calling a java method with incorrect number of arguments --- .../expression/spel/ast/FunctionReference.java | 11 ++++++++--- .../expression/spel/VariableAndFunctionTests.java | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java b/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java index 1b28e8f9bf..3fceb12856 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java @@ -35,12 +35,12 @@ import org.springframework.expression.spel.reflection.ReflectionUtils; * A function reference is of the form "#someFunction(a,b,c)". Functions may be defined in the context prior to the * expression being evaluated or within the expression itself using a lambda function definition. For example: Lambda * function definition in an expression: "(#max = {|x,y|$x>$y?$x:$y};max(2,3))" Calling context defined function: - * "#isEven(37)" + * "#isEven(37)". Functions may also be static java methods, registered in the context prior to invocation of the + * expression. * * Functions are very simplistic, the arguments are not part of the definition (right now), so the names must be unique. * * @author Andy Clement - * */ public class FunctionReference extends SpelNode { @@ -81,11 +81,15 @@ public class FunctionReference extends SpelNode { private Object executeFunctionJLRMethod(ExpressionState state, Method m) throws EvaluationException { Object[] functionArgs = getArguments(state); + if (!m.isVarArgs() && m.getParameterTypes().length != functionArgs.length) { + throw new SpelException(SpelMessages.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, m.getParameterTypes().length); + } // Only static methods can be called in this way if (!Modifier.isStatic(m.getModifiers())) { throw new SpelException(getCharPositionInLine(),SpelMessages.FUNCTION_MUST_BE_STATIC,m.getDeclaringClass().getName()+"."+m.getName(),name); } - + + // Convert arguments if necessary and remap them for varargs if required if (functionArgs != null) { EvaluationContext ctx = state.getEvaluationContext(); TypeConverter converter = null; @@ -112,6 +116,7 @@ public class FunctionReference extends SpelNode { } } + /* * Execute a function that was defined as a lambda function. */ diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java index 0ed45d4795..24fd27e57b 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java @@ -33,6 +33,7 @@ public class VariableAndFunctionTests extends ExpressionTestCase { public void testFunctionAccess01() { evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class); evaluate("#reverseInt('1',2,3)", "int[3]{3,2,1}", int[].class); // requires type conversion of '1' to 1 + evaluateAndCheckError("#reverseInt(1)", SpelMessages.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 1, 1, 3); } public void testFunctionAccess02() {