Skip to content

Commit f47352f

Browse files
committed
Polish PropertyOrFieldReference
1 parent c1ed504 commit f47352f

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java

+26-23
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
190190
if (accessorToUse != null) {
191191
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
192192
try {
193-
return accessorToUse.read(evalContext, contextObject.getValue(), name);
193+
return accessorToUse.read(evalContext, targetObject, name);
194194
}
195195
catch (Exception ex) {
196196
// This is OK - it may have gone stale due to a class change,
@@ -201,19 +201,19 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
201201
}
202202

203203
List<PropertyAccessor> accessorsToTry =
204-
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
204+
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
205205
// Go through the accessors that may be able to resolve it. If they are a cacheable accessor then
206206
// get the accessor and use it. If they are not cacheable but report they can read the property
207207
// then ask them to read it
208208
try {
209209
for (PropertyAccessor accessor : accessorsToTry) {
210-
if (accessor.canRead(evalContext, contextObject.getValue(), name)) {
210+
if (accessor.canRead(evalContext, targetObject, name)) {
211211
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) {
212212
accessor = reflectivePropertyAccessor.createOptimalAccessor(
213-
evalContext, contextObject.getValue(), name);
213+
evalContext, targetObject, name);
214214
}
215215
this.cachedReadAccessor = accessor;
216-
return accessor.read(evalContext, contextObject.getValue(), name);
216+
return accessor.read(evalContext, targetObject, name);
217217
}
218218
}
219219
}
@@ -234,18 +234,20 @@ private void writeProperty(
234234
TypedValue contextObject, EvaluationContext evalContext, String name, @Nullable Object newValue)
235235
throws EvaluationException {
236236

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);
242244
}
243245

244246
PropertyAccessor accessorToUse = this.cachedWriteAccessor;
245247
if (accessorToUse != null) {
246248
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
247249
try {
248-
accessorToUse.write(evalContext, contextObject.getValue(), name, newValue);
250+
accessorToUse.write(evalContext, targetObject, name, newValue);
249251
return;
250252
}
251253
catch (Exception ex) {
@@ -257,12 +259,12 @@ private void writeProperty(
257259
}
258260

259261
List<PropertyAccessor> accessorsToTry =
260-
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
262+
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
261263
try {
262264
for (PropertyAccessor accessor : accessorsToTry) {
263-
if (accessor.canWrite(evalContext, contextObject.getValue(), name)) {
265+
if (accessor.canWrite(evalContext, targetObject, name)) {
264266
this.cachedWriteAccessor = accessor;
265-
accessor.write(evalContext, contextObject.getValue(), name, newValue);
267+
accessor.write(evalContext, targetObject, name, newValue);
266268
return;
267269
}
268270
}
@@ -273,19 +275,19 @@ private void writeProperty(
273275
}
274276

275277
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name,
276-
FormatHelper.formatClassNameForMessage(getObjectClass(contextObject.getValue())));
278+
FormatHelper.formatClassNameForMessage(getObjectClass(targetObject)));
277279
}
278280

279281
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext)
280282
throws EvaluationException {
281283

282-
Object value = contextObject.getValue();
283-
if (value != null) {
284+
Object targetObject = contextObject.getValue();
285+
if (targetObject != null) {
284286
List<PropertyAccessor> accessorsToTry =
285-
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
287+
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
286288
for (PropertyAccessor accessor : accessorsToTry) {
287289
try {
288-
if (accessor.canWrite(evalContext, value, name)) {
290+
if (accessor.canWrite(evalContext, targetObject, name)) {
289291
return true;
290292
}
291293
}
@@ -301,13 +303,14 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
301303
* Determine the set of property accessors that should be used to try to
302304
* access a property on the specified context object.
303305
* <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
306309
*/
307310
private List<PropertyAccessor> getPropertyAccessorsToTry(
308-
@Nullable Object contextObject, List<PropertyAccessor> propertyAccessors) {
311+
@Nullable Object targetObject, List<PropertyAccessor> propertyAccessors) {
309312

310-
Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);
313+
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
311314
return AstUtils.getPropertyAccessorsToTry(targetType, propertyAccessors);
312315
}
313316

0 commit comments

Comments
 (0)