Sync MapAccessor implementations

This commit is contained in:
Sam Brannen 2024-07-23 18:27:52 +03:00
parent 4fa9781549
commit e547313fa6
1 changed files with 24 additions and 3 deletions

View File

@ -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<Object, Object> map = (Map<Object, Object>) 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);
}