KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > BeanAccessor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.reflect;
21
22 import java.beans.IntrospectionException JavaDoc;
23 import java.beans.PropertyDescriptor JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * A property accessor that uses set/get methods following JavaBean naming conventions.
28  *
29  * @since 1.2
30  * @author Andrus Adamchik
31  */

32 public class BeanAccessor implements Accessor {
33
34     protected String JavaDoc propertyName;
35     protected Method JavaDoc readMethod;
36     protected Method JavaDoc writeMethod;
37     protected Object JavaDoc nullValue;
38
39     public BeanAccessor(Class JavaDoc objectClass, String JavaDoc propertyName, Class JavaDoc propertyType) {
40         if (objectClass == null) {
41             throw new IllegalArgumentException JavaDoc("Null objectClass");
42         }
43
44         if (propertyName == null) {
45             throw new IllegalArgumentException JavaDoc("Null propertyName");
46         }
47
48         this.propertyName = propertyName;
49         this.nullValue = PropertyUtils.defaultNullValueForType(propertyType);
50
51         try {
52             PropertyDescriptor JavaDoc descriptor = new PropertyDescriptor JavaDoc(
53                     propertyName,
54                     objectClass);
55
56             this.readMethod = descriptor.getReadMethod();
57             this.writeMethod = descriptor.getWriteMethod();
58         }
59         catch (IntrospectionException JavaDoc e) {
60             throw new PropertyException(
61                     "Invalid bean property: " + propertyName,
62                     this,
63                     e);
64         }
65     }
66
67     public String JavaDoc getName() {
68         return propertyName;
69     }
70
71     /**
72      * @since 3.0
73      */

74     public Object JavaDoc getValue(Object JavaDoc object) throws PropertyException {
75         if (readMethod == null) {
76             throw new PropertyException("Property '"
77                     + propertyName
78                     + "' is not readable", this, object);
79         }
80
81         try {
82             return readMethod.invoke(object, null);
83         }
84         catch (Throwable JavaDoc th) {
85             throw new PropertyException(
86                     "Error reading property: " + propertyName,
87                     this,
88                     object,
89                     th);
90         }
91     }
92
93     /**
94      * @since 3.0
95      */

96     public void setValue(Object JavaDoc object, Object JavaDoc newValue) throws PropertyException {
97
98         if (writeMethod == null) {
99             throw new PropertyException("Property '"
100                     + propertyName
101                     + "' is not writable", this, object);
102         }
103
104         // this will take care of primitives.
105
if (newValue == null) {
106             newValue = this.nullValue;
107         }
108
109         try {
110             writeMethod.invoke(object, new Object JavaDoc[] {
111                 newValue
112             });
113         }
114         catch (Throwable JavaDoc th) {
115             throw new PropertyException(
116                     "Error reading property: " + propertyName,
117                     this,
118                     object,
119                     th);
120         }
121     }
122 }
123
Popular Tags