SPR-6763: more methods on StandardEvaluationContext supporting add/remove from resolver/accessor strategies

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2902 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Andy Clement 2010-02-02 21:38:33 +00:00
parent 21cb4a0bfc
commit 5024290980
4 changed files with 125 additions and 0 deletions

View File

@ -97,11 +97,20 @@ public class StandardEvaluationContext implements EvaluationContext {
ensureConstructorResolversInitialized();
this.constructorResolvers.add(this.constructorResolvers.size() - 1, resolver);
}
public boolean removeConstructorResolver(ConstructorResolver resolver) {
ensureConstructorResolversInitialized();
return this.constructorResolvers.remove(resolver);
}
public List<ConstructorResolver> getConstructorResolvers() {
ensureConstructorResolversInitialized();
return this.constructorResolvers;
}
public void setConstructorResolvers(List<ConstructorResolver> constructorResolvers) {
this.constructorResolvers = constructorResolvers;
}
private void ensureConstructorResolversInitialized() {
if (this.constructorResolvers == null) {
@ -114,11 +123,20 @@ public class StandardEvaluationContext implements EvaluationContext {
ensureMethodResolversInitialized();
this.methodResolvers.add(this.methodResolvers.size() - 1, resolver);
}
public boolean removeMethodResolver(MethodResolver methodResolver) {
ensureMethodResolversInitialized();
return this.methodResolvers.remove(methodResolver);
}
public List<MethodResolver> getMethodResolvers() {
ensureMethodResolversInitialized();
return this.methodResolvers;
}
public void setMethodResolvers(List<MethodResolver> methodResolvers) {
this.methodResolvers = methodResolvers;
}
private void ensureMethodResolversInitialized() {
if (this.methodResolvers == null) {
@ -131,11 +149,19 @@ public class StandardEvaluationContext implements EvaluationContext {
ensurePropertyAccessorsInitialized();
this.propertyAccessors.add(this.propertyAccessors.size() - 1, accessor);
}
public boolean removePropertyAccessor(PropertyAccessor accessor) {
return this.propertyAccessors.remove(accessor);
}
public List<PropertyAccessor> getPropertyAccessors() {
ensurePropertyAccessorsInitialized();
return this.propertyAccessors;
}
public void setPropertyAccessors(List<PropertyAccessor> propertyAccessors) {
this.propertyAccessors = propertyAccessors;
}
private void ensurePropertyAccessorsInitialized() {
if (this.propertyAccessors == null) {

View File

@ -16,9 +16,16 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.ConstructorExecutor;
import org.springframework.expression.ConstructorResolver;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@ -140,6 +147,37 @@ public class ConstructorInvocationTests extends ExpressionTestCase {
Assert.assertEquals(4,parser.parseExpression("counter").getValue(eContext));
}
@Test
public void testAddingConstructorResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective constructor accessor is the only one by default
List<ConstructorResolver> constructorResolvers = ctx.getConstructorResolvers();
Assert.assertEquals(1,constructorResolvers.size());
ConstructorResolver dummy = new DummyConstructorResolver();
ctx.addConstructorResolver(dummy);
Assert.assertEquals(2,ctx.getConstructorResolvers().size());
List<ConstructorResolver> copy = new ArrayList<ConstructorResolver>();
copy.addAll(ctx.getConstructorResolvers());
Assert.assertTrue(ctx.removeConstructorResolver(dummy));
Assert.assertFalse(ctx.removeConstructorResolver(dummy));
Assert.assertEquals(1,ctx.getConstructorResolvers().size());
ctx.setConstructorResolvers(copy);
Assert.assertEquals(2,ctx.getConstructorResolvers().size());
}
static class DummyConstructorResolver implements ConstructorResolver {
public ConstructorExecutor resolve(EvaluationContext context, String typeName, Class<?>[] argumentTypes)
throws AccessException {
throw new UnsupportedOperationException("Auto-generated method stub");
}
}
@Test
public void testVarargsInvocation01() {
// Calling 'Fruit(String... strings)'

View File

@ -25,8 +25,12 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodFilter;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@ -243,6 +247,38 @@ public class MethodInvocationTests extends ExpressionTestCase {
}
}
@Test
public void testAddingMethodResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective method accessor is the only one by default
List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
Assert.assertEquals(1,methodResolvers.size());
MethodResolver dummy = new DummyMethodResolver();
ctx.addMethodResolver(dummy);
Assert.assertEquals(2,ctx.getMethodResolvers().size());
List<MethodResolver> copy = new ArrayList<MethodResolver>();
copy.addAll(ctx.getMethodResolvers());
Assert.assertTrue(ctx.removeMethodResolver(dummy));
Assert.assertFalse(ctx.removeMethodResolver(dummy));
Assert.assertEquals(1,ctx.getMethodResolvers().size());
ctx.setMethodResolvers(copy);
Assert.assertEquals(2,ctx.getMethodResolvers().size());
}
static class DummyMethodResolver implements MethodResolver {
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
Class<?>[] argumentTypes) throws AccessException {
throw new UnsupportedOperationException("Auto-generated method stub");
}
}
@Test
public void testVarargsInvocation01() {

View File

@ -16,6 +16,9 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
@ -129,6 +132,28 @@ public class PropertyAccessTests extends ExpressionTestCase {
// System.out.println(e.getMessage());
}
}
@Test
public void testAddingRemovingAccessors() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective property accessor is the only one by default
List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
Assert.assertEquals(1,propertyAccessors.size());
StringyPropertyAccessor spa = new StringyPropertyAccessor();
ctx.addPropertyAccessor(spa);
Assert.assertEquals(2,ctx.getPropertyAccessors().size());
List<PropertyAccessor> copy = new ArrayList<PropertyAccessor>();
copy.addAll(ctx.getPropertyAccessors());
Assert.assertTrue(ctx.removePropertyAccessor(spa));
Assert.assertFalse(ctx.removePropertyAccessor(spa));
Assert.assertEquals(1,ctx.getPropertyAccessors().size());
ctx.setPropertyAccessors(copy);
Assert.assertEquals(2,ctx.getPropertyAccessors().size());
}
// This can resolve the property 'flibbles' on any String (very useful...)