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() . 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: 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 2
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 3
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 4
Source File: ExpressionWithConversionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 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 6
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 7
Source File: ExpressionWithConversionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 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 9
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 10
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());
}
 
Example 11
Source File: ExpressionWithConversionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}