Skip to content

Commit

Permalink
Polish PropertyOrFieldReference
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Mar 26, 2024
1 parent c1ed504 commit f47352f
Showing 1 changed file with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
if (accessorToUse != null) {
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
try {
return accessorToUse.read(evalContext, contextObject.getValue(), name);
return accessorToUse.read(evalContext, targetObject, name);
}
catch (Exception ex) {
// This is OK - it may have gone stale due to a class change,
Expand All @@ -201,19 +201,19 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
}

List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
// Go through the accessors that may be able to resolve it. If they are a cacheable accessor then
// get the accessor and use it. If they are not cacheable but report they can read the property
// then ask them to read it
try {
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canRead(evalContext, contextObject.getValue(), name)) {
if (accessor.canRead(evalContext, targetObject, name)) {
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) {
accessor = reflectivePropertyAccessor.createOptimalAccessor(
evalContext, contextObject.getValue(), name);
evalContext, targetObject, name);
}
this.cachedReadAccessor = accessor;
return accessor.read(evalContext, contextObject.getValue(), name);
return accessor.read(evalContext, targetObject, name);
}
}
}
Expand All @@ -234,18 +234,20 @@ private void writeProperty(
TypedValue contextObject, EvaluationContext evalContext, String name, @Nullable Object newValue)
throws EvaluationException {

if (contextObject.getValue() == null && isNullSafe()) {
return;
}
if (contextObject.getValue() == null) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, name);
Object targetObject = contextObject.getValue();
if (targetObject == null) {
if (isNullSafe()) {
return;
}
throw new SpelEvaluationException(
getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, name);
}

PropertyAccessor accessorToUse = this.cachedWriteAccessor;
if (accessorToUse != null) {
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
try {
accessorToUse.write(evalContext, contextObject.getValue(), name, newValue);
accessorToUse.write(evalContext, targetObject, name, newValue);
return;
}
catch (Exception ex) {
Expand All @@ -257,12 +259,12 @@ private void writeProperty(
}

List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
try {
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(evalContext, contextObject.getValue(), name)) {
if (accessor.canWrite(evalContext, targetObject, name)) {
this.cachedWriteAccessor = accessor;
accessor.write(evalContext, contextObject.getValue(), name, newValue);
accessor.write(evalContext, targetObject, name, newValue);
return;
}
}
Expand All @@ -273,19 +275,19 @@ private void writeProperty(
}

throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name,
FormatHelper.formatClassNameForMessage(getObjectClass(contextObject.getValue())));
FormatHelper.formatClassNameForMessage(getObjectClass(targetObject)));
}

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

Object value = contextObject.getValue();
if (value != null) {
Object targetObject = contextObject.getValue();
if (targetObject != null) {
List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
try {
if (accessor.canWrite(evalContext, value, name)) {
if (accessor.canWrite(evalContext, targetObject, name)) {
return true;
}
}
Expand All @@ -301,13 +303,14 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
* Determine the set of property accessors that should be used to try to
* access a property on the specified context object.
* <p>Delegates to {@link AstUtils#getPropertyAccessorsToTry(Class, List)}.
* @param contextObject the object upon which property access is being attempted
* @return a list of accessors that should be tried in order to access the property
* @param targetObject the object upon which property access is being attempted
* @return a list of accessors that should be tried in order to access the
* property, or an empty list if no suitable accessor could be found
*/
private List<PropertyAccessor> getPropertyAccessorsToTry(
@Nullable Object contextObject, List<PropertyAccessor> propertyAccessors) {
@Nullable Object targetObject, List<PropertyAccessor> propertyAccessors) {

Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
return AstUtils.getPropertyAccessorsToTry(targetType, propertyAccessors);
}

Expand Down

0 comments on commit f47352f

Please sign in to comment.