Search in sources :

Example 1 with StandardTypeConverter

use of cn.taketoday.expression.spel.support.StandardTypeConverter in project today-infrastructure by TAKETODAY.

the class BooleanExpressionTests method testConvertAndHandleNull.

@Test
public void testConvertAndHandleNull() {
    // SPR-9445
    // without null conversion
    evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
    evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    // with null conversion (null -> false)
    GenericConversionService conversionService = new GenericConversionService() {

        @Override
        protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
            return targetType.getType() == Boolean.class ? false : null;
        }
    };
    context.setTypeConverter(new StandardTypeConverter(conversionService));
    evaluate("null or true", Boolean.TRUE, Boolean.class, false);
    evaluate("null and true", Boolean.FALSE, Boolean.class, false);
    evaluate("!null", Boolean.TRUE, Boolean.class, false);
    evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with StandardTypeConverter

use of cn.taketoday.expression.spel.support.StandardTypeConverter in project today-infrastructure by TAKETODAY.

the class StandardBeanExpressionResolver method evaluate.

@Override
@Nullable
public Object evaluate(@Nullable String value, BeanExpressionContext evalContext) throws BeansException {
    if (StringUtils.isEmpty(value)) {
        return value;
    }
    try {
        Expression expr = expressionCache.get(value);
        if (expr == null) {
            expr = expressionParser.parseExpression(value, this.beanExpressionParserContext);
            expressionCache.put(value, expr);
        }
        StandardEvaluationContext sec = evaluationCache.get(evalContext);
        if (sec == null) {
            sec = new StandardEvaluationContext(evalContext);
            sec.addPropertyAccessor(new BeanExpressionContextAccessor());
            sec.addPropertyAccessor(new BeanFactoryAccessor());
            sec.addPropertyAccessor(new MapAccessor());
            sec.addPropertyAccessor(new EnvironmentAccessor());
            sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
            sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
            sec.setTypeConverter(new StandardTypeConverter(() -> {
                ConversionService cs = evalContext.getBeanFactory().getConversionService();
                return cs != null ? cs : ApplicationConversionService.getSharedInstance();
            }));
            customizeEvaluationContext(sec);
            evaluationCache.put(evalContext, sec);
        }
        return expr.getValue(sec);
    } catch (Throwable ex) {
        throw new BeanExpressionException("Expression parsing failed", ex);
    }
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) BeanExpressionException(cn.taketoday.beans.factory.BeanExpressionException) StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Expression(cn.taketoday.expression.Expression) ConversionService(cn.taketoday.core.conversion.ConversionService) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Nullable(cn.taketoday.lang.Nullable)

Example 3 with StandardTypeConverter

use of cn.taketoday.expression.spel.support.StandardTypeConverter in project today-framework by TAKETODAY.

the class BooleanExpressionTests method testConvertAndHandleNull.

@Test
public void testConvertAndHandleNull() {
    // SPR-9445
    // without null conversion
    evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
    evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    // with null conversion (null -> false)
    GenericConversionService conversionService = new GenericConversionService() {

        @Override
        protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
            return targetType.getType() == Boolean.class ? false : null;
        }
    };
    context.setTypeConverter(new StandardTypeConverter(conversionService));
    evaluate("null or true", Boolean.TRUE, Boolean.class, false);
    evaluate("null and true", Boolean.FALSE, Boolean.class, false);
    evaluate("!null", Boolean.TRUE, Boolean.class, false);
    evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 4 with StandardTypeConverter

use of cn.taketoday.expression.spel.support.StandardTypeConverter in project today-framework by TAKETODAY.

the class StandardBeanExpressionResolver method evaluate.

@Override
@Nullable
public Object evaluate(@Nullable String value, BeanExpressionContext evalContext) throws BeansException {
    if (StringUtils.isEmpty(value)) {
        return value;
    }
    try {
        Expression expr = expressionCache.get(value);
        if (expr == null) {
            expr = expressionParser.parseExpression(value, this.beanExpressionParserContext);
            expressionCache.put(value, expr);
        }
        StandardEvaluationContext sec = evaluationCache.get(evalContext);
        if (sec == null) {
            sec = new StandardEvaluationContext(evalContext);
            sec.addPropertyAccessor(new BeanExpressionContextAccessor());
            sec.addPropertyAccessor(new BeanFactoryAccessor());
            sec.addPropertyAccessor(new MapAccessor());
            sec.addPropertyAccessor(new EnvironmentAccessor());
            sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
            sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
            sec.setTypeConverter(new StandardTypeConverter(() -> {
                ConversionService cs = evalContext.getBeanFactory().getConversionService();
                return cs != null ? cs : ApplicationConversionService.getSharedInstance();
            }));
            customizeEvaluationContext(sec);
            evaluationCache.put(evalContext, sec);
        }
        return expr.getValue(sec);
    } catch (Throwable ex) {
        throw new BeanExpressionException("Expression parsing failed", ex);
    }
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) BeanExpressionException(cn.taketoday.beans.factory.BeanExpressionException) StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Expression(cn.taketoday.expression.Expression) ConversionService(cn.taketoday.core.conversion.ConversionService) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Nullable(cn.taketoday.lang.Nullable)

Example 5 with StandardTypeConverter

use of cn.taketoday.expression.spel.support.StandardTypeConverter in project today-framework by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithTimeConverted.

@Test
public void test_binaryPlusWithTimeConverted() {
    SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
    GenericConversionService conversionService = new GenericConversionService();
    conversionService.addConverter(Time.class, String.class, format::format);
    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, -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((format.format(time) + " is now"));
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ExpressionState(cn.taketoday.expression.spel.ExpressionState) Time(java.sql.Time) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Date(java.util.Date) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)6 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 Test (org.junit.jupiter.api.Test)4 BeanExpressionException (cn.taketoday.beans.factory.BeanExpressionException)2 TypeDescriptor (cn.taketoday.core.TypeDescriptor)2 ConversionService (cn.taketoday.core.conversion.ConversionService)2 Expression (cn.taketoday.expression.Expression)2 TypedValue (cn.taketoday.expression.TypedValue)2 ExpressionState (cn.taketoday.expression.spel.ExpressionState)2 StandardTypeLocator (cn.taketoday.expression.spel.support.StandardTypeLocator)2 ApplicationConversionService (cn.taketoday.format.support.ApplicationConversionService)2 Nullable (cn.taketoday.lang.Nullable)2 Time (java.sql.Time)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2