Skip to content

Commit d291150

Browse files
authored
Fix BZ 70297 - Evaluate a single EL function varargs argument once (#1130)
When an EL function whose method is varargs was called with exactly one argument for the varargs parameter, AstFunction evaluated that argument twice: once to check whether it is already an array and once more for the value. An argument with side effects ran twice, the function received the result of the second evaluation, and an expensive argument cost double. Evaluate the argument once and use the value both for the array check and as the single varargs element. Calls with two or more varargs values and the single array argument case are unchanged. Adds a test that counts the evaluations of a method call used as the single varargs argument. The test reports two evaluations against the previous implementation.
1 parent bda452a commit d291150

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎java/org/apache/el/parser/AstFunction.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,16 @@ public Object getValue(EvaluationContext ctx) throws ELException {
170170
// Java semantics: an empty array is passed when no
171171
// varargs elements are supplied
172172
params[i] = new Object[0];
173-
} else if (inputParameterCount == methodParameterCount &&
174-
isArray(parameters.jjtGetChild(i).getValue(ctx))) {
175-
params[i] = parameters.jjtGetChild(i).getValue(ctx);
173+
} else if (inputParameterCount == methodParameterCount) {
174+
// Evaluate the single argument once: it is either
175+
// the varargs array itself or its only element
176+
Object value = parameters.jjtGetChild(i).getValue(ctx);
177+
if (isArray(value)) {
178+
params[i] = value;
179+
} else {
180+
params[i] = new Object[] { ELSupport.coerceToType(ctx, value,
181+
paramTypes[i].getComponentType()) };
182+
}
176183
} else {
177184
Object[] varargs = new Object[inputParameterCount - methodParameterCount + 1];
178185
Class<?> target = paramTypes[i].getComponentType();

‎test/org/apache/el/parser/TestAstFunction.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Assert;
2626
import org.junit.Test;
2727

28+
import org.apache.el.TesterBeanK;
2829
import org.apache.el.TesterFunctions;
2930

3031
public class TestAstFunction {
@@ -108,6 +109,19 @@ public void testVarargMethod() throws NoSuchMethodException, SecurityException {
108109
Assert.assertEquals("one", result);
109110
}
110111

112+
@Test
113+
public void testVarargMethodSingleArgumentEvaluatedOnce() throws NoSuchMethodException {
114+
// The argument that alone fills the varargs parameter must be evaluated
115+
// exactly once, as each argument is when there are several
116+
ELProcessor processor = new ELProcessor();
117+
TesterBeanK bean = new TesterBeanK();
118+
processor.defineBean("beanK", bean);
119+
processor.defineFunction("fn", "format", String.class.getMethod("format", String.class, Object[].class));
120+
121+
Assert.assertEquals("1", processor.getValue("fn:format('%s', beanK.next())", String.class));
122+
Assert.assertEquals(1, bean.argCalls);
123+
}
124+
111125
@Test
112126
public void testVarargMethodEmptyVarargs01() throws NoSuchMethodException {
113127
// Java passes an empty array, never null, to a varargs method that is

0 commit comments

Comments
 (0)