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