@@ -190,7 +190,7 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
190
190
if (accessorToUse != null ) {
191
191
if (evalContext .getPropertyAccessors ().contains (accessorToUse )) {
192
192
try {
193
- return accessorToUse .read (evalContext , contextObject . getValue () , name );
193
+ return accessorToUse .read (evalContext , targetObject , name );
194
194
}
195
195
catch (Exception ex ) {
196
196
// This is OK - it may have gone stale due to a class change,
@@ -201,19 +201,19 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
201
201
}
202
202
203
203
List <PropertyAccessor > accessorsToTry =
204
- getPropertyAccessorsToTry (contextObject . getValue () , evalContext .getPropertyAccessors ());
204
+ getPropertyAccessorsToTry (targetObject , evalContext .getPropertyAccessors ());
205
205
// Go through the accessors that may be able to resolve it. If they are a cacheable accessor then
206
206
// get the accessor and use it. If they are not cacheable but report they can read the property
207
207
// then ask them to read it
208
208
try {
209
209
for (PropertyAccessor accessor : accessorsToTry ) {
210
- if (accessor .canRead (evalContext , contextObject . getValue () , name )) {
210
+ if (accessor .canRead (evalContext , targetObject , name )) {
211
211
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor ) {
212
212
accessor = reflectivePropertyAccessor .createOptimalAccessor (
213
- evalContext , contextObject . getValue () , name );
213
+ evalContext , targetObject , name );
214
214
}
215
215
this .cachedReadAccessor = accessor ;
216
- return accessor .read (evalContext , contextObject . getValue () , name );
216
+ return accessor .read (evalContext , targetObject , name );
217
217
}
218
218
}
219
219
}
@@ -234,18 +234,20 @@ private void writeProperty(
234
234
TypedValue contextObject , EvaluationContext evalContext , String name , @ Nullable Object newValue )
235
235
throws EvaluationException {
236
236
237
- if (contextObject .getValue () == null && isNullSafe ()) {
238
- return ;
239
- }
240
- if (contextObject .getValue () == null ) {
241
- throw new SpelEvaluationException (getStartPosition (), SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL , name );
237
+ Object targetObject = contextObject .getValue ();
238
+ if (targetObject == null ) {
239
+ if (isNullSafe ()) {
240
+ return ;
241
+ }
242
+ throw new SpelEvaluationException (
243
+ getStartPosition (), SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL , name );
242
244
}
243
245
244
246
PropertyAccessor accessorToUse = this .cachedWriteAccessor ;
245
247
if (accessorToUse != null ) {
246
248
if (evalContext .getPropertyAccessors ().contains (accessorToUse )) {
247
249
try {
248
- accessorToUse .write (evalContext , contextObject . getValue () , name , newValue );
250
+ accessorToUse .write (evalContext , targetObject , name , newValue );
249
251
return ;
250
252
}
251
253
catch (Exception ex ) {
@@ -257,12 +259,12 @@ private void writeProperty(
257
259
}
258
260
259
261
List <PropertyAccessor > accessorsToTry =
260
- getPropertyAccessorsToTry (contextObject . getValue () , evalContext .getPropertyAccessors ());
262
+ getPropertyAccessorsToTry (targetObject , evalContext .getPropertyAccessors ());
261
263
try {
262
264
for (PropertyAccessor accessor : accessorsToTry ) {
263
- if (accessor .canWrite (evalContext , contextObject . getValue () , name )) {
265
+ if (accessor .canWrite (evalContext , targetObject , name )) {
264
266
this .cachedWriteAccessor = accessor ;
265
- accessor .write (evalContext , contextObject . getValue () , name , newValue );
267
+ accessor .write (evalContext , targetObject , name , newValue );
266
268
return ;
267
269
}
268
270
}
@@ -273,19 +275,19 @@ private void writeProperty(
273
275
}
274
276
275
277
throw new SpelEvaluationException (getStartPosition (), SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE , name ,
276
- FormatHelper .formatClassNameForMessage (getObjectClass (contextObject . getValue () )));
278
+ FormatHelper .formatClassNameForMessage (getObjectClass (targetObject )));
277
279
}
278
280
279
281
public boolean isWritableProperty (String name , TypedValue contextObject , EvaluationContext evalContext )
280
282
throws EvaluationException {
281
283
282
- Object value = contextObject .getValue ();
283
- if (value != null ) {
284
+ Object targetObject = contextObject .getValue ();
285
+ if (targetObject != null ) {
284
286
List <PropertyAccessor > accessorsToTry =
285
- getPropertyAccessorsToTry (contextObject . getValue () , evalContext .getPropertyAccessors ());
287
+ getPropertyAccessorsToTry (targetObject , evalContext .getPropertyAccessors ());
286
288
for (PropertyAccessor accessor : accessorsToTry ) {
287
289
try {
288
- if (accessor .canWrite (evalContext , value , name )) {
290
+ if (accessor .canWrite (evalContext , targetObject , name )) {
289
291
return true ;
290
292
}
291
293
}
@@ -301,13 +303,14 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
301
303
* Determine the set of property accessors that should be used to try to
302
304
* access a property on the specified context object.
303
305
* <p>Delegates to {@link AstUtils#getPropertyAccessorsToTry(Class, List)}.
304
- * @param contextObject the object upon which property access is being attempted
305
- * @return a list of accessors that should be tried in order to access the property
306
+ * @param targetObject the object upon which property access is being attempted
307
+ * @return a list of accessors that should be tried in order to access the
308
+ * property, or an empty list if no suitable accessor could be found
306
309
*/
307
310
private List <PropertyAccessor > getPropertyAccessorsToTry (
308
- @ Nullable Object contextObject , List <PropertyAccessor > propertyAccessors ) {
311
+ @ Nullable Object targetObject , List <PropertyAccessor > propertyAccessors ) {
309
312
310
- Class <?> targetType = (contextObject != null ? contextObject .getClass () : null );
313
+ Class <?> targetType = (targetObject != null ? targetObject .getClass () : null );
311
314
return AstUtils .getPropertyAccessorsToTry (targetType , propertyAccessors );
312
315
}
313
316
0 commit comments