Skip to content

Commit e547313

Browse files
committed
Sync MapAccessor implementations
1 parent 4fa9781 commit e547313

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434
*/
3535
class CompilableMapAccessor implements CompilablePropertyAccessor {
3636

37+
private final boolean allowWrite;
38+
39+
/**
40+
* Create a new map accessor for reading as well as writing.
41+
* @since 6.2
42+
* @see #CompilableMapAccessor(boolean)
43+
*/
44+
public CompilableMapAccessor() {
45+
this(true);
46+
}
47+
48+
/**
49+
* Create a new map accessor for reading and possibly also writing.
50+
* @param allowWrite whether to allow write operations on a target instance
51+
* @since 6.2
52+
* @see #canWrite
53+
*/
54+
public CompilableMapAccessor(boolean allowWrite) {
55+
this.allowWrite = allowWrite;
56+
}
57+
3758
@Override
3859
public Class<?>[] getSpecificTargetClasses() {
3960
return new Class<?>[] {Map.class};
@@ -57,15 +78,15 @@ public TypedValue read(EvaluationContext context, @Nullable Object target, Strin
5778

5879
@Override
5980
public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
60-
return true;
81+
return (this.allowWrite && target instanceof Map);
6182
}
6283

6384
@Override
6485
@SuppressWarnings("unchecked")
6586
public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue)
6687
throws AccessException {
6788

68-
Assert.state(target instanceof Map, "Target must be a Map");
89+
Assert.state(target instanceof Map, "Target must be of type Map");
6990
Map<Object, Object> map = (Map<Object, Object>) target;
7091
map.put(name, newValue);
7192
}
@@ -90,7 +111,7 @@ public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
90111
CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
91112
}
92113
mv.visitLdcInsn(propertyName);
93-
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
114+
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
94115
}
95116

96117

0 commit comments

Comments
 (0)