use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class ExpressionStateTests method testActiveContextObject.
@Test
public void testActiveContextObject() {
ExpressionState state = getState();
assertThat(state.getActiveContextObject().getValue()).isEqualTo(state.getRootContextObject().getValue());
assertThatIllegalStateException().isThrownBy(state::popActiveContextObject);
state.pushActiveContextObject(new TypedValue(34));
assertThat(state.getActiveContextObject().getValue()).isEqualTo(34);
state.pushActiveContextObject(new TypedValue("hello"));
assertThat(state.getActiveContextObject().getValue()).isEqualTo("hello");
state.popActiveContextObject();
assertThat(state.getActiveContextObject().getValue()).isEqualTo(34);
state.popActiveContextObject();
assertThat(state.getActiveContextObject().getValue()).isEqualTo(state.getRootContextObject().getValue());
state = new ExpressionState(new StandardEvaluationContext());
assertThat(state.getActiveContextObject()).isEqualTo(TypedValue.NULL);
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectionWithPrimitiveArray.
@Test
void selectionWithPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("ints.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertThat(value.getClass().isArray()).isTrue();
TypedValue typedValue = new TypedValue(value);
assertThat(typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()).isEqualTo(Integer.class);
Integer[] array = (Integer[]) value;
assertThat(array).containsExactly(0, 1, 2, 3, 4);
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectionWithArray.
@Test
void selectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertThat(value.getClass().isArray()).isTrue();
TypedValue typedValue = new TypedValue(value);
assertThat(typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()).isEqualTo(Integer.class);
Integer[] array = (Integer[]) value;
assertThat(array).containsExactly(0, 1, 2, 3, 4);
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class Projection method getValueRef.
@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
TypedValue op = state.getActiveContextObject();
Object operand = op.getValue();
boolean operandIsArray = ObjectUtils.isArray(operand);
// eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
if (operand instanceof Map) {
Map<?, ?> mapData = (Map<?, ?>) operand;
List<Object> result = new ArrayList<>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
state.pushActiveContextObject(new TypedValue(entry));
state.enterScope();
result.add(this.children[0].getValueInternal(state).getValue());
} finally {
state.popActiveContextObject();
state.exitScope();
}
}
// TODO unable to build correct type descriptor
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand instanceof Iterable || operandIsArray) {
Iterable<?> data = (operand instanceof Iterable ? (Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<>();
Class<?> arrayElementType = null;
for (Object element : data) {
try {
state.pushActiveContextObject(new TypedValue(element));
state.enterScope("index", result.size());
Object value = this.children[0].getValueInternal(state).getValue();
if (value != null && operandIsArray) {
arrayElementType = determineCommonType(arrayElementType, value.getClass());
}
result.add(value);
} finally {
state.exitScope();
state.popActiveContextObject();
}
}
if (operandIsArray) {
if (arrayElementType == null) {
arrayElementType = Object.class;
}
Object resultArray = Array.newInstance(arrayElementType, 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(result), this);
}
if (operand == null) {
if (this.nullSafe) {
return ValueRef.NullValueRef.INSTANCE;
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClass().getName());
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class QualifiedIdentifier method getValueInternal.
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
// Cache the concatenation of child identifiers
if (this.value == null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < getChildCount(); i++) {
Object value = this.children[i].getValueInternal(state).getValue();
if (i > 0 && (value == null || !value.toString().startsWith("$"))) {
sb.append('.');
}
sb.append(value);
}
this.value = new TypedValue(sb.toString());
}
return this.value;
}
Aggregations