Search in sources :

Example 16 with TypedValue

use of cn.taketoday.expression.TypedValue in project today-framework by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithTime_ToString.

@Test
public void test_binaryPlusWithTime_ToString() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1, -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, -1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo((time + " is now"));
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 17 with TypedValue

use of cn.taketoday.expression.TypedValue in project today-framework by TAKETODAY.

the class SpelReproTests method SPR9994_bridgeMethods.

@Test
void SPR9994_bridgeMethods() throws Exception {
    ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
    StandardEvaluationContext context = new StandardEvaluationContext();
    GenericImplementation target = new GenericImplementation();
    accessor.write(context, target, "property", "1");
    assertThat(target.value).isEqualTo(1);
    TypedValue value = accessor.read(context, target, "property");
    assertThat(value.getValue()).isEqualTo(1);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
    assertThat(value.getTypeDescriptor().getAnnotations()).isNotEmpty();
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 18 with TypedValue

use of cn.taketoday.expression.TypedValue in project today-framework by TAKETODAY.

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().getElementDescriptor().getType()).isEqualTo(Integer.class);
    Integer[] array = (Integer[]) value;
    assertThat(array).containsExactly(0, 1, 2, 3, 4);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 19 with TypedValue

use of cn.taketoday.expression.TypedValue in project today-framework by TAKETODAY.

the class SelectionAndProjectionTests method projectionWithArray.

@Test
void projectionWithArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testArray", IntegerTestBean.createArray());
    Object value = expression.getValue(context);
    assertThat(value.getClass().isArray()).isTrue();
    TypedValue typedValue = new TypedValue(value);
    assertThat(typedValue.getTypeDescriptor().getElementDescriptor().getType()).isEqualTo(Number.class);
    Number[] array = (Number[]) value;
    assertThat(array).containsExactly(5, 5.9f, 7);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 20 with TypedValue

use of cn.taketoday.expression.TypedValue in project today-framework by TAKETODAY.

the class CompoundExpression method getValueRef.

@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    SpelNodeImpl[] children = this.children;
    if (getChildCount() == 1) {
        return children[0].getValueRef(state);
    }
    SpelNodeImpl nextNode = children[0];
    try {
        TypedValue result = nextNode.getValueInternal(state);
        int cc = getChildCount();
        for (int i = 1; i < cc - 1; i++) {
            try {
                state.pushActiveContextObject(result);
                nextNode = children[i];
                result = nextNode.getValueInternal(state);
            } finally {
                state.popActiveContextObject();
            }
        }
        try {
            state.pushActiveContextObject(result);
            nextNode = children[cc - 1];
            return nextNode.getValueRef(state);
        } finally {
            state.popActiveContextObject();
        }
    } catch (SpelEvaluationException ex) {
        // Correct the position for the error before re-throwing
        ex.setPosition(nextNode.getStartPosition());
        throw ex;
    }
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypedValue(cn.taketoday.expression.TypedValue)

Aggregations

TypedValue (cn.taketoday.expression.TypedValue)64 Test (org.junit.jupiter.api.Test)34 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)28 ExpressionState (cn.taketoday.expression.spel.ExpressionState)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)14 EvaluationContext (cn.taketoday.expression.EvaluationContext)12 Expression (cn.taketoday.expression.Expression)8 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)8 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)8 Nullable (cn.taketoday.lang.Nullable)8 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 AccessException (cn.taketoday.expression.AccessException)6 EvaluationException (cn.taketoday.expression.EvaluationException)4 ReflectivePropertyAccessor (cn.taketoday.expression.spel.support.ReflectivePropertyAccessor)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2