Java Code Examples for org.springframework.expression.spel.support.StandardEvaluationContext#setTypeConverter()

The following examples show how to use org.springframework.expression.spel.support.StandardEvaluationContext#setTypeConverter() . These examples are extracted from open source projects. 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 Project: spring-analysis-note   File: OpPlusTests.java    License: 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 2
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 3
Source Project: spring-analysis-note   File: EvalTag.java    License: 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 4
@Test
public void testSetParameterizedList() throws Exception {
	StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
	Expression e = parser.parseExpression("listOfInteger.size()");
	assertEquals(0,e.getValue(context,Integer.class).intValue());
	context.setTypeConverter(new TypeConvertorUsingConversionService());
	// Assign a List<String> to the List<Integer> field - the component elements should be converted
	parser.parseExpression("listOfInteger").setValue(context,listOfString);
	assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3
	Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
	assertEquals(Integer.class,clazz);
}
 
Example 5
Source Project: java-technology-stack   File: EvalTag.java    License: 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 6
Source Project: java-technology-stack   File: OpPlusTests.java    License: 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 7
@Test
public void testSetParameterizedList() throws Exception {
	StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
	Expression e = parser.parseExpression("listOfInteger.size()");
	assertEquals(0,e.getValue(context,Integer.class).intValue());
	context.setTypeConverter(new TypeConvertorUsingConversionService());
	// Assign a List<String> to the List<Integer> field - the component elements should be converted
	parser.parseExpression("listOfInteger").setValue(context,listOfString);
	assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3
	Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
	assertEquals(Integer.class,clazz);
}
 
Example 8
Source Project: lams   File: EvalTag.java    License: 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 9
Source Project: spring4-understanding   File: EvalTag.java    License: 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 10
@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 11
@Test
public void testSetParameterizedList() throws Exception {
	StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
	Expression e = parser.parseExpression("listOfInteger.size()");
	assertEquals(0,e.getValue(context,Integer.class).intValue());
	context.setTypeConverter(new TypeConvertorUsingConversionService());
	// Assign a List<String> to the List<Integer> field - the component elements should be converted
	parser.parseExpression("listOfInteger").setValue(context,listOfString);
	assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3
	Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
	assertEquals(Integer.class,clazz);
}