Skip to content

Commit 4684a17

Browse files
committed
Polishing
1 parent f3123d8 commit 4684a17

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean canWrite(EvaluationContext context, @Nullable Object target, Stri
8989
public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue)
9090
throws AccessException {
9191

92-
Assert.state(target instanceof Map, "Target must be a Map");
92+
Assert.state(target instanceof Map, "Target must be of type Map");
9393
Map<Object, Object> map = (Map<Object, Object>) target;
9494
map.put(name, newValue);
9595
}
@@ -114,7 +114,7 @@ public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
114114
CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
115115
}
116116
mv.visitLdcInsn(propertyName);
117-
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
117+
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
118118
}
119119

120120

spring-context/src/test/java/org/springframework/context/expression/MapAccessorTests.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.context.expression;
1818

1919
import java.util.HashMap;
20+
import java.util.List;
2021
import java.util.Map;
2122

2223
import org.junit.jupiter.api.Test;
@@ -36,80 +37,87 @@
3637
class MapAccessorTests {
3738

3839
@Test
39-
void mapAccessorCompilable() {
40+
void compilationSupport() {
4041
Map<String, Object> testMap = getSimpleTestMap();
4142
StandardEvaluationContext sec = new StandardEvaluationContext();
4243
sec.addPropertyAccessor(new MapAccessor());
4344
SpelExpressionParser sep = new SpelExpressionParser();
4445

4546
// basic
4647
Expression ex = sep.parseExpression("foo");
47-
assertThat(ex.getValue(sec,testMap)).isEqualTo("bar");
48+
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar");
4849
assertThat(SpelCompiler.compile(ex)).isTrue();
49-
assertThat(ex.getValue(sec,testMap)).isEqualTo("bar");
50+
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar");
5051

5152
// compound expression
5253
ex = sep.parseExpression("foo.toUpperCase()");
53-
assertThat(ex.getValue(sec,testMap)).isEqualTo("BAR");
54+
assertThat(ex.getValue(sec, testMap)).isEqualTo("BAR");
5455
assertThat(SpelCompiler.compile(ex)).isTrue();
55-
assertThat(ex.getValue(sec,testMap)).isEqualTo("BAR");
56+
assertThat(ex.getValue(sec, testMap)).isEqualTo("BAR");
5657

5758
// nested map
58-
Map<String,Map<String,Object>> nestedMap = getNestedTestMap();
59+
Map<String, Map<String, Object>> nestedMap = getNestedTestMap();
5960
ex = sep.parseExpression("aaa.foo.toUpperCase()");
60-
assertThat(ex.getValue(sec,nestedMap)).isEqualTo("BAR");
61+
assertThat(ex.getValue(sec, nestedMap)).isEqualTo("BAR");
6162
assertThat(SpelCompiler.compile(ex)).isTrue();
62-
assertThat(ex.getValue(sec,nestedMap)).isEqualTo("BAR");
63+
assertThat(ex.getValue(sec, nestedMap)).isEqualTo("BAR");
6364

6465
// avoiding inserting checkcast because first part of expression returns a Map
6566
ex = sep.parseExpression("getMap().foo");
6667
MapGetter mapGetter = new MapGetter();
67-
assertThat(ex.getValue(sec,mapGetter)).isEqualTo("bar");
68+
assertThat(ex.getValue(sec, mapGetter)).isEqualTo("bar");
6869
assertThat(SpelCompiler.compile(ex)).isTrue();
69-
assertThat(ex.getValue(sec,mapGetter)).isEqualTo("bar");
70+
assertThat(ex.getValue(sec, mapGetter)).isEqualTo("bar");
7071

7172
// basic isWritable
7273
ex = sep.parseExpression("foo");
73-
assertThat(ex.isWritable(sec,testMap)).isTrue();
74+
assertThat(ex.isWritable(sec, testMap)).isTrue();
7475

7576
// basic write
7677
ex = sep.parseExpression("foo2");
7778
ex.setValue(sec, testMap, "bar2");
78-
assertThat(ex.getValue(sec,testMap)).isEqualTo("bar2");
79+
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2");
7980
assertThat(SpelCompiler.compile(ex)).isTrue();
80-
assertThat(ex.getValue(sec,testMap)).isEqualTo("bar2");
81+
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2");
8182
}
8283

8384
@Test
84-
void mapAccessorNotWritable() {
85+
void isWritable() {
8586
Map<String, Object> testMap = getSimpleTestMap();
8687
StandardEvaluationContext sec = new StandardEvaluationContext();
87-
sec.addPropertyAccessor(new MapAccessor(false));
8888
SpelExpressionParser sep = new SpelExpressionParser();
8989
Expression ex = sep.parseExpression("foo");
90+
91+
assertThat(ex.isWritable(sec, testMap)).isFalse();
92+
93+
sec.setPropertyAccessors(List.of(new MapAccessor(true)));
94+
assertThat(ex.isWritable(sec, testMap)).isTrue();
95+
96+
sec.setPropertyAccessors(List.of(new MapAccessor(false)));
9097
assertThat(ex.isWritable(sec, testMap)).isFalse();
9198
}
9299

100+
101+
@SuppressWarnings({"rawtypes", "unchecked"})
93102
public static class MapGetter {
94-
Map<String,Object> map = new HashMap<>();
103+
Map map = new HashMap<>();
95104

96105
public MapGetter() {
97-
map.put("foo", "bar");
106+
this.map.put("foo", "bar");
98107
}
99108

100-
@SuppressWarnings("rawtypes")
101109
public Map getMap() {
102-
return map;
110+
return this.map;
103111
}
104112
}
105113

106-
public Map<String,Object> getSimpleTestMap() {
114+
private static Map<String,Object> getSimpleTestMap() {
107115
Map<String,Object> map = new HashMap<>();
108116
map.put("foo","bar");
109117
return map;
110118
}
111119

112-
public Map<String,Map<String,Object>> getNestedTestMap() {
120+
private static Map<String,Map<String,Object>> getNestedTestMap() {
113121
Map<String,Object> map = new HashMap<>();
114122
map.put("foo","bar");
115123
Map<String,Map<String,Object>> map2 = new HashMap<>();

0 commit comments

Comments
 (0)