KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > DirectFieldAccessor


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
17 package org.springframework.beans;
18
19 import java.beans.PropertyChangeEvent JavaDoc;
20 import java.lang.reflect.Field JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.springframework.util.Assert;
25 import org.springframework.util.ReflectionUtils;
26
27 /**
28  * PropertyAccessor implementation that directly accesses instance fields.
29  * Allows for direct binding to fields instead of going through JavaBean setters.
30  *
31  * <p>This implementation just supports fields in the actual target object.
32  * It is not able to traverse nested fields.
33  *
34  * <p>A DirectFieldAccessor's default for the "extractOldValueForEditor" setting
35  * is "true", since a field can always be read without side effects.
36  *
37  * @author Juergen Hoeller
38  * @since 2.0
39  * @see #setExtractOldValueForEditor
40  * @see BeanWrapper
41  * @see org.springframework.validation.DirectFieldBindingResult
42  * @see org.springframework.validation.DataBinder#initDirectFieldAccess()
43  */

44 public class DirectFieldAccessor extends AbstractPropertyAccessor {
45
46     private final Object JavaDoc target;
47
48     private final Map JavaDoc fieldMap = new HashMap JavaDoc();
49
50     private final TypeConverterDelegate typeConverterDelegate;
51
52
53     /**
54      * Create a new DirectFieldAccessor for the given target object.
55      * @param target the target object to access
56      */

57     public DirectFieldAccessor(Object JavaDoc target) {
58         Assert.notNull(target, "Target object must not be null");
59         this.target = target;
60         ReflectionUtils.doWithFields(this.target.getClass(), new ReflectionUtils.FieldCallback() {
61             public void doWith(Field JavaDoc field) {
62                 fieldMap.put(field.getName(), field);
63             }
64         });
65         this.typeConverterDelegate = new TypeConverterDelegate(this, target);
66         setExtractOldValueForEditor(true);
67     }
68
69
70     public boolean isReadableProperty(String JavaDoc propertyName) throws BeansException {
71         return this.fieldMap.containsKey(propertyName);
72     }
73
74     public boolean isWritableProperty(String JavaDoc propertyName) throws BeansException {
75         return this.fieldMap.containsKey(propertyName);
76     }
77
78     public Class JavaDoc getPropertyType(String JavaDoc propertyName) throws BeansException {
79         Field JavaDoc field = (Field JavaDoc) this.fieldMap.get(propertyName);
80         if (field != null) {
81             return field.getType();
82         }
83         return null;
84     }
85
86     public Object JavaDoc getPropertyValue(String JavaDoc propertyName) throws BeansException {
87         Field JavaDoc field = (Field JavaDoc) this.fieldMap.get(propertyName);
88         if (field == null) {
89             throw new NotReadablePropertyException(
90                     this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
91         }
92         try {
93             ReflectionUtils.makeAccessible(field);
94             return field.get(this.target);
95         }
96         catch (IllegalAccessException JavaDoc ex) {
97             throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex);
98         }
99     }
100
101     public void setPropertyValue(String JavaDoc propertyName, Object JavaDoc newValue) throws BeansException {
102         Field JavaDoc field = (Field JavaDoc) this.fieldMap.get(propertyName);
103         if (field == null) {
104             throw new NotWritablePropertyException(
105                     this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
106         }
107         Object JavaDoc oldValue = null;
108         try {
109             ReflectionUtils.makeAccessible(field);
110             oldValue = field.get(this.target);
111             Object JavaDoc convertedValue =
112                     this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, newValue, field.getType());
113             field.set(this.target, convertedValue);
114         }
115         catch (IllegalAccessException JavaDoc ex) {
116             throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex);
117         }
118         catch (IllegalArgumentException JavaDoc ex) {
119             PropertyChangeEvent JavaDoc pce = new PropertyChangeEvent JavaDoc(this.target, propertyName, oldValue, newValue);
120             throw new TypeMismatchException(pce, field.getType(), ex);
121         }
122     }
123
124 }
125
Popular Tags