Skip to content

Commit de9ce80

Browse files
committed
Polishing
1 parent 8f579b3 commit de9ce80

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333
*/
3434
abstract class AstUtils {
3535

36+
/**
37+
* Determine the set of accessors that should be used to try to access an
38+
* element on the specified target object.
39+
* <p>Delegates to {@link #getAccessorsToTry(Class, List)} with the type of
40+
* the supplied target object.
41+
* @param targetObject the object upon which element access is being attempted
42+
* @param accessors the list of element accessors to process
43+
* @return a list of accessors that should be tried in order to access the
44+
* element on the specified target type, or an empty list if no suitable
45+
* accessor could be found
46+
* @since 6.2
47+
*/
48+
static <T extends TargetedAccessor> List<T> getAccessorsToTry(
49+
@Nullable Object targetObject, List<T> accessors) {
50+
51+
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
52+
return getAccessorsToTry(targetType, accessors);
53+
}
54+
3655
/**
3756
* Determine the set of accessors that should be used to try to access an
3857
* element on the specified target type.

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ else if (target instanceof Collection<?> collection) {
246246

247247
// Check for a custom IndexAccessor.
248248
EvaluationContext evalContext = state.getEvaluationContext();
249-
List<IndexAccessor> accessorsToTry = getIndexAccessorsToTry(target, evalContext.getIndexAccessors());
249+
List<IndexAccessor> accessorsToTry =
250+
AstUtils.getAccessorsToTry(target, evalContext.getIndexAccessors());
250251
if (accessMode.supportsReads) {
251252
try {
252253
for (IndexAccessor indexAccessor : accessorsToTry) {
@@ -444,22 +445,6 @@ private static Class<?> getObjectType(Object obj) {
444445
return (obj instanceof Class<?> clazz ? clazz : obj.getClass());
445446
}
446447

447-
/**
448-
* Determine the set of index accessors that should be used to try to access
449-
* an index on the specified context object.
450-
* <p>Delegates to {@link AstUtils#getAccessorsToTry(Class, List)}.
451-
* @param targetObject the object upon which index access is being attempted
452-
* @param indexAccessors the list of index accessors to process
453-
* @return a list of accessors that should be tried in order to access the
454-
* index, or an empty list if no suitable accessor could be found
455-
*/
456-
private static List<IndexAccessor> getIndexAccessorsToTry(
457-
@Nullable Object targetObject, List<IndexAccessor> indexAccessors) {
458-
459-
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
460-
return AstUtils.getAccessorsToTry(targetType, indexAccessors);
461-
}
462-
463448

464449
/**
465450
* Tracks state when the {@code Indexer} is being used as a {@link PropertyAccessor}.
@@ -740,8 +725,8 @@ public TypedValue getValue() {
740725
// we need to reset our cached state.
741726
Indexer.this.cachedPropertyReadState = null;
742727
}
743-
List<PropertyAccessor> accessorsToTry = AstUtils.getAccessorsToTry(targetType,
744-
this.evaluationContext.getPropertyAccessors());
728+
List<PropertyAccessor> accessorsToTry =
729+
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
745730
for (PropertyAccessor accessor : accessorsToTry) {
746731
if (accessor.canRead(this.evaluationContext, this.targetObject, this.name)) {
747732
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) {
@@ -783,8 +768,8 @@ public void setValue(@Nullable Object newValue) {
783768
// we need to reset our cached state.
784769
Indexer.this.cachedPropertyWriteState = null;
785770
}
786-
List<PropertyAccessor> accessorsToTry = AstUtils.getAccessorsToTry(targetType,
787-
this.evaluationContext.getPropertyAccessors());
771+
List<PropertyAccessor> accessorsToTry =
772+
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
788773
for (PropertyAccessor accessor : accessorsToTry) {
789774
if (accessor.canWrite(this.evaluationContext, this.targetObject, this.name)) {
790775
accessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
@@ -998,7 +983,7 @@ public TypedValue getValue() {
998983
Indexer.this.cachedIndexReadState = null;
999984
}
1000985
List<IndexAccessor> accessorsToTry =
1001-
getIndexAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
986+
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
1002987
for (IndexAccessor indexAccessor : accessorsToTry) {
1003988
if (indexAccessor.canRead(this.evaluationContext, this.target, this.index)) {
1004989
TypedValue result = indexAccessor.read(this.evaluationContext, this.target, this.index);
@@ -1050,7 +1035,7 @@ public void setValue(@Nullable Object newValue) {
10501035
Indexer.this.cachedIndexWriteState = null;
10511036
}
10521037
List<IndexAccessor> accessorsToTry =
1053-
getIndexAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
1038+
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
10541039
for (IndexAccessor indexAccessor : accessorsToTry) {
10551040
if (indexAccessor.canWrite(this.evaluationContext, this.target, this.index)) {
10561041
indexAccessor.write(this.evaluationContext, this.target, this.index, newValue);

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
201201
}
202202

203203
List<PropertyAccessor> accessorsToTry =
204-
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
204+
AstUtils.getAccessorsToTry(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
@@ -259,7 +259,7 @@ private void writeProperty(
259259
}
260260

261261
List<PropertyAccessor> accessorsToTry =
262-
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
262+
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
263263
try {
264264
for (PropertyAccessor accessor : accessorsToTry) {
265265
if (accessor.canWrite(evalContext, targetObject, name)) {
@@ -284,7 +284,7 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
284284
Object targetObject = contextObject.getValue();
285285
if (targetObject != null) {
286286
List<PropertyAccessor> accessorsToTry =
287-
getPropertyAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
287+
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
288288
for (PropertyAccessor accessor : accessorsToTry) {
289289
try {
290290
if (accessor.canWrite(evalContext, targetObject, name)) {
@@ -299,21 +299,6 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
299299
return false;
300300
}
301301

302-
/**
303-
* Determine the set of property accessors that should be used to try to
304-
* access a property on the specified context object.
305-
* <p>Delegates to {@link AstUtils#getAccessorsToTry(Class, List)}.
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
309-
*/
310-
private List<PropertyAccessor> getPropertyAccessorsToTry(
311-
@Nullable Object targetObject, List<PropertyAccessor> propertyAccessors) {
312-
313-
Class<?> targetType = (targetObject != null ? targetObject.getClass() : null);
314-
return AstUtils.getAccessorsToTry(targetType, propertyAccessors);
315-
}
316-
317302
@Override
318303
public boolean isCompilable() {
319304
return (this.cachedReadAccessor instanceof CompilablePropertyAccessor compilablePropertyAccessor &&

0 commit comments

Comments
 (0)