34
34
*/
35
35
class CompilableMapAccessor implements CompilablePropertyAccessor {
36
36
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
+
37
58
@ Override
38
59
public Class <?>[] getSpecificTargetClasses () {
39
60
return new Class <?>[] {Map .class };
@@ -57,15 +78,15 @@ public TypedValue read(EvaluationContext context, @Nullable Object target, Strin
57
78
58
79
@ Override
59
80
public boolean canWrite (EvaluationContext context , @ Nullable Object target , String name ) throws AccessException {
60
- return true ;
81
+ return ( this . allowWrite && target instanceof Map ) ;
61
82
}
62
83
63
84
@ Override
64
85
@ SuppressWarnings ("unchecked" )
65
86
public void write (EvaluationContext context , @ Nullable Object target , String name , @ Nullable Object newValue )
66
87
throws AccessException {
67
88
68
- Assert .state (target instanceof Map , "Target must be a Map" );
89
+ Assert .state (target instanceof Map , "Target must be of type Map" );
69
90
Map <Object , Object > map = (Map <Object , Object >) target ;
70
91
map .put (name , newValue );
71
92
}
@@ -90,7 +111,7 @@ public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
90
111
CodeFlow .insertCheckCast (mv , "Ljava/util/Map" );
91
112
}
92
113
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 );
94
115
}
95
116
96
117
0 commit comments