Skip to content

Commit

Permalink
Polish SpEL's Indexer and test
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Feb 21, 2024
1 parent 5a2b127 commit 734fc47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@
import org.springframework.util.ReflectionUtils;

/**
* An Indexer can index into some proceeding structure to access a particular piece of it.
* <p>Supported structures are: strings / collections (lists/sets) / arrays.
* An {@code Indexer} can index into some proceeding structure to access a
* particular element of the structure.
*
* <p>Numerical index values are zero-based, such as when accessing the
* n<sup>th</sup> element of an array in Java.
*
* <h3>Supported Structures</h3>
*
* <ul>
* <li>Arrays: the n<sup>th</sup> element</li>
* <li>Collections (list and sets): the n<sup>th</sup> element</li>
* <li>Strings: the n<sup>th</sup> character as a {@link String}</li>
* <li>Maps: the value for the specified key</li>
* <li>Objects: the property with the specified name</li>
* </ul>
*
* @author Andy Clement
* @author Phillip Webb
Expand All @@ -58,6 +71,9 @@ public class Indexer extends SpelNodeImpl {
private enum IndexedType {ARRAY, LIST, MAP, STRING, OBJECT}


@Nullable
private IndexedType indexedType;

// These fields are used when the indexer is being used as a property read accessor.
// If the name and target type match these cached values then the cachedReadAccessor
// is used to read the property. If they do not match, the correct accessor is
Expand Down Expand Up @@ -86,12 +102,13 @@ private enum IndexedType {ARRAY, LIST, MAP, STRING, OBJECT}
@Nullable
private PropertyAccessor cachedWriteAccessor;

@Nullable
private IndexedType indexedType;


public Indexer(int startPos, int endPos, SpelNodeImpl expr) {
super(startPos, endPos, expr);
/**
* Create an {@code Indexer} with the given start position, end position, and
* index expression.
*/
public Indexer(int startPos, int endPos, SpelNodeImpl indexExpression) {
super(startPos, endPos, indexExpression);
}


Expand Down Expand Up @@ -146,6 +163,7 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException
if (target == null) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
}

// At this point, we need a TypeDescriptor for a non-null target object
Assert.state(targetDescriptor != null, "No type descriptor");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void indexIntoArrays() {
assertThat(expression.getValue(this)).isEqualTo(4);
}


@Test
@SuppressWarnings("unchecked")
void indexIntoGenericPropertyContainingMap() {
Expand Down Expand Up @@ -302,7 +301,7 @@ void indexIntoGenericPropertyContainingGrowingList2() {

@Test
void indexIntoGenericPropertyContainingArray() {
String[] property = new String[] { "bar" };
String[] property = { "bar" };
this.property = property;
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property");
Expand Down Expand Up @@ -357,17 +356,17 @@ void resolveMapKeyValueTypes() {

@Test
@SuppressWarnings("unchecked")
void testListOfScalar() {
void listOfScalars() {
listOfScalarNotGeneric = new ArrayList(1);
listOfScalarNotGeneric.add("5");
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("listOfScalarNotGeneric[0]");
assertThat(expression.getValue(this, Integer.class)).isEqualTo(Integer.valueOf(5));
assertThat(expression.getValue(this, Integer.class)).isEqualTo(5);
}

@Test
@SuppressWarnings("unchecked")
void testListsOfMap() {
void listOfMaps() {
listOfMapsNotGeneric = new ArrayList();
Map map = new HashMap();
map.put("fruit", "apple");
Expand Down

0 comments on commit 734fc47

Please sign in to comment.