org.springframework.expression.spel.support.StandardTypeConverter Java Examples

The following examples show how to use org.springframework.expression.spel.support.StandardTypeConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BooleanExpressionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #2
Source File: OpPlusTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);

	assertEquals(String.class, value.getTypeDescriptor().getObjectType());
	assertEquals(String.class, value.getTypeDescriptor().getType());
	assertEquals(format.format(time) + " is now", value.getValue());
}
 
Example #3
Source File: BooleanExpressionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #4
Source File: BooleanExpressionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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;
		}
	};
	eContext.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);
}
 
Example #5
Source File: EntityExpressionSupport.java    From springlets with Apache License 2.0 6 votes vote down vote up
private void registerConversionServiceInSpelExpressions(Expression parsedExpression) {
  if (conversionService == null) {
    return;
  }
  StandardTypeConverter converter = new StandardTypeConverter(conversionService);
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeConverter(converter);
  setContextIfSpelExpression(parsedExpression, context);

  if (parsedExpression instanceof CompositeStringExpression) {
    CompositeStringExpression composite = (CompositeStringExpression) parsedExpression;
    for (Expression childExpresion : composite.getExpressions()) {
      setContextIfSpelExpression(childExpresion, context);
    }
  }
}
 
Example #6
Source File: EvalTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #7
Source File: EvalTag.java    From java-technology-stack with MIT License 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #8
Source File: OpPlusTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void test_binaryPlusWithTimeConverted() {

	final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);

	GenericConversionService conversionService = new GenericConversionService();
	conversionService.addConverter(new Converter<Time, String>() {
		@Override
		public String convert(Time source) {
			return format.format(source);
		}
	});

	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);
	var.setValue(expressionState, time);

	StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
	OpPlus o = new OpPlus(-1, var, n2);
	TypedValue value = o.getValueInternal(expressionState);

	assertEquals(String.class, value.getTypeDescriptor().getObjectType());
	assertEquals(String.class, value.getTypeDescriptor().getType());
	assertEquals(format.format(time) + " is now", value.getValue());
}
 
Example #9
Source File: EvalTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #10
Source File: EvalTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #11
Source File: OpPlusTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void test_binaryPlusWithTimeConverted() {

	final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);

	GenericConversionService conversionService = new GenericConversionService();
	conversionService.addConverter(new Converter<Time, String>() {
		@Override
		public String convert(Time source) {
			return format.format(source);
		}
	});

	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);
	var.setValue(expressionState, time);

	StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
	OpPlus o = new OpPlus(-1, var, n2);
	TypedValue value = o.getValueInternal(expressionState);

	assertEquals(String.class, value.getTypeDescriptor().getObjectType());
	assertEquals(String.class, value.getTypeDescriptor().getType());
	assertEquals(format.format(time) + " is now", value.getValue());
}