diff --git a/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpression.java b/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpression.java index 1573392e..5fb6a187 100644 --- a/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpression.java +++ b/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpression.java @@ -1,12 +1,12 @@ /* * Copyright 2004-2012 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,6 +19,7 @@ import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeansException; import org.springframework.beans.NotReadablePropertyException; import org.springframework.beans.NotWritablePropertyException; +import org.springframework.beans.NullValueInNestedPathException; import org.springframework.beans.TypeMismatchException; import org.springframework.binding.convert.ConversionService; import org.springframework.binding.expression.EvaluationException; @@ -28,18 +29,18 @@ import org.springframework.binding.expression.ValueCoercionException; /** * An expression that delegates to a {@link BeanWrapperImpl bean wrapper} to evaluate or set a property of a context. - * + * * Also supports the configuration of a {@link ConversionService} to allow StringToObject type conversion to occur as * part of setting a property. The StringToObject ConversionExecutors are automatically adapted and registered as * PropertyEditors. - * + * * Mainly exists to take advantage of BeanWrapper's unique property access features as an Expression implementation, * notably the ability to infer types of generic collections and maps and perform type coersion on collection elements * when setting values. - * + * * Note that Spring's BeanWrapper is not a full-blown EL implementation: it only supports property access, and does not * support method invocation, arithmetic operations, or logic operations. - * + * * @author Keith Donald * @author Scott Andrews */ @@ -49,6 +50,11 @@ public class BeanWrapperExpression implements Expression { private ConversionService conversionService; + private boolean autoGrowNestedPaths = false; + + private int autoGrowCollectionLimit = Integer.MAX_VALUE; + + /** * Creates a new bean wrapper expression. * @param expression the property expression string @@ -60,6 +66,25 @@ public class BeanWrapperExpression implements Expression { this.conversionService = conversionService; } + /** + * Set whether this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value. + *
If "true", a null path location will be populated with a default object value and traversed + * instead of resulting in a {@link NullValueInNestedPathException}. Turning this flag on also + * enables auto-growth of collection elements when accessing an out-of-bounds index. + *
Default is "false" on a plain BeanWrapper. + */ + public void setAutoGrowNestedPaths(boolean autoGrowNestedPaths) { + this.autoGrowNestedPaths = autoGrowNestedPaths; + } + + /** + * Specify a limit for array and collection auto-growing. + *
Default is unlimited on a plain BeanWrapper. + */ + public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit) { + this.autoGrowCollectionLimit = autoGrowCollectionLimit; + } + public boolean equals(Object o) { if (!(o instanceof BeanWrapperExpression)) { return false; @@ -75,6 +100,8 @@ public class BeanWrapperExpression implements Expression { public Object getValue(Object context) throws EvaluationException { try { BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context); + beanWrapper.setAutoGrowNestedPaths(autoGrowNestedPaths); + beanWrapper.setAutoGrowCollectionLimit(autoGrowCollectionLimit); return beanWrapper.getPropertyValue(expression); } catch (NotReadablePropertyException e) { throw new PropertyNotFoundException(context.getClass(), expression, e); @@ -88,6 +115,8 @@ public class BeanWrapperExpression implements Expression { public void setValue(Object context, Object value) { try { BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context); + beanWrapper.setAutoGrowNestedPaths(autoGrowNestedPaths); + beanWrapper.setAutoGrowCollectionLimit(autoGrowCollectionLimit); beanWrapper.setConversionService(conversionService.getDelegateConversionService()); beanWrapper.setPropertyValue(expression, value); } catch (NotWritablePropertyException e) { diff --git a/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpressionParser.java b/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpressionParser.java index 1a070bde..544ab6ec 100644 --- a/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpressionParser.java +++ b/spring-binding/src/main/java/org/springframework/binding/expression/beanwrapper/BeanWrapperExpressionParser.java @@ -1,12 +1,12 @@ /* * Copyright 2004-2008 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,6 +16,7 @@ package org.springframework.binding.expression.beanwrapper; import org.springframework.beans.BeanWrapperImpl; +import org.springframework.beans.NullValueInNestedPathException; import org.springframework.beans.propertyeditors.PropertiesEditor; import org.springframework.binding.convert.ConversionExecutor; import org.springframework.binding.convert.ConversionService; @@ -27,13 +28,17 @@ import org.springframework.binding.expression.support.AbstractExpressionParser; /** * An expression parser that parses BeanWrapper property expressions. - * + * * @author Keith Donald */ public class BeanWrapperExpressionParser extends AbstractExpressionParser { private ConversionService conversionService; + private boolean autoGrowNestedPaths = false; + + private int autoGrowCollectionLimit = Integer.MAX_VALUE; + /** * Creates a new expression parser that uses a {@link DefaultConversionService} to perform type conversion. */ @@ -69,7 +74,29 @@ public class BeanWrapperExpressionParser extends AbstractExpressionParser { this.conversionService = conversionService; } + /** + * Set whether this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value. + *
If "true", a null path location will be populated with a default object value and traversed + * instead of resulting in a {@link NullValueInNestedPathException}. Turning this flag on also + * enables auto-growth of collection elements when accessing an out-of-bounds index. + *
Default is "false" on a plain BeanWrapper. + */ + public void setAutoGrowNestedPaths(boolean autoGrowNestedPaths) { + this.autoGrowNestedPaths = autoGrowNestedPaths; + } + + /** + * Specify a limit for array and collection auto-growing. + *
Default is unlimited on a plain BeanWrapper. + */ + public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit) { + this.autoGrowCollectionLimit = autoGrowCollectionLimit; + } + protected Expression doParseExpression(String expressionString, ParserContext context) throws ParserException { - return new BeanWrapperExpression(expressionString, conversionService); + BeanWrapperExpression expression = new BeanWrapperExpression(expressionString, conversionService); + expression.setAutoGrowNestedPaths(autoGrowNestedPaths); + expression.setAutoGrowCollectionLimit(autoGrowCollectionLimit); + return expression; } } \ No newline at end of file