|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 |
|
22 | 22 | import org.springframework.expression.PropertyAccessor;
|
23 | 23 | import org.springframework.lang.Nullable;
|
| 24 | +import org.springframework.util.ObjectUtils; |
24 | 25 |
|
25 | 26 | /**
|
26 |
| - * Utilities methods for use in the Ast classes. |
| 27 | + * Utility methods for use in the AST classes. |
27 | 28 | *
|
28 | 29 | * @author Andy Clement
|
29 | 30 | * @since 3.0.2
|
30 | 31 | */
|
31 | 32 | public abstract class AstUtils {
|
32 | 33 |
|
33 | 34 | /**
|
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. |
42 | 45 | * @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 |
44 | 48 | */
|
45 | 49 | public static List<PropertyAccessor> getPropertyAccessorsToTry(
|
46 | 50 | @Nullable Class<?> targetType, List<PropertyAccessor> propertyAccessors) {
|
47 | 51 |
|
48 | 52 | List<PropertyAccessor> specificAccessors = new ArrayList<>();
|
49 | 53 | 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); |
54 | 59 | }
|
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); |
65 | 69 | }
|
66 | 70 | }
|
67 | 71 | }
|
68 | 72 | }
|
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; |
73 | 77 | }
|
74 | 78 |
|
75 | 79 | }
|
0 commit comments