KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > expression > support > BeanWrapperExpression


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
28  * An expression evaluator that uses the spring bean wrapper.
29  *
30  * @author Keith Donald
31  */

32 public class BeanWrapperExpression implements SettableExpression {
33
34     /**
35      * The expression.
36      */

37     private String JavaDoc expression;
38
39     public BeanWrapperExpression(String JavaDoc expression) {
40         this.expression = expression;
41     }
42
43     public int hashCode() {
44         return expression.hashCode();
45     }
46
47     public boolean equals(Object JavaDoc 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 JavaDoc evaluate(Object JavaDoc 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 JavaDoc target, Object JavaDoc 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 JavaDoc toString() {
75         return expression;
76     }
77 }
Popular Tags