Search in sources :

Example 26 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class OpPlusTests method test_binaryPlusWithTimeConverted.

@Test
public void test_binaryPlusWithTimeConverted() {
    final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
    GenericConversionService conversionService = new GenericConversionService();
    conversionService.addConverter(new Converter<Time, String>() {

        @Override
        public String convert(Time source) {
            return format.format(source);
        }
    });
    StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
    evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
    ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertEquals(String.class, value.getTypeDescriptor().getObjectType());
    assertEquals(String.class, value.getTypeDescriptor().getType());
    assertEquals(format.format(time) + " is now", value.getValue());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) ExpressionState(org.springframework.expression.spel.ExpressionState) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 27 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class OpPlusTests method test_binaryPlusWithStringOperands.

@Test
public void test_binaryPlusWithStringOperands() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    StringLiteral n1 = new StringLiteral("\"foo\"", -1, "\"foo\"");
    StringLiteral n2 = new StringLiteral("\"bar\"", -1, "\"bar\"");
    OpPlus o = new OpPlus(-1, n1, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertEquals(String.class, value.getTypeDescriptor().getObjectType());
    assertEquals(String.class, value.getTypeDescriptor().getType());
    assertEquals("foobar", value.getValue());
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 28 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class SpelReproTests method SPR9495.

@Test
public void SPR9495() throws Exception {
    SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
    ExpressionParser parser = new SpelExpressionParser(configuration);
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression spel = parser.parseExpression("#enumType.values()");
    context.setVariable("enumType", ABC.class);
    Object result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(ABC.A, Array.get(result, 0));
    assertEquals(ABC.B, Array.get(result, 1));
    assertEquals(ABC.C, Array.get(result, 2));
    context.addMethodResolver(new MethodResolver() {

        @Override
        public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
            return new MethodExecutor() {

                @Override
                public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
                    try {
                        Method method = XYZ.class.getMethod("values");
                        Object value = method.invoke(target, arguments);
                        return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
                    } catch (Exception ex) {
                        throw new AccessException(ex.getMessage(), ex);
                    }
                }
            };
        }
    });
    result = spel.getValue(context);
    assertNotNull(result);
    assertTrue(result.getClass().isArray());
    assertEquals(XYZ.X, Array.get(result, 0));
    assertEquals(XYZ.Y, Array.get(result, 1));
    assertEquals(XYZ.Z, Array.get(result, 2));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) Method(java.lang.reflect.Method) ExpressionException(org.springframework.expression.ExpressionException) EvaluationException(org.springframework.expression.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) AccessException(org.springframework.expression.AccessException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) MethodExecutor(org.springframework.expression.MethodExecutor) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodParameter(org.springframework.core.MethodParameter) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 29 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class SpelReproTests method SPR10162_onlyBridgeMethod.

@Test
public void SPR10162_onlyBridgeMethod() throws Exception {
    ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Object target = new OnlyBridgeMethod();
    TypedValue value = accessor.read(context, target, "property");
    assertEquals(Integer.class, value.getTypeDescriptor().getType());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 30 with TypedValue

use of org.springframework.expression.TypedValue in project spring-security by spring-projects.

the class DelegatingEvaluationContextTests method getRootObject.

@Test
public void getRootObject() {
    TypedValue expected = mock(TypedValue.class);
    when(this.delegate.getRootObject()).thenReturn(expected);
    assertThat(this.context.getRootObject()).isEqualTo(expected);
}
Also used : TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Aggregations

TypedValue (org.springframework.expression.TypedValue)63 Test (org.junit.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)18 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)13 ExpressionState (org.springframework.expression.spel.ExpressionState)11 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 AccessException (org.springframework.expression.AccessException)4 Expression (org.springframework.expression.Expression)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 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2