Skip to content

Commit 4b0a048

Browse files
committed
Polish SpEL internals and remove duplicate code
1 parent 57632f9 commit 4b0a048

File tree

4 files changed

+40
-65
lines changed

4 files changed

+40
-65
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/ConstructorExecutor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Andy Clement
3535
* @author Sam Brannen
3636
* @since 3.0
37-
* @see MethodResolver
37+
* @see ConstructorResolver
3838
* @see MethodExecutor
3939
*/
4040
@FunctionalInterface

Diff for: spring-expression/src/main/java/org/springframework/expression/PropertyAccessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface PropertyAccessor {
4545
/**
4646
* Return an array of classes for which this property accessor should be called.
4747
* <p>Returning {@code null} indicates this is a general property accessor that
48-
* can be called in an attempt to resolve a property on any type.
48+
* can be called in an attempt to access a property on any type.
4949
* @return an array of classes that this property accessor is suitable for
5050
* (or {@code null} if a general property accessor)
5151
*/
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,55 +21,59 @@
2121

2222
import org.springframework.expression.PropertyAccessor;
2323
import org.springframework.lang.Nullable;
24+
import org.springframework.util.ObjectUtils;
2425

2526
/**
26-
* Utilities methods for use in the Ast classes.
27+
* Utility methods for use in the AST classes.
2728
*
2829
* @author Andy Clement
2930
* @since 3.0.2
3031
*/
3132
public abstract class AstUtils {
3233

3334
/**
34-
* Determines the set of property resolvers that should be used to try and access a
35-
* property on the specified target type. The resolvers are considered to be in an
36-
* ordered list, however in the returned list any that are exact matches for the input
37-
* target type (as opposed to 'general' resolvers that could work for any type) are
38-
* placed at the start of the list. In addition, there are specific resolvers that
39-
* exactly name the class in question and resolvers that name a specific class but it
40-
* is a supertype of the class we have. These are put at the end of the specific resolvers
41-
* set and will be tried after exactly matching accessors but before generic accessors.
35+
* Determine the set of property accessors that should be used to try to
36+
* access a property on the specified target type.
37+
* <p>The accessors are considered to be in an ordered list; however, in the
38+
* returned list any accessors that are exact matches for the input target
39+
* type (as opposed to 'general' accessors that could work for any type) are
40+
* placed at the start of the list. In addition, if there are specific
41+
* accessors that exactly name the class in question and accessors that name
42+
* a specific class which is a supertype of the class in question, the latter
43+
* are put at the end of the specific accessors set and will be tried after
44+
* exactly matching accessors but before generic accessors.
4245
* @param targetType the type upon which property access is being attempted
43-
* @return a list of resolvers that should be tried in order to access the property
46+
* @param propertyAccessors the list of property accessors to process
47+
* @return a list of accessors that should be tried in order to access the property
4448
*/
4549
public static List<PropertyAccessor> getPropertyAccessorsToTry(
4650
@Nullable Class<?> targetType, List<PropertyAccessor> propertyAccessors) {
4751

4852
List<PropertyAccessor> specificAccessors = new ArrayList<>();
4953
List<PropertyAccessor> generalAccessors = new ArrayList<>();
50-
for (PropertyAccessor resolver : propertyAccessors) {
51-
Class<?>[] targets = resolver.getSpecificTargetClasses();
52-
if (targets == null) { // generic resolver that says it can be used for any type
53-
generalAccessors.add(resolver);
54+
for (PropertyAccessor accessor : propertyAccessors) {
55+
Class<?>[] targets = accessor.getSpecificTargetClasses();
56+
if (ObjectUtils.isEmpty(targets)) {
57+
// generic accessor that says it can be used for any type
58+
generalAccessors.add(accessor);
5459
}
55-
else {
56-
if (targetType != null) {
57-
for (Class<?> clazz : targets) {
58-
if (clazz == targetType) { // put exact matches on the front to be tried first?
59-
specificAccessors.add(resolver);
60-
}
61-
else if (clazz.isAssignableFrom(targetType)) { // put supertype matches at the end of the
62-
// specificAccessor list
63-
generalAccessors.add(resolver);
64-
}
60+
else if (targetType != null) {
61+
for (Class<?> clazz : targets) {
62+
if (clazz == targetType) {
63+
// add exact matches to the specificAccessors list
64+
specificAccessors.add(accessor);
65+
}
66+
else if (clazz.isAssignableFrom(targetType)) {
67+
// add supertype matches to the front of the generalAccessors list
68+
generalAccessors.add(0, accessor);
6569
}
6670
}
6771
}
6872
}
69-
List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors.size() + generalAccessors.size());
70-
resolvers.addAll(specificAccessors);
71-
resolvers.addAll(generalAccessors);
72-
return resolvers;
73+
List<PropertyAccessor> accessors = new ArrayList<>(specificAccessors.size() + generalAccessors.size());
74+
accessors.addAll(specificAccessors);
75+
accessors.addAll(generalAccessors);
76+
return accessors;
7377
}
7478

7579
}

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

+5-34
Original file line numberDiff line numberDiff line change
@@ -298,46 +298,17 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
298298
}
299299

300300
/**
301-
* Determines the set of property resolvers that should be used to try and access a property
302-
* on the specified target type. The resolvers are considered to be in an ordered list,
303-
* however in the returned list any that are exact matches for the input target type (as
304-
* opposed to 'general' resolvers that could work for any type) are placed at the start of the
305-
* list. In addition, there are specific resolvers that exactly name the class in question
306-
* and resolvers that name a specific class but it is a supertype of the class we have.
307-
* These are put at the end of the specific resolvers set and will be tried after exactly
308-
* matching accessors but before generic accessors.
301+
* Determine the set of property accessors that should be used to try to
302+
* access a property on the specified context object.
303+
* <p>Delegates to {@link AstUtils#getPropertyAccessorsToTry(Class, List)}.
309304
* @param contextObject the object upon which property access is being attempted
310-
* @return a list of resolvers that should be tried in order to access the property
305+
* @return a list of accessors that should be tried in order to access the property
311306
*/
312307
private List<PropertyAccessor> getPropertyAccessorsToTry(
313308
@Nullable Object contextObject, List<PropertyAccessor> propertyAccessors) {
314309

315310
Class<?> targetType = (contextObject != null ? contextObject.getClass() : null);
316-
317-
List<PropertyAccessor> specificAccessors = new ArrayList<>();
318-
List<PropertyAccessor> generalAccessors = new ArrayList<>();
319-
for (PropertyAccessor resolver : propertyAccessors) {
320-
Class<?>[] targets = resolver.getSpecificTargetClasses();
321-
if (targets == null) {
322-
// generic resolver that says it can be used for any type
323-
generalAccessors.add(resolver);
324-
}
325-
else if (targetType != null) {
326-
for (Class<?> clazz : targets) {
327-
if (clazz == targetType) {
328-
specificAccessors.add(resolver);
329-
break;
330-
}
331-
else if (clazz.isAssignableFrom(targetType)) {
332-
generalAccessors.add(resolver);
333-
}
334-
}
335-
}
336-
}
337-
List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors);
338-
generalAccessors.removeAll(specificAccessors);
339-
resolvers.addAll(generalAccessors);
340-
return resolvers;
311+
return AstUtils.getPropertyAccessorsToTry(targetType, propertyAccessors);
341312
}
342313

343314
@Override

0 commit comments

Comments
 (0)