diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java b/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java index 3557dfa98de..f96d023e546 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java @@ -34,6 +34,27 @@ import org.springframework.util.Assert; */ class CompilableMapAccessor implements CompilablePropertyAccessor { + private final boolean allowWrite; + + /** + * Create a new map accessor for reading as well as writing. + * @since 6.2 + * @see #CompilableMapAccessor(boolean) + */ + public CompilableMapAccessor() { + this(true); + } + + /** + * Create a new map accessor for reading and possibly also writing. + * @param allowWrite whether to allow write operations on a target instance + * @since 6.2 + * @see #canWrite + */ + public CompilableMapAccessor(boolean allowWrite) { + this.allowWrite = allowWrite; + } + @Override public Class[] getSpecificTargetClasses() { return new Class[] {Map.class}; @@ -57,7 +78,7 @@ class CompilableMapAccessor implements CompilablePropertyAccessor { @Override public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException { - return true; + return (this.allowWrite && target instanceof Map); } @Override @@ -65,7 +86,7 @@ class CompilableMapAccessor implements CompilablePropertyAccessor { public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) throws AccessException { - Assert.state(target instanceof Map, "Target must be a Map"); + Assert.state(target instanceof Map, "Target must be of type Map"); Map map = (Map) target; map.put(name, newValue); } @@ -90,7 +111,7 @@ class CompilableMapAccessor implements CompilablePropertyAccessor { CodeFlow.insertCheckCast(mv, "Ljava/util/Map"); } mv.visitLdcInsn(propertyName); - mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true); + mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); }