Polishing

This commit is contained in:
Sam Brannen
2024-04-25 14:00:35 +03:00
parent 8f579b3144
commit de9ce800bf
3 changed files with 30 additions and 41 deletions

View File

@@ -33,6 +33,25 @@ import org.springframework.util.ObjectUtils;
*/
abstract class AstUtils {
/**
* Determine the set of accessors that should be used to try to access an
* element on the specified target object.
* <p>Delegates to {@link #getAccessorsToTry(Class, List)} with the type of
* the supplied target object.
* @param targetObject the object upon which element access is being attempted
* @param accessors the list of element accessors to process
* @return a list of accessors that should be tried in order to access the
* element on the specified target type, or an empty list if no suitable
* accessor could be found
* @since 6.2
*/
static <T extends TargetedAccessor> List<T> getAccessorsToTry(
@Nullable Object targetObject, List<T> accessors) {
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
return getAccessorsToTry(targetType, accessors);
}
/**
* Determine the set of accessors that should be used to try to access an
* element on the specified target type.

View File

@@ -246,7 +246,8 @@ public class Indexer extends SpelNodeImpl {
// Check for a custom IndexAccessor.
EvaluationContext evalContext = state.getEvaluationContext();
List<IndexAccessor> accessorsToTry = getIndexAccessorsToTry(target, evalContext.getIndexAccessors());
List<IndexAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(target, evalContext.getIndexAccessors());
if (accessMode.supportsReads) {
try {
for (IndexAccessor indexAccessor : accessorsToTry) {
@@ -444,22 +445,6 @@ public class Indexer extends SpelNodeImpl {
return (obj instanceof Class<?> clazz ? clazz : obj.getClass());
}
/**
* Determine the set of index accessors that should be used to try to access
* an index on the specified context object.
* <p>Delegates to {@link AstUtils#getAccessorsToTry(Class, List)}.
* @param targetObject the object upon which index access is being attempted
* @param indexAccessors the list of index accessors to process
* @return a list of accessors that should be tried in order to access the
* index, or an empty list if no suitable accessor could be found
*/
private static List<IndexAccessor> getIndexAccessorsToTry(
@Nullable Object targetObject, List<IndexAccessor> indexAccessors) {
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
return AstUtils.getAccessorsToTry(targetType, indexAccessors);
}
/**
* Tracks state when the {@code Indexer} is being used as a {@link PropertyAccessor}.
@@ -740,8 +725,8 @@ public class Indexer extends SpelNodeImpl {
// we need to reset our cached state.
Indexer.this.cachedPropertyReadState = null;
}
List<PropertyAccessor> accessorsToTry = AstUtils.getAccessorsToTry(targetType,
this.evaluationContext.getPropertyAccessors());
List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canRead(this.evaluationContext, this.targetObject, this.name)) {
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) {
@@ -783,8 +768,8 @@ public class Indexer extends SpelNodeImpl {
// we need to reset our cached state.
Indexer.this.cachedPropertyWriteState = null;
}
List<PropertyAccessor> accessorsToTry = AstUtils.getAccessorsToTry(targetType,
this.evaluationContext.getPropertyAccessors());
List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(this.evaluationContext, this.targetObject, this.name)) {
accessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
@@ -998,7 +983,7 @@ public class Indexer extends SpelNodeImpl {
Indexer.this.cachedIndexReadState = null;
}
List<IndexAccessor> accessorsToTry =
getIndexAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
for (IndexAccessor indexAccessor : accessorsToTry) {
if (indexAccessor.canRead(this.evaluationContext, this.target, this.index)) {
TypedValue result = indexAccessor.read(this.evaluationContext, this.target, this.index);
@@ -1050,7 +1035,7 @@ public class Indexer extends SpelNodeImpl {
Indexer.this.cachedIndexWriteState = null;
}
List<IndexAccessor> accessorsToTry =
getIndexAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
for (IndexAccessor indexAccessor : accessorsToTry) {
if (indexAccessor.canWrite(this.evaluationContext, this.target, this.index)) {
indexAccessor.write(this.evaluationContext, this.target, this.index, newValue);

View File

@@ -201,7 +201,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
}
List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AstUtils.getAccessorsToTry(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
@@ -259,7 +259,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
}
List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
try {
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(evalContext, targetObject, name)) {
@@ -284,7 +284,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
Object targetObject = contextObject.getValue();
if (targetObject != null) {
List<PropertyAccessor> accessorsToTry =
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
try {
if (accessor.canWrite(evalContext, targetObject, name)) {
@@ -299,21 +299,6 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
return false;
}
/**
* 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#getAccessorsToTry(Class, List)}.
* @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 targetObject, List<PropertyAccessor> propertyAccessors) {
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
return AstUtils.getAccessorsToTry(targetType, propertyAccessors);
}
@Override
public boolean isCompilable() {
return (this.cachedReadAccessor instanceof CompilablePropertyAccessor compilablePropertyAccessor &&