Search in sources :

Example 1 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project core by craftercms.

the class SpELStringTemplateCompiler method init.

@PostConstruct
public void init() {
    if (evalContext == null) {
        evalContext = new StandardEvaluationContext();
    }
    if (evalContext instanceof StandardEvaluationContext) {
        StandardEvaluationContext standardEvalContext = (StandardEvaluationContext) evalContext;
        // PropertyAccessor used when the model is a BeanFactory.
        standardEvalContext.addPropertyAccessor(new BeanFactoryAccessor());
        if (beanFactory != null) {
            if (standardEvalContext.getBeanResolver() == null) {
                standardEvalContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
            }
            if (standardEvalContext.getTypeLocator() == null) {
                standardEvalContext.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader()));
            }
            if (standardEvalContext.getTypeConverter() == null) {
                ConversionService conversionService = beanFactory.getConversionService();
                if (conversionService != null) {
                    standardEvalContext.setTypeConverter(new StandardTypeConverter(conversionService));
                }
            }
        }
    }
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) StandardTypeLocator(org.springframework.expression.spel.support.StandardTypeLocator) ConversionService(org.springframework.core.convert.ConversionService) BeanFactoryAccessor(org.springframework.context.expression.BeanFactoryAccessor) PostConstruct(javax.annotation.PostConstruct)

Example 2 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project spring-framework by spring-projects.

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(org.springframework.expression.spel.support.StandardTypeConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 3 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project spring-framework by spring-projects.

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(org.springframework.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ExpressionState(org.springframework.expression.spel.ExpressionState) Time(java.sql.Time) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Date(java.util.Date) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 4 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project spring-integration by spring-projects.

the class AbstractEvaluationContextFactoryBean method initialize.

protected void initialize(String beanName) throws Exception {
    if (this.applicationContext != null) {
        ConversionService conversionService = IntegrationUtils.getConversionService(getApplicationContext());
        if (conversionService != null) {
            this.typeConverter = new StandardTypeConverter(conversionService);
        }
        Map<String, SpelFunctionFactoryBean> functionFactoryBeanMap = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicationContext, SpelFunctionFactoryBean.class);
        for (SpelFunctionFactoryBean spelFunctionFactoryBean : functionFactoryBeanMap.values()) {
            if (!getFunctions().containsKey(spelFunctionFactoryBean.getFunctionName())) {
                getFunctions().put(spelFunctionFactoryBean.getFunctionName(), spelFunctionFactoryBean.getObject());
            }
        }
        try {
            SpelPropertyAccessorRegistrar propertyAccessorRegistrar = this.applicationContext.getBean(SpelPropertyAccessorRegistrar.class);
            for (Entry<String, PropertyAccessor> entry : propertyAccessorRegistrar.getPropertyAccessors().entrySet()) {
                if (!getPropertyAccessors().containsKey(entry.getKey())) {
                    getPropertyAccessors().put(entry.getKey(), entry.getValue());
                }
            }
        } catch (NoSuchBeanDefinitionException e) {
        // There is no 'SpelPropertyAccessorRegistrar' bean in the application context.
        }
        ApplicationContext parent = this.applicationContext.getParent();
        if (parent != null && parent.containsBean(beanName)) {
            AbstractEvaluationContextFactoryBean parentFactoryBean = parent.getBean("&" + beanName, getClass());
            for (Entry<String, PropertyAccessor> entry : parentFactoryBean.getPropertyAccessors().entrySet()) {
                if (!getPropertyAccessors().containsKey(entry.getKey())) {
                    getPropertyAccessors().put(entry.getKey(), entry.getValue());
                }
            }
            for (Entry<String, Method> entry : parentFactoryBean.getFunctions().entrySet()) {
                if (!getFunctions().containsKey(entry.getKey())) {
                    getFunctions().put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    this.initialized = true;
}
Also used : SpelPropertyAccessorRegistrar(org.springframework.integration.expression.SpelPropertyAccessorRegistrar) PropertyAccessor(org.springframework.expression.PropertyAccessor) Method(java.lang.reflect.Method) StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) ApplicationContext(org.springframework.context.ApplicationContext) ConversionService(org.springframework.core.convert.ConversionService) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 5 with StandardTypeConverter

use of org.springframework.expression.spel.support.StandardTypeConverter in project spring-integration by spring-projects.

the class ExpressionUtils method createEvaluationContext.

/**
 * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
 * property accessor property and the supplied {@link ConversionService} in its
 * conversionService property.
 * @param conversionService the conversion service.
 * @param beanFactory the bean factory.
 * @param simple true if simple.
 * @return the evaluation context.
 */
private static EvaluationContext createEvaluationContext(ConversionService conversionService, BeanFactory beanFactory, boolean simple) {
    if (simple) {
        Builder ecBuilder = SimpleEvaluationContext.forPropertyAccessors(new MapAccessor(), DataBindingPropertyAccessor.forReadOnlyAccess()).withInstanceMethods();
        if (conversionService != null) {
            ecBuilder.withConversionService(conversionService);
        }
        return ecBuilder.build();
    } else {
        StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
        evaluationContext.addPropertyAccessor(new MapAccessor());
        if (conversionService != null) {
            evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
        }
        if (beanFactory != null) {
            evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
        }
        return evaluationContext;
    }
}
Also used : StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Builder(org.springframework.expression.spel.support.SimpleEvaluationContext.Builder) MapAccessor(org.springframework.context.expression.MapAccessor)

Aggregations

StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)7 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)5 ConversionService (org.springframework.core.convert.ConversionService)4 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)3 Test (org.junit.jupiter.api.Test)2 MapAccessor (org.springframework.context.expression.MapAccessor)2 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)2 StandardTypeLocator (org.springframework.expression.spel.support.StandardTypeLocator)2 Method (java.lang.reflect.Method)1 Time (java.sql.Time)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 PostConstruct (javax.annotation.PostConstruct)1 BeanExpressionException (org.springframework.beans.factory.BeanExpressionException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 BeanFactoryAccessor (org.springframework.context.expression.BeanFactoryAccessor)1 EnvironmentAccessor (org.springframework.context.expression.EnvironmentAccessor)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1