View | Details | Raw Unified | Return to bug 51852
Collapse All | Expand All

(-)test/javax/el/TestBeanELResolverVarargsInvocation.java (+122 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 * 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 * 
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package javax.el;
18
19
import java.lang.reflect.Method;
20
import java.util.HashMap;
21
import java.util.Map;
22
23
import javax.el.BeanELResolver;
24
import javax.el.ELContext;
25
import javax.el.ELResolver;
26
import javax.el.FunctionMapper;
27
import javax.el.ValueExpression;
28
import javax.el.VariableMapper;
29
30
import org.junit.Assert;
31
import org.junit.Before;
32
import org.junit.Test;
33
34
public class TestBeanELResolverVarargsInvocation {
35
    public static class Foo {
36
37
        public String joinDelimited(String delim, String... strings) {
38
            StringBuilder result = new StringBuilder();
39
            if (strings != null) {
40
                for (String s : strings) {
41
                    if (delim != null && result.length() > 0) {
42
                        result.append(delim);
43
                    }
44
                    result.append(s);
45
                }
46
            }
47
            return result.toString();
48
        }
49
50
        public String join(String... strings) {
51
            return joinDelimited(null, strings);
52
        }
53
    }
54
55
    private Foo foo;
56
    private ELContext elContext;
57
    private BeanELResolver beanELResolver;
58
59
    @Before
60
    public void setup() {
61
        foo = new Foo();
62
        beanELResolver = new BeanELResolver();
63
        elContext = new ELContext() {
64
            private VariableMapper variableMapper = new VariableMapper() {
65
                private Map<String, ValueExpression> vars = new HashMap<String, ValueExpression>();
66
67
                @Override
68
                public ValueExpression setVariable(String arg0, ValueExpression arg1) {
69
                    return vars.put(arg0, arg1);
70
                }
71
72
                @Override
73
                public ValueExpression resolveVariable(String arg0) {
74
                    return vars.get(arg0);
75
                }
76
            };
77
            private FunctionMapper functionMapper = new FunctionMapper() {
78
79
                @Override
80
                public Method resolveFunction(String arg0, String arg1) {
81
                    return null;
82
                }
83
            };
84
85
            @Override
86
            public VariableMapper getVariableMapper() {
87
                return variableMapper;
88
            }
89
90
            @Override
91
            public FunctionMapper getFunctionMapper() {
92
                return functionMapper;
93
            }
94
95
            @Override
96
            public ELResolver getELResolver() {
97
                return beanELResolver;
98
            }
99
        };
100
    }
101
102
    /**
103
     * Tests varargs that come after an opening argument.
104
     */
105
    @Test
106
    public void testJoinDelimited() {
107
        Assert.assertEquals(foo.joinDelimited("-", "foo", "bar", "baz"),
108
            beanELResolver.invoke(elContext, foo, "joinDelimited", null, new Object[] { "-", "foo", "bar", "baz" }));
109
    }
110
111
    /**
112
     * Tests varargs that constitute a method's only parameters, as well as bogus results
113
     * due to improper matching of ANY vararg method, and depending on the order in which
114
     * reflected methods are encountered.
115
     */
116
    @Test
117
    public void testJoin() {
118
        Assert.assertEquals(foo.join("foo", "bar", "baz"),
119
            beanELResolver.invoke(elContext, foo, "join", null, new Object[] { "foo", "bar", "baz" }));
120
    }
121
122
}
0
  + native
123
  + native
(-)java/javax/el/BeanELResolver.java (-8 / +9 lines)
Lines 420-426 Link Here
420
                    matchingMethod = getMethod(clazz, m);
420
                    matchingMethod = getMethod(clazz, m);
421
                    break;
421
                    break;
422
                }
422
                }
423
                if (m.isVarArgs()) {
423
                if (m.isVarArgs() && methodName.equals(m.getName()) && 
424
                            paramCount > m.getParameterTypes().length - 2 ) {
424
                    matchingMethod = getMethod(clazz, m);
425
                    matchingMethod = getMethod(clazz, m);
425
                }
426
                }
426
            }
427
            }
Lines 440-460 Link Here
440
            if (matchingMethod.isVarArgs()) {
441
            if (matchingMethod.isVarArgs()) {
441
                int varArgIndex = parameterTypes.length - 1;
442
                int varArgIndex = parameterTypes.length - 1;
442
                // First argCount-1 parameters are standard
443
                // First argCount-1 parameters are standard
443
                for (int i = 0; (i < varArgIndex - 1); i++) {
444
                for (int i = 0; (i < varArgIndex); i++) {
444
                    parameters[i] = factory.coerceToType(params[i],
445
                    parameters[i] = factory.coerceToType(params[i],
445
                            parameterTypes[i]);
446
                            parameterTypes[i]);
446
                }
447
                }
447
                // Last parameter is the varags
448
                // Last parameter is the varargs
448
                Class<?> varArgClass =
449
                Class<?> varArgClass =
449
                    parameterTypes[varArgIndex].getComponentType();
450
                    parameterTypes[varArgIndex].getComponentType();
451
                final Object varargs = Array.newInstance(
452
                    varArgClass,
453
                    (paramCount - varArgIndex));
450
                for (int i = (varArgIndex); i < paramCount; i++) {
454
                for (int i = (varArgIndex); i < paramCount; i++) {
451
                    Object varargs = Array.newInstance(
455
                    Array.set(varargs, i - varArgIndex,
452
                            parameterTypes[paramCount],
453
                            (paramCount - varArgIndex));
454
                    Array.set(varargs, i,
455
                            factory.coerceToType(params[i], varArgClass));
456
                            factory.coerceToType(params[i], varArgClass));
456
                    parameters[varArgIndex] = varargs;
457
                }
457
                }
458
                parameters[varArgIndex] = varargs;
458
            } else {
459
            } else {
459
                parameters = new Object[parameterTypes.length];
460
                parameters = new Object[parameterTypes.length];
460
                for (int i = 0; i < parameterTypes.length; i++) {
461
                for (int i = 0; i < parameterTypes.length; i++) {

Return to bug 51852