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);
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations