SpEL supports projection on any kind of Collection (SPR-7493)
This commit is contained in:
parent
3f68ccadd9
commit
055c343ce0
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2010 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -36,7 +36,7 @@ import org.springframework.util.ObjectUtils;
|
||||||
* Represents projection, where a given operation is performed on all elements in some input sequence, returning
|
* Represents projection, where a given operation is performed on all elements in some input sequence, returning
|
||||||
* a new sequence of the same size. For example:
|
* a new sequence of the same size. For example:
|
||||||
* "{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}" returns "[n, y, n, y, n, y, n, y, n, y]"
|
* "{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}" returns "[n, y, n, y, n, y, n, y, n, y]"
|
||||||
*
|
*
|
||||||
* @author Andy Clement
|
* @author Andy Clement
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
|
|
@ -45,8 +45,8 @@ public class Projection extends SpelNodeImpl {
|
||||||
|
|
||||||
private final boolean nullSafe;
|
private final boolean nullSafe;
|
||||||
|
|
||||||
public Projection(boolean nullSafe, int pos,SpelNodeImpl expression) {
|
public Projection(boolean nullSafe, int pos, SpelNodeImpl expression) {
|
||||||
super(pos,expression);
|
super(pos, expression);
|
||||||
this.nullSafe = nullSafe;
|
this.nullSafe = nullSafe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,34 +64,36 @@ public class Projection extends SpelNodeImpl {
|
||||||
// and value, and they can be referenced in the operation
|
// and value, and they can be referenced in the operation
|
||||||
// eg. {'a':'y','b':'n'}.!{value=='y'?key:null}" == ['a', null]
|
// eg. {'a':'y','b':'n'}.!{value=='y'?key:null}" == ['a', null]
|
||||||
if (operand instanceof Map) {
|
if (operand instanceof Map) {
|
||||||
Map<?, ?> mapdata = (Map<?, ?>) operand;
|
Map<?, ?> mapData = (Map<?, ?>) operand;
|
||||||
List<Object> result = new ArrayList<Object>();
|
List<Object> result = new ArrayList<Object>();
|
||||||
for (Map.Entry entry : mapdata.entrySet()) {
|
for (Map.Entry entry : mapData.entrySet()) {
|
||||||
try {
|
try {
|
||||||
state.pushActiveContextObject(new TypedValue(entry,TypeDescriptor.valueOf(Map.Entry.class)));
|
state.pushActiveContextObject(new TypedValue(entry, TypeDescriptor.valueOf(Map.Entry.class)));
|
||||||
result.add(children[0].getValueInternal(state).getValue());
|
result.add(this.children[0].getValueInternal(state).getValue());
|
||||||
} finally {
|
}
|
||||||
|
finally {
|
||||||
state.popActiveContextObject();
|
state.popActiveContextObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new TypedValue(result,TypeDescriptor.valueOf(List.class)); // TODO unable to build correct type descriptor
|
return new TypedValue(result,TypeDescriptor.valueOf(List.class)); // TODO unable to build correct type descriptor
|
||||||
} else if (operand instanceof List || operandIsArray) {
|
}
|
||||||
List<Object> data = new ArrayList<Object>();
|
else if (operand instanceof Collection || operandIsArray) {
|
||||||
Collection<?> c = (operand instanceof List) ? (Collection<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand));
|
Collection<?> data = (operand instanceof Collection ? (Collection<?>) operand :
|
||||||
data.addAll(c);
|
Arrays.asList(ObjectUtils.toObjectArray(operand)));
|
||||||
List<Object> result = new ArrayList<Object>();
|
List<Object> result = new ArrayList<Object>();
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
Class<?> arrayElementType = null;
|
Class<?> arrayElementType = null;
|
||||||
for (Object element : data) {
|
for (Object element : data) {
|
||||||
try {
|
try {
|
||||||
state.pushActiveContextObject(new TypedValue(element,TypeDescriptor.valueOf(op.getTypeDescriptor().getType())));
|
state.pushActiveContextObject(new TypedValue(element ,TypeDescriptor.valueOf(op.getTypeDescriptor().getType())));
|
||||||
state.enterScope("index", idx);
|
state.enterScope("index", idx);
|
||||||
Object value = children[0].getValueInternal(state).getValue();
|
Object value = children[0].getValueInternal(state).getValue();
|
||||||
if (value != null && operandIsArray) {
|
if (value != null && operandIsArray) {
|
||||||
arrayElementType = this.determineCommonType(arrayElementType, value.getClass());
|
arrayElementType = determineCommonType(arrayElementType, value.getClass());
|
||||||
}
|
}
|
||||||
result.add(value);
|
result.add(value);
|
||||||
} finally {
|
}
|
||||||
|
finally {
|
||||||
state.exitScope();
|
state.exitScope();
|
||||||
state.popActiveContextObject();
|
state.popActiveContextObject();
|
||||||
}
|
}
|
||||||
|
|
@ -105,16 +107,21 @@ public class Projection extends SpelNodeImpl {
|
||||||
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
|
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
|
||||||
return new TypedValue(resultArray, op.getTypeDescriptor());
|
return new TypedValue(resultArray, op.getTypeDescriptor());
|
||||||
}
|
}
|
||||||
return new TypedValue(result,op.getTypeDescriptor());
|
return new TypedValue(result, op.getTypeDescriptor());
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if (operand==null) {
|
if (operand==null) {
|
||||||
if (nullSafe) {
|
if (this.nullSafe) {
|
||||||
return TypedValue.NULL;
|
return TypedValue.NULL;
|
||||||
} else {
|
|
||||||
throw new SpelEvaluationException(getStartPosition(),SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
|
|
||||||
}
|
}
|
||||||
} else {
|
else {
|
||||||
throw new SpelEvaluationException(getStartPosition(),SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClass().getName());
|
throw new SpelEvaluationException(getStartPosition(),
|
||||||
|
SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new SpelEvaluationException(getStartPosition(),
|
||||||
|
SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClass().getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,16 @@
|
||||||
|
|
||||||
package org.springframework.expression.spel;
|
package org.springframework.expression.spel;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.EvaluationContext;
|
||||||
import org.springframework.expression.Expression;
|
import org.springframework.expression.Expression;
|
||||||
import org.springframework.expression.ExpressionParser;
|
import org.springframework.expression.ExpressionParser;
|
||||||
|
|
@ -35,8 +36,6 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
*
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
public class SelectionAndProjectionTests {
|
public class SelectionAndProjectionTests {
|
||||||
|
|
||||||
|
|
@ -73,6 +72,39 @@ public class SelectionAndProjectionTests {
|
||||||
assertEquals(4, value);
|
assertEquals(4, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectionWithSet() throws Exception {
|
||||||
|
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
|
||||||
|
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
|
||||||
|
Object value = expression.getValue(context);
|
||||||
|
assertTrue(value instanceof List);
|
||||||
|
List list = (List) value;
|
||||||
|
assertEquals(5, list.size());
|
||||||
|
assertEquals(0, list.get(0));
|
||||||
|
assertEquals(1, list.get(1));
|
||||||
|
assertEquals(2, list.get(2));
|
||||||
|
assertEquals(3, list.get(3));
|
||||||
|
assertEquals(4, list.get(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectFirstItemInSet() throws Exception {
|
||||||
|
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
|
||||||
|
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
|
||||||
|
Object value = expression.getValue(context);
|
||||||
|
assertTrue(value instanceof Integer);
|
||||||
|
assertEquals(0, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectLastItemInSet() throws Exception {
|
||||||
|
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
|
||||||
|
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
|
||||||
|
Object value = expression.getValue(context);
|
||||||
|
assertTrue(value instanceof Integer);
|
||||||
|
assertEquals(4, value);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void selectionWithArray() throws Exception {
|
public void selectionWithArray() throws Exception {
|
||||||
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
|
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
|
||||||
|
|
@ -195,6 +227,20 @@ public class SelectionAndProjectionTests {
|
||||||
assertEquals(7, list.get(2));
|
assertEquals(7, list.get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void projectionWithSet() throws Exception {
|
||||||
|
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
|
||||||
|
EvaluationContext context = new StandardEvaluationContext();
|
||||||
|
context.setVariable("testList", IntegerTestBean.createSet());
|
||||||
|
Object value = expression.getValue(context);
|
||||||
|
assertTrue(value instanceof List);
|
||||||
|
List list = (List) value;
|
||||||
|
assertEquals(3, list.size());
|
||||||
|
assertEquals(5, list.get(0));
|
||||||
|
assertEquals(6, list.get(1));
|
||||||
|
assertEquals(7, list.get(2));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void projectionWithArray() throws Exception {
|
public void projectionWithArray() throws Exception {
|
||||||
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
|
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
|
||||||
|
|
@ -244,6 +290,21 @@ public class SelectionAndProjectionTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class SetTestBean {
|
||||||
|
|
||||||
|
private final Set<Integer> integers = new LinkedHashSet<Integer>();
|
||||||
|
|
||||||
|
SetTestBean() {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
integers.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Integer> getIntegers() {
|
||||||
|
return integers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class ArrayTestBean {
|
static class ArrayTestBean {
|
||||||
|
|
||||||
private final int[] ints = new int[10];
|
private final int[] ints = new int[10];
|
||||||
|
|
@ -286,6 +347,14 @@ public class SelectionAndProjectionTests {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Set<IntegerTestBean> createSet() {
|
||||||
|
Set<IntegerTestBean> set = new LinkedHashSet<IntegerTestBean>();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
set.add(new IntegerTestBean(i + 5));
|
||||||
|
}
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
static IntegerTestBean[] createArray() {
|
static IntegerTestBean[] createArray() {
|
||||||
IntegerTestBean[] array = new IntegerTestBean[3];
|
IntegerTestBean[] array = new IntegerTestBean[3];
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue