KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.utils;
56
57 import org.jboss.logging.Logger;
58
59 import java.beans.IndexedPropertyDescriptor JavaDoc;
60 import java.beans.PropertyDescriptor JavaDoc;
61 import java.lang.reflect.Array JavaDoc;
62 import java.lang.reflect.InvocationTargetException JavaDoc;
63 import java.lang.reflect.Method JavaDoc;
64
65
66 /**
67  * This class represents a field/property in a value type (a class with either
68  * bean-style getters/setters or public fields).
69  * <p/>
70  * It is essentially a thin wrapper around the PropertyDescriptor from the
71  * JavaBean utilities. We wrap it with this class so that we can create
72  * the subclass FieldPropertyDescriptor and access public fields (who
73  * wouldn't have PropertyDescriptors normally) via the same interface.
74  * <p/>
75  * There are also some interesting tricks where indexed properties are
76  * concerned, mostly involving the fact that we manage the arrays here
77  * rather than relying on the value type class to do it itself.
78  *
79  * @author Rich Scheuerle <scheu@us.ibm.com>
80  * @author Glen Daniels (gdaniels@apache.org)
81  */

82 public class BeanPropertyDescriptor
83 {
84    private static Logger log = Logger.getLogger(BeanPropertyDescriptor.class.getName());
85
86    protected PropertyDescriptor JavaDoc myPD = null;
87
88    protected static final Object JavaDoc[] noArgs = new Object JavaDoc[]{};
89
90    /**
91     * Constructor (takes a PropertyDescriptor)
92     *
93     * @param pd
94     */

95    public BeanPropertyDescriptor(PropertyDescriptor JavaDoc pd)
96    {
97       myPD = pd;
98    }
99
100    /**
101     * Protected constructor for use by our children
102     */

103    protected BeanPropertyDescriptor()
104    {
105    }
106
107    /**
108     * Get our property name.
109     */

110    public String JavaDoc getName()
111    {
112       return myPD.getName();
113    }
114
115    /**
116     * Query if property is readable
117     *
118     * @return true if readable
119     */

120    public boolean isReadable()
121    {
122       return (myPD.getReadMethod() != null);
123    }
124
125    /**
126     * Query if property is writeable
127     *
128     * @return true if writeable
129     */

130    public boolean isWriteable()
131    {
132       return (myPD.getWriteMethod() != null);
133    }
134
135    /**
136     * Query if property is indexed.
137     *
138     * @return true if indexed methods exist
139     */

140    public boolean isIndexed()
141    {
142       return (myPD instanceof IndexedPropertyDescriptor JavaDoc);
143    }
144
145    /**
146     * Get the property value
147     *
148     * @param obj is the object
149     * @return the entire propery value
150     */

151    public Object JavaDoc get(Object JavaDoc obj)
152            throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
153    {
154
155       if (obj == null)
156          throw new IllegalArgumentException JavaDoc("Target object cannot be null");
157
158       Method JavaDoc readMethod = myPD.getReadMethod();
159       log.debug("get: [method=" + readMethod + "]");
160       if (readMethod != null)
161       {
162          Object JavaDoc retObj = readMethod.invoke(obj, noArgs);
163          log.debug("got: " + retObj);
164          return retObj;
165       }
166       throw new IllegalAccessException JavaDoc(Messages.getMessage("badGetter00"));
167    }
168
169    /**
170     * Set the property value
171     *
172     * @param obj is the object
173     * @param newValue is the new value
174     */

175    public void set(Object JavaDoc obj, Object JavaDoc newValue)
176            throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
177    {
178
179       if (obj == null)
180          throw new IllegalArgumentException JavaDoc("Target object cannot be null");
181
182       Method JavaDoc writeMethod = myPD.getWriteMethod();
183       log.debug("set: [val=" + newValue + ",method=" + writeMethod + "]");
184       if (writeMethod != null)
185       {
186          writeMethod.invoke(obj, new Object JavaDoc[]{newValue});
187       }
188       else
189       {
190          throw new IllegalAccessException JavaDoc(Messages.getMessage("badSetter00"));
191       }
192    }
193
194    /**
195     * Get an indexed property
196     *
197     * @param obj is the object
198     * @param i the index
199     * @return the object at the indicated index
200     */

201    public Object JavaDoc get(Object JavaDoc obj, int i)
202            throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
203    {
204
205       if (obj == null)
206          throw new IllegalArgumentException JavaDoc("Target object cannot be null");
207
208       if (!isIndexed())
209       {
210          return Array.get(get(obj), i);
211       }
212       else
213       {
214          IndexedPropertyDescriptor JavaDoc id = (IndexedPropertyDescriptor JavaDoc)myPD;
215          Method JavaDoc readMethod = id.getIndexedReadMethod();
216          log.debug("get: [method=" + readMethod + ",index=" + i + "]");
217          Object JavaDoc retObj = readMethod.invoke(obj, new Object JavaDoc[]{new Integer JavaDoc(i)});
218          log.debug("got: " + retObj);
219          return retObj;
220       }
221    }
222
223    /**
224     * Set an indexed property value
225     *
226     * @param obj is the object
227     * @param i the index
228     * @param newValue is the new value
229     */

230    public void set(Object JavaDoc obj, int i, Object JavaDoc newValue)
231            throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
232    {
233
234       if (obj == null)
235          throw new IllegalArgumentException JavaDoc("Target object cannot be null");
236
237       // Set the new value
238
if (isIndexed())
239       {
240          IndexedPropertyDescriptor JavaDoc id = (IndexedPropertyDescriptor JavaDoc)myPD;
241          growArrayToSize(obj, id.getIndexedPropertyType(), i);
242          id.getIndexedWriteMethod().invoke(obj,
243                  new Object JavaDoc[]{
244                     new Integer JavaDoc(i), newValue});
245       }
246       else
247       {
248          Object JavaDoc array = get(obj);
249          if (array == null || Array.getLength(array) <= i)
250          {
251             Class JavaDoc componentType = getType().getComponentType();
252             growArrayToSize(obj, componentType, i);
253             array = get(obj);
254          }
255          Array.set(array, i, newValue);
256       }
257    }
258
259    /**
260     * Grow the array
261     *
262     * @param obj
263     * @param componentType
264     * @param i
265     * @throws InvocationTargetException
266     * @throws IllegalAccessException
267     */

268    protected void growArrayToSize(Object JavaDoc obj, Class JavaDoc componentType, int i) throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
269    {
270
271       if (obj == null)
272          throw new IllegalArgumentException JavaDoc("Target object cannot be null");
273
274       // Get the entire array and make sure it is large enough
275
Object JavaDoc array = get(obj);
276       if (array == null || Array.getLength(array) <= i)
277       {
278          // Construct a larger array of the same type
279
Object JavaDoc newArray = Array.newInstance(componentType, i + 1);
280          // Copy over the old elements
281
if (array != null)
282          {
283             System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
284          }
285
286          // Set the object to use the larger array
287
set(obj, newArray);
288       }
289    }
290
291    /**
292     * Get the type of a property
293     *
294     * @return the type of the property
295     */

296    public Class JavaDoc getType()
297    {
298       if (isIndexed())
299       {
300          return ((IndexedPropertyDescriptor JavaDoc)myPD).getIndexedPropertyType();
301       }
302       else
303       {
304          return myPD.getPropertyType();
305       }
306    }
307 }
308
Popular Tags