Skip to content

Commit dc73ec7

Browse files
committed
Address TODOs in SpEL's Indexer
This commit deletes outdated TODOs and addresses a remaining "current" TODO in SpEL's Indexer.
1 parent 888e501 commit dc73ec7

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Collection;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.StringJoiner;
2827
import java.util.function.Supplier;
2928

3029
import org.springframework.asm.MethodVisitor;
@@ -54,8 +53,6 @@
5453
* @author Sam Brannen
5554
* @since 3.0
5655
*/
57-
// TODO support multidimensional arrays
58-
// TODO support correct syntax for multidimensional [][][] and not [,,,]
5956
public class Indexer extends SpelNodeImpl {
6057

6158
private enum IndexedType {ARRAY, LIST, MAP, STRING, OBJECT}
@@ -185,7 +182,6 @@ else if (target instanceof Collection<?> collection) {
185182
}
186183

187184
// Try and treat the index value as a property of the context object
188-
// TODO Could call the conversion service to convert the value to a String
189185
TypeDescriptor valueType = indexValue.getTypeDescriptor();
190186
if (valueType != null && String.class == valueType.getType()) {
191187
this.indexedType = IndexedType.OBJECT;
@@ -209,7 +205,7 @@ else if (this.indexedType == IndexedType.MAP) {
209205
return (this.children[0] instanceof PropertyOrFieldReference || this.children[0].isCompilable());
210206
}
211207
else if (this.indexedType == IndexedType.OBJECT) {
212-
// If the string name is changing the accessor is clearly going to change (so no compilation possible)
208+
// If the string name is changing, the accessor is clearly going to change (so no compilation possible)
213209
return (this.cachedReadAccessor != null &&
214210
this.cachedReadAccessor instanceof ReflectivePropertyAccessor.OptimalPropertyAccessor &&
215211
getChild(0) instanceof StringLiteral);
@@ -326,12 +322,7 @@ else if (this.indexedType == IndexedType.OBJECT) {
326322

327323
@Override
328324
public String toStringAST() {
329-
// TODO Since we do not support multidimensional arrays, we should be able to return: "[" + getChild(0).toStringAST() + "]"
330-
StringJoiner sj = new StringJoiner(",", "[", "]");
331-
for (int i = 0; i < getChildCount(); i++) {
332-
sj.add(getChild(i).toStringAST());
333-
}
334-
return sj.toString();
325+
return "[" + getChild(0).toStringAST() + "]";
335326
}
336327

337328

spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@
4444
@SuppressWarnings("rawtypes")
4545
class IndexingTests {
4646

47+
@Test
48+
@SuppressWarnings("unchecked")
49+
void indexIntoArrays() {
50+
SpelExpressionParser parser = new SpelExpressionParser();
51+
52+
// One-dimensional
53+
this.property = new int[] {1, 2, 3, 4};
54+
Expression expression = parser.parseExpression("property[2]");
55+
assertThat(expression.getValue(this)).isEqualTo(3);
56+
57+
// Multi-dimensional
58+
this.property = new int[][] {{1, 2}, {3, 4}};
59+
expression = parser.parseExpression("property[0]");
60+
assertThat(expression.getValue(this)).isEqualTo(new int[] {1, 2});
61+
expression = parser.parseExpression("property[1][1]");
62+
assertThat(expression.getValue(this)).isEqualTo(4);
63+
}
64+
65+
4766
@Test
4867
@SuppressWarnings("unchecked")
4968
void indexIntoGenericPropertyContainingMap() {

0 commit comments

Comments
 (0)