diff --git a/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpression.java b/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpression.java index 217e3021..c0937a40 100644 --- a/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpression.java +++ b/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpression.java @@ -1,5 +1,5 @@ /* - * Copyright 2004-2012 the original author or authors. + * Copyright 2004-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,11 +33,9 @@ import org.springframework.expression.spel.support.StandardTypeConverter; import org.springframework.util.Assert; /** - *

- * A wrapper for a Spring EL {@link org.springframework.expression.Expression} allowing it to be used under the Spring - * Binding {@link Expression} abstraction. - *

- * + * A wrapper for a Spring EL {@link org.springframework.expression.Expression} + * allowing it to be used under the Spring Binding {@link Expression} abstraction. + * * @author Rossen Stoyanchev * @since 2.1.0 */ @@ -55,18 +53,20 @@ public class SpringELExpression implements Expression { /** * Constructor for SpringELExpression. - * + * * @param expression a parsed Spring EL expression instance. Must not be null. - * @param expressionVariables provides a mapping between variables names and parsed Spring EL expression instances. + * @param expressionVariables provides a mapping between variables names and + * parsed Spring EL expression instances. + * This parameter is optional (may be null). + * @param expectedType the target type expected from the evaluation of the expression or null. * This parameter is optional (may be null). - * @param expectedType the target type expected from the evaluation of the expression or null. This parameter is - * optional (may be null). * @param conversionService the Spring ConversionService instance to use for type conversion * @param propertyAccessors propertyAccessors for Spring EL to use when evaluating expressions */ public SpringELExpression(org.springframework.expression.Expression expression, Map expressionVariables, Class expectedType, ConversionService conversionService, List propertyAccessors) { + Assert.notNull(expression, "The SpelExpression is required for evaluation"); this.expression = expression; this.expressionVariables = expressionVariables; @@ -81,7 +81,7 @@ public class SpringELExpression implements Expression { public Object getValue(Object rootObject) throws EvaluationException { try { - return expression.getValue(newEvaluationContext(rootObject), expectedType); + return expression.getValue(createEvaluationContext(rootObject), expectedType); } catch (SpelEvaluationException e) { if (e.getMessageCode().equals(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE)) { throw new PropertyNotFoundException(rootObject.getClass(), getExpressionString(), e); @@ -97,7 +97,7 @@ public class SpringELExpression implements Expression { public Class getValueType(Object rootObject) throws EvaluationException { try { - return expression.getValueType(newEvaluationContext(rootObject)); + return expression.getValueType(createEvaluationContext(rootObject)); } catch (SpelEvaluationException e) { if (e.getMessageCode().equals(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE)) { throw new PropertyNotFoundException(rootObject.getClass(), getExpressionString(), e); @@ -110,7 +110,8 @@ public class SpringELExpression implements Expression { public void setValue(Object rootObject, Object value) throws EvaluationException { try { - expression.setValue(newEvaluationContext(rootObject), value); + StandardEvaluationContext evaluationContext = createEvaluationContext(rootObject); + expression.setValue(evaluationContext, value); } catch (SpelEvaluationException e) { if (e.getMessageCode().equals(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE)) { throw new PropertyNotFoundException(rootObject.getClass(), getExpressionString(), e); @@ -125,12 +126,9 @@ public class SpringELExpression implements Expression { } /** - * Updates the Spring EL evaluation context to reflect the given rootObject. - * - * @param rootObject the object for the evaluation. - * @return + * Create a new Spring EL evaluation context for the given rootObject. */ - private StandardEvaluationContext newEvaluationContext(Object rootObject) { + private StandardEvaluationContext createEvaluationContext(Object rootObject) { StandardEvaluationContext context = new StandardEvaluationContext(rootObject); context.setVariables(getVariableValues(rootObject)); context.setTypeConverter(new StandardTypeConverter(conversionService)); @@ -139,9 +137,9 @@ public class SpringELExpression implements Expression { } /** - * Turns the map of variable-names-to-expressions into a map of variable-names-to-plain-objects by evaluating each - * object against the input rootObject. - * + * Turn the map of variable-names-to-expressions into a map of variable-names-to-plain-objects + * by evaluating each object against the input rootObject. + * * @param rootObject the Object to evaluate variable expressions against. * @return a mapping between variables names and plain Object's. */ diff --git a/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpressionParser.java b/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpressionParser.java index a7ed15e7..818136b0 100644 --- a/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpressionParser.java +++ b/spring-binding/src/main/java/org/springframework/binding/expression/spel/SpringELExpressionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2004-2012 the original author or authors. + * Copyright 2004-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,10 +35,9 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.util.Assert; /** - *

- * Adapts the Spring EL {@link SpelExpressionParser} to the Spring Binding {@link ExpressionParser} interface. - *

- * + * Adapt the Spring EL {@link SpelExpressionParser} to the Spring Binding + * {@link ExpressionParser} contract. + * * @author Rossen Stoyanchev * @since 2.1.0 */ @@ -68,42 +67,45 @@ public class SpringELExpressionParser implements ExpressionParser { propertyAccessors.add(propertyAccessor); } - public Expression parseExpression(String expressionString, ParserContext parserContext) throws ParserException { - Assert.hasText(expressionString, "The expression string to parse is required and must not be empty"); - parserContext = (parserContext == null) ? NullParserContext.INSTANCE : parserContext; - Map spelExpressionVariables = parseSpelExpressionVariables(parserContext - .getExpressionVariables()); - return new SpringELExpression(parseSpelExpression(expressionString, parserContext), spelExpressionVariables, - parserContext.getExpectedEvaluationResultType(), conversionService.getDelegateConversionService(), - propertyAccessors); + public Expression parseExpression(String expression, ParserContext context) throws ParserException { + + Assert.hasText(expression, "The expression string to parse is required and must not be empty"); + + context = (context == null) ? NullParserContext.INSTANCE : context; + Map expressionVars = parseSpelExpressionVariables(context.getExpressionVariables()); + + org.springframework.expression.Expression spelExpression = parseSpelExpression(expression, context); + Class expectedResultType = context.getExpectedEvaluationResultType(); + org.springframework.core.convert.ConversionService cs = conversionService.getDelegateConversionService(); + + return new SpringELExpression(spelExpression, expressionVars, expectedResultType, cs, propertyAccessors); } - private org.springframework.expression.Expression parseSpelExpression(String expression, ParserContext parserContext) { - return expressionParser.parseExpression(expression, getSpelParserContext(parserContext)); + private org.springframework.expression.Expression parseSpelExpression(String expression, ParserContext context) { + return expressionParser.parseExpression(expression, getSpelParserContext(context)); } - private org.springframework.expression.ParserContext getSpelParserContext(ParserContext parserContext) { - return parserContext.isTemplate() ? org.springframework.expression.ParserContext.TEMPLATE_EXPRESSION : null; + private org.springframework.expression.ParserContext getSpelParserContext(ParserContext context) { + return context.isTemplate() ? org.springframework.expression.ParserContext.TEMPLATE_EXPRESSION : null; } /** - * Turns {@link ExpressionVariable}'s (pairs of variable names and string expressions) into a map of variable names - * and parsed Spring EL expressions. The map will be saved in a Spring EL {@link EvaluationContext} for later use at - * evaluation time. - * - * @param expressionVariables an array of ExpressionVariable instances. + * Turn {@link ExpressionVariable}'s (pairs of variable names and string expressions) + * into a map of variable names and parsed Spring EL expressions. The map will be saved + * in a Spring EL {@link EvaluationContext} for later use at evaluation time. + * + * @param expressionVars an array of ExpressionVariable instances. * @return a Map or null if the input array is empty. */ - private Map parseSpelExpressionVariables(ExpressionVariable[] expressionVariables) { - if (expressionVariables == null || expressionVariables.length == 0) { + private Map parseSpelExpressionVariables(ExpressionVariable[] expressionVars) { + if (expressionVars == null || expressionVars.length == 0) { return null; } - Map spelExpressionVariables = new HashMap(expressionVariables.length); - for (ExpressionVariable var : expressionVariables) { - spelExpressionVariables.put(var.getName(), - parseExpression(var.getValueExpression(), var.getParserContext())); + Map result = new HashMap(expressionVars.length); + for (ExpressionVariable var : expressionVars) { + result.put(var.getName(), parseExpression(var.getValueExpression(), var.getParserContext())); } - return spelExpressionVariables; + return result; } } diff --git a/spring-webflow/src/main/java/org/springframework/webflow/expression/spel/WebFlowSpringELExpressionParser.java b/spring-webflow/src/main/java/org/springframework/webflow/expression/spel/WebFlowSpringELExpressionParser.java index c0c08c07..e9e096c3 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/expression/spel/WebFlowSpringELExpressionParser.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/expression/spel/WebFlowSpringELExpressionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +20,22 @@ import org.springframework.binding.expression.spel.SpringELExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; /** - * A sub-class for {@link SpringELExpressionParser} that registers Web Flow specific Spring EL PropertyAccessors. - * + * A sub-class for {@link SpringELExpressionParser} that registers Web Flow + * specific Spring EL PropertyAccessors. + * * @author Rossen Stoyanchev * @since 2.1 */ public class WebFlowSpringELExpressionParser extends SpringELExpressionParser { + public WebFlowSpringELExpressionParser(SpelExpressionParser expressionParser) { super(expressionParser); addDefaultPropertyAccessors(); } - public WebFlowSpringELExpressionParser(SpelExpressionParser expressionParser, ConversionService conversionService) { - super(expressionParser, conversionService); + public WebFlowSpringELExpressionParser(SpelExpressionParser parser, ConversionService conversionService) { + super(parser, conversionService); addDefaultPropertyAccessors(); }