Correctly resolve accessors for static properties on Class

Issue: SPR-11609
This commit is contained in:
Juergen Hoeller
2014-03-26 21:46:38 +01:00
parent e3035363a6
commit 3af8a3260a
2 changed files with 66 additions and 57 deletions

View File

@@ -286,6 +286,11 @@ public class SpelReproTests extends AbstractExpressionTests {
static class MapAccessor implements PropertyAccessor {
@Override
public Class<?>[] getSpecificTargetClasses() {
return new Class<?>[] {Map.class};
}
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
return (((Map<?, ?>) target).containsKey(name));
@@ -306,11 +311,6 @@ public class SpelReproTests extends AbstractExpressionTests {
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
((Map<String, Object>) target).put(name, newValue);
}
@Override
public Class<?>[] getSpecificTargetClasses() {
return new Class[] { Map.class };
}
}
@@ -1774,10 +1774,10 @@ public class SpelReproTests extends AbstractExpressionTests {
public void SPR10486() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
SPR10486 rootObject = new SPR10486();
Spr10486 rootObject = new Spr10486();
Expression classNameExpression = parser.parseExpression("class.name");
Expression nameExpression = parser.parseExpression("name");
assertThat(classNameExpression.getValue(context, rootObject), equalTo((Object) SPR10486.class.getName()));
assertThat(classNameExpression.getValue(context, rootObject), equalTo((Object) Spr10486.class.getName()));
assertThat(nameExpression.getValue(context, rootObject), equalTo((Object) "name"));
}
@@ -1785,7 +1785,7 @@ public class SpelReproTests extends AbstractExpressionTests {
public void SPR11142() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
SPR11142 rootObject = new SPR11142();
Spr11142 rootObject = new Spr11142();
Expression expression = parser.parseExpression("something");
thrown.expect(SpelEvaluationException.class);
thrown.expectMessage("'something' cannot be found");
@@ -1837,26 +1837,6 @@ public class SpelReproTests extends AbstractExpressionTests {
assertEquals(1, expr.getValue(context));
}
static class Spr11445Class implements BeanResolver {
private final AtomicInteger counter = new AtomicInteger();
public int echo(int invocation) {
return invocation;
}
public int parameter() {
return counter.incrementAndGet();
}
@Override
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
return beanName.equals("bean") ? this : null;
}
}
@Test
public void SPR11494() {
Expression exp = new SpelExpressionParser().parseExpression("T(java.util.Arrays).asList('a','b')");
@@ -1864,6 +1844,15 @@ public class SpelReproTests extends AbstractExpressionTests {
assertThat(list.size(), is(2));
}
@Test
public void SPR11609() {
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.addPropertyAccessor(new MapAccessor());
Expression exp = new SpelExpressionParser().parseExpression(
"T(org.springframework.expression.spel.SpelReproTests$MapWithConstant).X");
assertEquals(1, exp.getValue(sec));
}
private static enum ABC { A, B, C }
@@ -1939,7 +1928,7 @@ public class SpelReproTests extends AbstractExpressionTests {
}
public static class SPR10486 {
public static class Spr10486 {
private String name = "name";
@@ -1953,7 +1942,7 @@ public class SpelReproTests extends AbstractExpressionTests {
}
static class SPR11142 {
static class Spr11142 {
public String isSomething() {
return "";
@@ -1983,4 +1972,29 @@ public class SpelReproTests extends AbstractExpressionTests {
}
}
static class Spr11445Class implements BeanResolver {
private final AtomicInteger counter = new AtomicInteger();
public int echo(int invocation) {
return invocation;
}
public int parameter() {
return counter.incrementAndGet();
}
@Override
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
return beanName.equals("bean") ? this : null;
}
}
public static class MapWithConstant extends HashMap {
public static final int X = 1;
}
}