-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
InlineMap.java
165 lines (151 loc) · 4.7 KB
/
InlineMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.ast;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelNode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Represent a map in an expression, e.g. '{name:'foo',age:12}'
*
* @author Andy Clement
* @author Sam Brannen
* @since 4.1
*/
public class InlineMap extends SpelNodeImpl {
// If the map is purely literals, it is a constant value and can be computed and cached
@Nullable
private TypedValue constant;
public InlineMap(int startPos, int endPos, SpelNodeImpl... args) {
super(startPos, endPos, args);
checkIfConstant();
}
/**
* If all the components of the map are constants, or lists/maps that themselves
* contain constants, then a constant list can be built to represent this node.
* This will speed up later getValue calls and reduce the amount of garbage created.
*/
private void checkIfConstant() {
boolean isConstant = true;
for (int c = 0, max = getChildCount(); c < max; c++) {
SpelNode child = getChild(c);
if (!(child instanceof Literal)) {
if (child instanceof InlineList inlineList) {
if (!inlineList.isConstant()) {
isConstant = false;
break;
}
}
else if (child instanceof InlineMap inlineMap) {
if (!inlineMap.isConstant()) {
isConstant = false;
break;
}
}
else if (!(c % 2 == 0 && child instanceof PropertyOrFieldReference)) {
isConstant = false;
break;
}
}
}
if (isConstant) {
Map<Object, Object> constantMap = new LinkedHashMap<>();
int childCount = getChildCount();
for (int c = 0; c < childCount; c++) {
SpelNode keyChild = getChild(c++);
SpelNode valueChild = getChild(c);
Object key = null;
Object value = null;
if (keyChild instanceof Literal literal) {
key = literal.getLiteralValue().getValue();
}
else if (keyChild instanceof PropertyOrFieldReference propertyOrFieldReference) {
key = propertyOrFieldReference.getName();
}
else {
return;
}
if (valueChild instanceof Literal literal) {
value = literal.getLiteralValue().getValue();
}
else if (valueChild instanceof InlineList inlineList) {
value = inlineList.getConstantValue();
}
else if (valueChild instanceof InlineMap inlineMap) {
value = inlineMap.getConstantValue();
}
constantMap.put(key, value);
}
this.constant = new TypedValue(Collections.unmodifiableMap(constantMap));
}
}
@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
if (this.constant != null) {
return this.constant;
}
else {
Map<Object, Object> returnValue = new LinkedHashMap<>();
int childcount = getChildCount();
for (int c = 0; c < childcount; c++) {
// TODO allow for key being PropertyOrFieldReference like Indexer on maps
SpelNode keyChild = getChild(c++);
Object key = null;
if (keyChild instanceof PropertyOrFieldReference reference) {
key = reference.getName();
}
else {
key = keyChild.getValue(expressionState);
}
Object value = getChild(c).getValue(expressionState);
returnValue.put(key, value);
}
return new TypedValue(returnValue);
}
}
@Override
public String toStringAST() {
StringBuilder sb = new StringBuilder("{");
int count = getChildCount();
for (int c = 0; c < count; c++) {
if (c > 0) {
sb.append(',');
}
sb.append(getChild(c++).toStringAST());
sb.append(':');
sb.append(getChild(c).toStringAST());
}
sb.append('}');
return sb.toString();
}
/**
* Return whether this list is a constant value.
*/
public boolean isConstant() {
return this.constant != null;
}
@SuppressWarnings("unchecked")
@Nullable
public Map<Object, Object> getConstantValue() {
Assert.state(this.constant != null, "No constant");
return (Map<Object, Object>) this.constant.getValue();
}
}