1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2023 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.
32
32
*
33
33
* @author Andy Clement
34
34
* @author Sam Brannen
35
+ * @author Harry Yang
35
36
* @since 4.1
36
37
*/
37
38
public class InlineMap extends SpelNodeImpl {
38
39
39
40
// If the map is purely literals, it is a constant value and can be computed and cached
40
41
@ Nullable
41
- private TypedValue constant ;
42
+ private final TypedValue constant ;
42
43
43
44
44
45
public InlineMap (int startPos , int endPos , SpelNodeImpl ... args ) {
45
46
super (startPos , endPos , args );
46
- checkIfConstant ();
47
+ this . constant = computeConstantValue ();
47
48
}
48
49
49
50
@@ -52,59 +53,55 @@ public InlineMap(int startPos, int endPos, SpelNodeImpl... args) {
52
53
* contain constants, then a constant list can be built to represent this node.
53
54
* This will speed up later getValue calls and reduce the amount of garbage created.
54
55
*/
55
- private void checkIfConstant () {
56
- boolean isConstant = true ;
56
+ @ Nullable
57
+ private TypedValue computeConstantValue () {
57
58
for (int c = 0 , max = getChildCount (); c < max ; c ++) {
58
59
SpelNode child = getChild (c );
59
60
if (!(child instanceof Literal )) {
60
61
if (child instanceof InlineList inlineList ) {
61
62
if (!inlineList .isConstant ()) {
62
- isConstant = false ;
63
- break ;
63
+ return null ;
64
64
}
65
65
}
66
66
else if (child instanceof InlineMap inlineMap ) {
67
67
if (!inlineMap .isConstant ()) {
68
- isConstant = false ;
69
- break ;
68
+ return null ;
70
69
}
71
70
}
72
71
else if (!(c % 2 == 0 && child instanceof PropertyOrFieldReference )) {
73
- isConstant = false ;
74
- break ;
72
+ return null ;
75
73
}
76
74
}
77
75
}
78
- if (isConstant ) {
79
- Map <Object , Object > constantMap = new LinkedHashMap <>();
80
- int childCount = getChildCount ();
81
- for (int c = 0 ; c < childCount ; c ++) {
82
- SpelNode keyChild = getChild (c ++);
83
- SpelNode valueChild = getChild (c );
84
- Object key = null ;
85
- Object value = null ;
86
- if (keyChild instanceof Literal literal ) {
87
- key = literal .getLiteralValue ().getValue ();
88
- }
89
- else if (keyChild instanceof PropertyOrFieldReference propertyOrFieldReference ) {
90
- key = propertyOrFieldReference .getName ();
91
- }
92
- else {
93
- return ;
94
- }
95
- if (valueChild instanceof Literal literal ) {
96
- value = literal .getLiteralValue ().getValue ();
97
- }
98
- else if (valueChild instanceof InlineList inlineList ) {
99
- value = inlineList .getConstantValue ();
100
- }
101
- else if (valueChild instanceof InlineMap inlineMap ) {
102
- value = inlineMap .getConstantValue ();
103
- }
104
- constantMap .put (key , value );
76
+
77
+ Map <Object , Object > constantMap = new LinkedHashMap <>();
78
+ int childCount = getChildCount ();
79
+ for (int c = 0 ; c < childCount ; c ++) {
80
+ SpelNode keyChild = getChild (c ++);
81
+ SpelNode valueChild = getChild (c );
82
+ Object key ;
83
+ Object value = null ;
84
+ if (keyChild instanceof Literal literal ) {
85
+ key = literal .getLiteralValue ().getValue ();
86
+ }
87
+ else if (keyChild instanceof PropertyOrFieldReference propertyOrFieldReference ) {
88
+ key = propertyOrFieldReference .getName ();
89
+ }
90
+ else {
91
+ return null ;
92
+ }
93
+ if (valueChild instanceof Literal literal ) {
94
+ value = literal .getLiteralValue ().getValue ();
95
+ }
96
+ else if (valueChild instanceof InlineList inlineList ) {
97
+ value = inlineList .getConstantValue ();
98
+ }
99
+ else if (valueChild instanceof InlineMap inlineMap ) {
100
+ value = inlineMap .getConstantValue ();
105
101
}
106
- this . constant = new TypedValue ( Collections . unmodifiableMap ( constantMap ) );
102
+ constantMap . put ( key , value );
107
103
}
104
+ return new TypedValue (Collections .unmodifiableMap (constantMap ));
108
105
}
109
106
110
107
@ Override
0 commit comments