Skip to content

Commit e3e34b0

Browse files
committed
for Keith and his binder: fault in support for maps (if attempt made to reference into null map) and for general objects, e.g. foo.bar if foo was null, attempt to dynamically build foo.
1 parent 85eda27 commit e3e34b0

5 files changed

Lines changed: 79 additions & 16 deletions

File tree

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/SpelMessage.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public enum SpelMessage {
9090
RUN_OUT_OF_ARGUMENTS(Kind.ERROR,1051,"Unexpected ran out of arguments"),//
9191
UNABLE_TO_GROW_COLLECTION(Kind.ERROR,1052,"Unable to grow collection"),//
9292
UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE(Kind.ERROR,1053,"Unable to grow collection: unable to determine list element type"),//
93-
UNABLE_TO_CREATE_LIST_FOR_INDEXING(Kind.ERROR,1054,"Unable to create a List for the following indexer"),//
93+
UNABLE_TO_CREATE_LIST_FOR_INDEXING(Kind.ERROR,1054,"Unable to dynamically create a List to replace a null value"),//
94+
UNABLE_TO_CREATE_MAP_FOR_INDEXING(Kind.ERROR,1055,"Unable to dynamically create a Map to replace a null value"),//
95+
UNABLE_TO_DYNAMICALLY_CREATE_OBJECT(Kind.ERROR,1056,"Unable to dynamically create instance of ''{0}'' to replace a null value"),//
9496
;
9597

9698
private Kind kind;

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
6969
}
7070

7171
// Indexing into a Map
72-
if (targetObject instanceof Map) {
72+
if (targetObjectTypeDescriptor.isMap()) {
73+
if (targetObject == null) {
74+
// Current decision: attempt to index into null map == exception and does not just return null
75+
throw new SpelEvaluationException(getStartPosition(),SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
76+
// if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
77+
// return new TypedValue(null,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()));
78+
// } else {
79+
// return new TypedValue(null,TypeDescriptor.NULL);
80+
// }
81+
}
7382
Object possiblyConvertedKey = index;
7483
if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
7584
possiblyConvertedKey = state.convertValue(index,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType()));

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.expression.spel.ast;
1818

1919
import java.util.ArrayList;
20+
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Map;
2223

@@ -56,18 +57,50 @@ public PropertyOrFieldReference(boolean nullSafe, String propertyOrFieldName, in
5657
@Override
5758
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
5859
TypedValue result = readProperty(state, this.name);
59-
if (result.getValue()==null && state.configuredToCreateCollection() && result.getTypeDescriptor().getType().equals(List.class) && nextChildIs(Indexer.class)) {
60-
// Create a new list ready for the indexer
61-
try {
62-
if (isWritable(state)) {
63-
List newList = ArrayList.class.newInstance();
64-
writeProperty(state, name, newList);
65-
result = readProperty(state, this.name);
60+
TypeDescriptor resultDescriptor = result.getTypeDescriptor();
61+
// Dynamically create the objects if the user has requested that optional behaviour
62+
if (result.getValue()==null && state.configuredToCreateCollectionOrMap() && nextChildIs(Indexer.class,PropertyOrFieldReference.class)) {
63+
// Creating lists and maps
64+
if ((resultDescriptor.getType().equals(List.class) || resultDescriptor.getType().equals(Map.class))) {
65+
// Create a new collection or map ready for the indexer
66+
if (resultDescriptor.getType().equals(List.class)) {
67+
try {
68+
if (isWritable(state)) {
69+
List newList = ArrayList.class.newInstance();
70+
writeProperty(state, name, newList);
71+
result = readProperty(state, this.name);
72+
}
73+
} catch (InstantiationException e) {
74+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
75+
} catch (IllegalAccessException e) {
76+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
77+
}
78+
} else {
79+
try {
80+
if (isWritable(state)) {
81+
Map newMap = HashMap.class.newInstance();
82+
writeProperty(state, name, newMap);
83+
result = readProperty(state, this.name);
84+
}
85+
} catch (InstantiationException e) {
86+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING);
87+
} catch (IllegalAccessException e) {
88+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING);
89+
}
6690
}
67-
} catch (InstantiationException e) {
68-
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
69-
} catch (IllegalAccessException e) {
70-
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
91+
} else {
92+
// 'simple' object
93+
try {
94+
if (isWritable(state)) {
95+
Object newObject = result.getTypeDescriptor().getType().newInstance();
96+
writeProperty(state, name, newObject);
97+
result = readProperty(state, this.name);
98+
}
99+
} catch (InstantiationException e) {
100+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType());
101+
} catch (IllegalAccessException e) {
102+
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType());
103+
}
71104
}
72105
}
73106
return result;

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,25 @@ protected SpelNodeImpl getPreviousChild() {
6262
return result;
6363
}
6464

65-
protected boolean nextChildIs(Class clazz) {
65+
/**
66+
* @return true if the next child is one of the specified classes
67+
*/
68+
protected boolean nextChildIs(Class... clazzes) {
6669
if (parent!=null) {
6770
SpelNodeImpl[] peers = parent.children;
6871
for (int i=0,max=peers.length;i<max;i++) {
6972
if (peers[i]==this) {
70-
return (i+1)<max && peers[i+1].getClass().equals(clazz);
73+
if ((i+1)>=max) {
74+
return false;
75+
} else {
76+
Class clazz = peers[i+1].getClass();
77+
for (Class desiredClazz: clazzes) {
78+
if (clazz.equals(desiredClazz)) {
79+
return true;
80+
}
81+
}
82+
return false;
83+
}
7184
}
7285
}
7386
}

‎org.springframework.expression/src/main/java/org/springframework/expression/spel/standard/SpelExpressionParserConfiguration.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
*/
2525
public interface SpelExpressionParserConfiguration {
2626

27-
static final int CreateListsOnAttemptToIndexIntoNull = 0x0001;
27+
/**
28+
* This option applies to maps/collections and regular objects. If the initial part of an expression evaluates to null and then an
29+
* attempt is made to resolve an index '[]' or property against it, and this option is set, then the relevant object will be constructed so that
30+
* the index/property resolution can proceed.
31+
*/
32+
static final int CreateObjectIfAttemptToReferenceNull = 0x0001;
33+
2834
static final int GrowListsOnIndexBeyondSize = 0x0002;
2935

3036
}

0 commit comments

Comments
 (0)