KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > BeanPropertyDescriptor


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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.apache.axis.utils;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.commons.logging.Log;
20
21 import java.beans.IndexedPropertyDescriptor JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.lang.reflect.Array JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27
28 /**
29  * This class represents a field/property in a value type (a class with either
30  * bean-style getters/setters or public fields).
31  *
32  * It is essentially a thin wrapper around the PropertyDescriptor from the
33  * JavaBean utilities. We wrap it with this class so that we can create
34  * the subclass FieldPropertyDescriptor and access public fields (who
35  * wouldn't have PropertyDescriptors normally) via the same interface.
36  *
37  * There are also some interesting tricks where indexed properties are
38  * concerned, mostly involving the fact that we manage the arrays here
39  * rather than relying on the value type class to do it itself.
40  *
41  * @author Rich Scheuerle <scheu@us.ibm.com>
42  * @author Glen Daniels (gdaniels@apache.org)
43  **/

44 public class BeanPropertyDescriptor
45 {
46     protected static Log log =
47         LogFactory.getLog(BeanPropertyDescriptor.class.getName());
48
49     protected PropertyDescriptor JavaDoc myPD = null;
50
51     protected static final Object JavaDoc[] noArgs = new Object JavaDoc[] {};
52
53     /**
54      * Constructor (takes a PropertyDescriptor)
55      *
56      * @param pd
57      */

58     public BeanPropertyDescriptor(PropertyDescriptor JavaDoc pd) {
59         myPD = pd;
60     }
61
62     /**
63      * Protected constructor for use by our children
64      */

65     protected BeanPropertyDescriptor() {
66     }
67
68     /**
69      * Get our property name.
70      */

71     public String JavaDoc getName(){
72         return myPD.getName();
73     }
74
75     /**
76      * Query if property is readable
77      * @return true if readable
78      */

79     public boolean isReadable() {
80         return (myPD.getReadMethod() != null);
81     }
82
83     /**
84      * Query if property is writeable
85      * @return true if writeable
86      */

87     public boolean isWriteable() {
88         return (myPD.getWriteMethod() != null);
89     }
90
91     /**
92      * Query if property is indexed
93      *
94      * @return true if indexed methods exist
95      */

96     public boolean isIndexed() {
97         return (myPD instanceof IndexedPropertyDescriptor JavaDoc);
98     }
99
100     /**
101      * Query if property is indexed or if it' an array.
102      *
103      * @return true if indexed methods exist or if it's an array
104      */

105     public boolean isIndexedOrArray() {
106         return (isIndexed() || isArray());
107     }
108
109     /**
110      * Query if property is an array (excluded byte[]).
111      * @return true if it's an array (excluded byte[])
112      */

113     public boolean isArray() {
114         return ((myPD.getPropertyType() != null) && myPD.getPropertyType()
115                                    .isArray());
116     }
117
118     /**
119      * Get the property value
120      * @param obj is the object
121      * @return the entire propery value
122      */

123     public Object JavaDoc get(Object JavaDoc obj)
124         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
125         Method JavaDoc readMethod = myPD.getReadMethod();
126         if (readMethod != null) {
127             return readMethod.invoke(obj, noArgs);
128         } else {
129             throw new IllegalAccessException JavaDoc(Messages.getMessage("badGetter00"));
130         }
131     }
132
133     /**
134      * Set the property value
135      * @param obj is the object
136      * @param newValue is the new value
137      */

138     public void set(Object JavaDoc obj, Object JavaDoc newValue)
139         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
140         Method JavaDoc writeMethod = myPD.getWriteMethod();
141         if (writeMethod != null) {
142             writeMethod.invoke(obj, new Object JavaDoc[] {newValue});
143         } else {
144             throw new IllegalAccessException JavaDoc(Messages.getMessage("badSetter00"));
145         }
146     }
147
148     /**
149      * Get an indexed property
150      * @param obj is the object
151      * @param i the index
152      * @return the object at the indicated index
153      */

154     public Object JavaDoc get(Object JavaDoc obj, int i)
155         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
156         if (!isIndexed()) {
157             return Array.get(get(obj), i);
158         } else {
159             IndexedPropertyDescriptor JavaDoc id = (IndexedPropertyDescriptor JavaDoc)myPD;
160             return id.getIndexedReadMethod().invoke(obj,
161                                                     new Object JavaDoc[] {
162                                                         new Integer JavaDoc(i)});
163         }
164     }
165
166     /**
167      * Set an indexed property value
168      * @param obj is the object
169      * @param i the index
170      * @param newValue is the new value
171      */

172     public void set(Object JavaDoc obj, int i, Object JavaDoc newValue)
173         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
174         // Set the new value
175
if (isIndexed()) {
176             IndexedPropertyDescriptor JavaDoc id = (IndexedPropertyDescriptor JavaDoc)myPD;
177             growArrayToSize(obj, id.getIndexedPropertyType(), i);
178             id.getIndexedWriteMethod().invoke(obj,
179                                               new Object JavaDoc[] {
180                                                   new Integer JavaDoc(i), newValue});
181         } else {
182             growArrayToSize(obj, myPD.getPropertyType().getComponentType(), i);
183             Array.set(get(obj), i, newValue);
184         }
185     }
186
187     /**
188      * Grow the array
189      * @param obj
190      * @param componentType
191      * @param i
192      * @throws InvocationTargetException
193      * @throws IllegalAccessException
194      */

195     protected void growArrayToSize(Object JavaDoc obj, Class JavaDoc componentType, int i) throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
196         // Get the entire array and make sure it is large enough
197
Object JavaDoc array = get(obj);
198         if (array == null || Array.getLength(array) <= i) {
199             // Construct a larger array of the same type
200
Object JavaDoc newArray = Array.newInstance(componentType, i + 1);
201             // Copy over the old elements
202
if (array != null) {
203                 System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
204             }
205             // Set the object to use the larger array
206
set(obj, newArray);
207         }
208     }
209
210     /**
211      * Get the type of a property
212      * @return the type of the property
213      */

214     public Class JavaDoc getType() {
215         if (isIndexed()) {
216             return ((IndexedPropertyDescriptor JavaDoc)myPD).getIndexedPropertyType();
217         } else {
218             return myPD.getPropertyType();
219         }
220     }
221
222     public Class JavaDoc getActualType() {
223         return myPD.getPropertyType();
224     }
225 }
226
Popular Tags