Search in sources :

Example 1 with StandardTypeLocator

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

the class StandardTypeLocatorTests method testImports.

@Test
void testImports() throws EvaluationException {
    StandardTypeLocator locator = new StandardTypeLocator();
    assertThat(locator.findType("java.lang.Integer")).isEqualTo(Integer.class);
    assertThat(locator.findType("java.lang.String")).isEqualTo(String.class);
    List<String> prefixes = locator.getImportPrefixes();
    assertThat(prefixes.size()).isEqualTo(1);
    assertThat(prefixes.contains("java.lang")).isTrue();
    assertThat(prefixes.contains("java.util")).isFalse();
    assertThat(locator.findType("Boolean")).isEqualTo(Boolean.class);
    // currently does not know about java.util by default
    // assertEquals(java.util.List.class,locator.findType("List"));
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> locator.findType("URL")).satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.TYPE_NOT_FOUND));
    locator.registerImport("java.net");
    assertThat(locator.findType("URL")).isEqualTo(java.net.URL.class);
}
Also used : StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Test(org.junit.jupiter.api.Test)

Example 2 with StandardTypeLocator

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

the class StandardTypeLocatorTests method importClass.

@Test
void importClass() {
    StandardTypeLocator locator = new StandardTypeLocator();
    assertThat(locator.findType("Integer")).isEqualTo(Integer.class);
    assertThat(locator.findType("String")).isEqualTo(String.class);
    locator.importClass(A.class);
    assertThat(locator.findType("A")).isEqualTo(A.class);
}
Also used : StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Test(org.junit.jupiter.api.Test)

Example 3 with StandardTypeLocator

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

the class StandardTypeLocatorTests method registerAlias.

@Test
void registerAlias() {
    StandardTypeLocator locator = new StandardTypeLocator();
    assertThat(locator.findType("Integer")).isEqualTo(Integer.class);
    assertThat(locator.findType("String")).isEqualTo(String.class);
    locator.importClass(A.class);
    locator.registerAlias("A", "a");
    assertThat(locator.findType("a")).isEqualTo(A.class);
    locator.registerAlias(B.class, "b");
    assertThat(locator.findType("b")).isEqualTo(B.class);
}
Also used : StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Test(org.junit.jupiter.api.Test)

Example 4 with StandardTypeLocator

use of cn.taketoday.expression.spel.support.StandardTypeLocator 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 5 with StandardTypeLocator

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

the class StandardTypeLocatorTests method importClass.

@Test
void importClass() {
    StandardTypeLocator locator = new StandardTypeLocator();
    assertThat(locator.findType("Integer")).isEqualTo(Integer.class);
    assertThat(locator.findType("String")).isEqualTo(String.class);
    locator.importClass(A.class);
    assertThat(locator.findType("A")).isEqualTo(A.class);
}
Also used : StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Test(org.junit.jupiter.api.Test)

Aggregations

StandardTypeLocator (cn.taketoday.expression.spel.support.StandardTypeLocator)8 Test (org.junit.jupiter.api.Test)6 BeanExpressionException (cn.taketoday.beans.factory.BeanExpressionException)2 ConversionService (cn.taketoday.core.conversion.ConversionService)2 Expression (cn.taketoday.expression.Expression)2 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)2 StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)2 ApplicationConversionService (cn.taketoday.format.support.ApplicationConversionService)2 Nullable (cn.taketoday.lang.Nullable)2