Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.binding.expression.beanwrapper;
 
 import java.util.Set;
 
An expression that delegates to a bean wrapper to evaluate or set a property of a context. Also supports the configuration of a org.springframework.binding.convert.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(s):
Keith Donald
Scott Andrews
 
 public class BeanWrapperExpression implements Expression {
 
 	private String expression;
 
Creates a new bean wrapper expression.

Parameters:
expression the property expression string
conversionService the conversion service containing converters to use as PropertyEditors for type conversion
 
 	public BeanWrapperExpression(String expressionConversionService conversionService) {
 		this. = expression;
 		this. = conversionService;
 	}
 
 	public boolean equals(Object o) {
 		if (!(o instanceof BeanWrapperExpression)) {
 			return false;
 		}
 		return .equals(other.expression);
 	}
 
 	public int hashCode() {
 		return .hashCode();
 	}
 
 	public Object getValue(Object contextthrows EvaluationException {
 		try {
 			BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context);
 			return beanWrapper.getPropertyValue();
 		} catch (NotReadablePropertyException e) {
 			throw new PropertyNotFoundException(context.getClass(), e);
 		} catch (BeansException e) {
 			throw new EvaluationException(context.getClass(), getExpressionString(),
 					"A BeansException occurred getting the value for expression '" + getExpressionString()
 							+ "' on context [" + context.getClass() + "]"e);
 		}
 	}
 
 	public void setValue(Object contextObject value) {
 		try {
 			BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context);
 			for (Iterator it = converters.iterator(); it.hasNext();) {
 				ConversionExecutor converter = (ConversionExecutorit.next();
				beanWrapper.registerCustomEditor(converter.getTargetClass(), new PropertyEditorConverter(converter));
			}
			beanWrapper.setPropertyValue(value);
			throw new PropertyNotFoundException(context.getClass(), e);
catch (TypeMismatchException e) {
			throw new ValueCoercionException(context.getClass(), valuee.getRequiredType(), e);
catch (BeansException e) {
					"A BeansException occurred setting the value of expression '" + getExpressionString()
"' on context [" + context.getClass() + "] to [" + value + "]"e);
		}
	}
	public Class getValueType(Object context) {
		try {
			BeanWrapperImpl beanWrapper = new BeanWrapperImpl(context);
			return beanWrapper.getPropertyType();
			throw new PropertyNotFoundException(context.getClass(), e);
catch (BeansException e) {
					"An BeansException occurred getting the value type for expression '" + getExpressionString()
"' on context [" + context.getClass() + "]"e);
		}
	}
		return ;
	}
	public String toString() {
		return ;
	}
	private static class PropertyEditorConverter extends PropertyEditorSupport {
			this. = converter;
		}
		public void setAsText(String textthrows IllegalArgumentException {
			try {
				Object convertedValue = .execute(text);
				setValue(convertedValue);
catch (ConversionException e) {
				IllegalArgumentException iae = new IllegalArgumentException("Unable to convert text '" + text + "'");
				iae.initCause(e);
				throw iae;
			}
		}
	}
New to GrepCode? Check out our FAQ X