Include argument types in MethodReference cache

Update the cached MethodExecutor in MethodReference to include the
method argument types. Prevents the incorrect use of the cache when the
SpEL expression refers to a class that has overloaded methods.

Issue: SPR-10657
(cherry picked from commit f9b12b8)
This commit is contained in:
Phillip Webb
2013-06-24 21:28:59 +02:00
committed by Juergen Hoeller
parent d4b54d834e
commit 1e3dc4aeae
2 changed files with 145 additions and 27 deletions

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
import org.junit.Before;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.ast.MethodReference;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import static org.junit.Assert.*;
/**
* Test for caching in {@link MethodReference} (SPR-10657).
*
* @author Oliver Becker
*/
public class CachedMethodExecutorTests {
private final ExpressionParser parser = new SpelExpressionParser();
private StandardEvaluationContext context;
@Before
public void setUp() throws Exception {
this.context = new StandardEvaluationContext(new RootObject());
}
@Test
public void testCachedExecution() throws Exception {
Expression expression = this.parser.parseExpression("echo(#something)");
assertMethodExecution(expression, 42, "int: 42");
assertMethodExecution(expression, 42, "int: 42");
assertMethodExecution(expression, "Deep Thought", "String: Deep Thought");
assertMethodExecution(expression, 42, "int: 42");
}
private void assertMethodExecution(Expression expression, Object var, String expected) {
this.context.setVariable("something", var);
assertEquals(expected, expression.getValue(this.context));
}
public static class RootObject {
public String echo(String value) {
return "String: " + value;
}
public String echo(int value) {
return "int: " + value;
}
}
}