SpEL selection/projection works with Iterable as well
Issue: SPR-13231
This commit is contained in:
@@ -19,7 +19,6 @@ package org.springframework.expression.spel.ast;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,6 +37,7 @@ import org.springframework.util.ObjectUtils;
|
||||
*
|
||||
* @author Andy Clement
|
||||
* @author Mark Fisher
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class Projection extends SpelNodeImpl {
|
||||
@@ -86,9 +86,10 @@ public class Projection extends SpelNodeImpl {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this); // TODO unable to build correct type descriptor
|
||||
}
|
||||
|
||||
if (operand instanceof Collection || operandIsArray) {
|
||||
Collection<?> data = (operand instanceof Collection ? (Collection<?>) operand :
|
||||
Arrays.asList(ObjectUtils.toObjectArray(operand)));
|
||||
if (operand instanceof Iterable || operandIsArray) {
|
||||
Iterable<?> data = (operand instanceof Iterable ?
|
||||
(Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
|
||||
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
int idx = 0;
|
||||
Class<?> arrayElementType = null;
|
||||
@@ -108,6 +109,7 @@ public class Projection extends SpelNodeImpl {
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (operandIsArray) {
|
||||
if (arrayElementType == null) {
|
||||
arrayElementType = Object.class;
|
||||
@@ -116,10 +118,11 @@ public class Projection extends SpelNodeImpl {
|
||||
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray),this);
|
||||
}
|
||||
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
|
||||
}
|
||||
|
||||
if (operand==null) {
|
||||
if (operand == null) {
|
||||
if (this.nullSafe) {
|
||||
return ValueRef.NullValueRef.INSTANCE;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.expression.spel.ast;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -43,6 +42,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Andy Clement
|
||||
* @author Mark Fisher
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class Selection extends SpelNodeImpl {
|
||||
@@ -75,13 +75,14 @@ public class Selection extends SpelNodeImpl {
|
||||
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
|
||||
TypedValue op = state.getActiveContextObject();
|
||||
Object operand = op.getValue();
|
||||
|
||||
SpelNodeImpl selectionCriteria = this.children[0];
|
||||
|
||||
if (operand instanceof Map) {
|
||||
Map<?, ?> mapdata = (Map<?, ?>) operand;
|
||||
// TODO don't lose generic info for the new map
|
||||
Map<Object, Object> result = new HashMap<Object, Object>();
|
||||
Object lastKey = null;
|
||||
|
||||
for (Map.Entry<?, ?> entry : mapdata.entrySet()) {
|
||||
try {
|
||||
TypedValue kvPair = new TypedValue(entry);
|
||||
@@ -108,6 +109,7 @@ public class Selection extends SpelNodeImpl {
|
||||
state.exitScope();
|
||||
}
|
||||
}
|
||||
|
||||
if ((this.variant == FIRST || this.variant == LAST) && result.isEmpty()) {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(null), this);
|
||||
}
|
||||
@@ -122,11 +124,10 @@ public class Selection extends SpelNodeImpl {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
|
||||
}
|
||||
|
||||
if ((operand instanceof Collection) || ObjectUtils.isArray(operand)) {
|
||||
List<Object> data = new ArrayList<Object>();
|
||||
Collection<?> coll = (operand instanceof Collection ?
|
||||
(Collection<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
|
||||
data.addAll(coll);
|
||||
if (operand instanceof Iterable || ObjectUtils.isArray(operand)) {
|
||||
Iterable<?> data = (operand instanceof Iterable ?
|
||||
(Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
|
||||
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
int index = 0;
|
||||
for (Object element : data) {
|
||||
@@ -154,22 +155,23 @@ public class Selection extends SpelNodeImpl {
|
||||
}
|
||||
}
|
||||
|
||||
if ((this.variant == FIRST || this.variant == LAST) && result.size() == 0) {
|
||||
if ((this.variant == FIRST || this.variant == LAST) && result.isEmpty()) {
|
||||
return ValueRef.NullValueRef.INSTANCE;
|
||||
}
|
||||
|
||||
if (this.variant == LAST) {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result.get(result.size() - 1)),this);
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result.get(result.size() - 1)), this);
|
||||
}
|
||||
|
||||
if (operand instanceof Collection) {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
|
||||
if (operand instanceof Iterable) {
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
|
||||
}
|
||||
|
||||
Class<?> elementType = ClassUtils.resolvePrimitiveIfNecessary(
|
||||
op.getTypeDescriptor().getElementTypeDescriptor().getType());
|
||||
Object resultArray = Array.newInstance(elementType, result.size());
|
||||
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray),this);
|
||||
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray), this);
|
||||
}
|
||||
if (operand == null) {
|
||||
if (this.nullSafe) {
|
||||
|
||||
Reference in New Issue
Block a user