Search in sources :

Example 51 with TypedValue

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);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 52 with TypedValue

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);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 53 with TypedValue

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);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 54 with TypedValue

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());
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ArrayList(java.util.ArrayList) Map(java.util.Map) TypedValue(org.springframework.expression.TypedValue)

Example 55 with TypedValue

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;
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Aggregations

TypedValue (org.springframework.expression.TypedValue)65 Test (org.junit.jupiter.api.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)16 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)14 ExpressionState (org.springframework.expression.spel.ExpressionState)9 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AccessException (org.springframework.expression.AccessException)5 Expression (org.springframework.expression.Expression)4 CompiledExpression (org.springframework.expression.spel.CompiledExpression)4 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)4 MethodParameter (org.springframework.core.MethodParameter)3 EvaluationException (org.springframework.expression.EvaluationException)3 SpelNode (org.springframework.expression.spel.SpelNode)3 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)3 Nullable (org.springframework.lang.Nullable)3