1 16 package org.springframework.binding.expression.support; 17 18 import org.springframework.beans.BeanWrapperImpl; 19 import org.springframework.beans.BeansException; 20 import org.springframework.binding.expression.EvaluationAttempt; 21 import org.springframework.binding.expression.EvaluationContext; 22 import org.springframework.binding.expression.EvaluationException; 23 import org.springframework.binding.expression.SetValueAttempt; 24 import org.springframework.binding.expression.SettableExpression; 25 import org.springframework.util.Assert; 26 27 32 public class BeanWrapperExpression implements SettableExpression { 33 34 37 private String expression; 38 39 public BeanWrapperExpression(String expression) { 40 this.expression = expression; 41 } 42 43 public int hashCode() { 44 return expression.hashCode(); 45 } 46 47 public boolean equals(Object o) { 48 if (!(o instanceof BeanWrapperExpression)) { 49 return false; 50 } 51 BeanWrapperExpression other = (BeanWrapperExpression)o; 52 return expression.equals(other.expression); 53 } 54 55 public Object evaluate(Object target, EvaluationContext context) throws EvaluationException { 56 try { 57 return new BeanWrapperImpl(target).getPropertyValue(expression); 58 } 59 catch (BeansException e) { 60 throw new EvaluationException(new EvaluationAttempt(this, target, null), e); 61 } 62 } 63 64 public void evaluateToSet(Object target, Object value, EvaluationContext context) throws EvaluationException { 65 try { 66 Assert.notNull(target, "The target object to evaluate is required"); 67 new BeanWrapperImpl(target).setPropertyValue(expression, value); 68 } 69 catch (BeansException e) { 70 throw new EvaluationException(new SetValueAttempt(this, target, value, null), e); 71 } 72 } 73 74 public String toString() { 75 return expression; 76 } 77 } | Popular Tags |