Search in sources :

Example 26 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class OpPlus method convertTypedValueToString.

/**
	 * Convert operand value to string using registered converter or using
	 * {@code toString} method.
	 * @param value typed value to be converted
	 * @param state expression state
	 * @return {@code TypedValue} instance converted to {@code String}
	 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
    TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
    TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
    if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
        return String.valueOf(typeConverter.convertValue(value.getValue(), value.getTypeDescriptor(), typeDescriptor));
    }
    return String.valueOf(value.getValue());
}
Also used : TypeConverter(org.springframework.expression.TypeConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor)

Example 27 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class SpelExpression method getValueType.

@Override
public Class<?> getValueType(EvaluationContext context) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    ExpressionState expressionState = new ExpressionState(context, this.configuration);
    TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor();
    return (typeDescriptor != null ? typeDescriptor.getType() : null);
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) TypeDescriptor(org.springframework.core.convert.TypeDescriptor)

Example 28 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class ExpressionWithConversionTests method testCoercionToCollectionOfPrimitive.

@Test
public void testCoercionToCollectionOfPrimitive() throws Exception {
    class TestTarget {

        @SuppressWarnings("unused")
        public int sum(Collection<Integer> numbers) {
            int total = 0;
            for (int i : numbers) {
                total += i;
            }
            return total;
        }
    }
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    TypeDescriptor collectionType = new TypeDescriptor(new MethodParameter(TestTarget.class.getDeclaredMethod("sum", Collection.class), 0));
    // The type conversion is possible
    assertTrue(evaluationContext.getTypeConverter().canConvert(TypeDescriptor.valueOf(String.class), collectionType));
    // ... and it can be done successfully
    assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString());
    evaluationContext.setVariable("target", new TestTarget());
    // OK up to here, so the evaluation should be fine...
    // ... but this fails
    int result = (Integer) parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4");
    assertEquals("Wrong result: " + result, 10, result);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Collection(java.util.Collection) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 29 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class SpelReproTests method projectionTypeDescriptors_2.

@Test
public void projectionTypeDescriptors_2() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "as.![#this.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    Object[] value = (Object[]) exp.getValue(ctx);
    // value is array containing [true,false]
    assertEquals(Boolean.class, value[0].getClass());
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
    assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 30 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor 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)

Aggregations

TypeDescriptor (org.springframework.core.convert.TypeDescriptor)115 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)35 List (java.util.List)20 Map (java.util.Map)16 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 LinkedList (java.util.LinkedList)12 MethodParameter (org.springframework.core.MethodParameter)12 Collection (java.util.Collection)11 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)10 Method (java.lang.reflect.Method)9 AccessException (org.springframework.expression.AccessException)9 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)8 TypedValue (org.springframework.expression.TypedValue)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)8 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)8 MultiValueMap (org.springframework.util.MultiValueMap)8 AbstractList (java.util.AbstractList)7 Field (java.lang.reflect.Field)6