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 { 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 @Override
public Class<?>[] getSpecificTargetClasses() { public Class<?>[] getSpecificTargetClasses() {
return new Class<?>[] {Map.class}; return new Class<?>[] {Map.class};
@ -57,7 +78,7 @@ class CompilableMapAccessor implements CompilablePropertyAccessor {
@Override @Override
public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException { public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
return true; return (this.allowWrite && target instanceof Map);
} }
@Override @Override
@ -65,7 +86,7 @@ class CompilableMapAccessor implements CompilablePropertyAccessor {
public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue)
throws AccessException { 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<Object, Object> map = (Map<Object, Object>) target;
map.put(name, newValue); map.put(name, newValue);
} }