KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > beanutils > PropertyUtils


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

61
62
63 package com.opensymphony.workflow.designer.beanutils;
64
65 import java.beans.PropertyDescriptor JavaDoc;
66 import java.lang.reflect.InvocationTargetException JavaDoc;
67 import java.lang.reflect.Method JavaDoc;
68 import java.util.Map JavaDoc;
69
70 /**
71  * <p>Utility methods for using Java Reflection APIs to facilitate generic
72  * property getter and setter operations on Java objects.</p>
73  *
74  * <p>The implementations for these methods are provided by <code>PropertyUtilsBean</code>.
75  * For more details see {@link PropertyUtilsBean}.</p>
76  *
77  * @author Craig R. McClanahan
78  * @author Ralph Schaer
79  * @author Chris Audley
80  * @author Rey François
81  * @author Gregor Raıman
82  * @author Jan Sorensen
83  * @author Scott Sanders
84  * @version $Revision: 1.1 $ $Date: 2003/12/06 18:05:58 $
85  * @see PropertyUtilsBean
86  */

87
88 public class PropertyUtils {
89
90
91     // ----------------------------------------------------- Manifest Constants
92

93
94     /**
95      * The delimiter that preceeds the zero-relative subscript for an
96      * indexed reference.
97      */

98     public static final char INDEXED_DELIM = '[';
99
100
101     /**
102      * The delimiter that follows the zero-relative subscript for an
103      * indexed reference.
104      */

105     public static final char INDEXED_DELIM2 = ']';
106
107
108     /**
109      * The delimiter that preceeds the key of a mapped property.
110      */

111     public static final char MAPPED_DELIM = '(';
112
113
114     /**
115      * The delimiter that follows the key of a mapped property.
116      */

117     public static final char MAPPED_DELIM2 = ')';
118
119
120     /**
121      * The delimiter that separates the components of a nested reference.
122      */

123     public static final char NESTED_DELIM = '.';
124
125
126     // ------------------------------------------------------- Static Variables
127

128
129     // --------------------------------------------------------- Public Methods
130

131
132     /**
133      * Clear any cached property descriptors information for all classes
134      * loaded by any class loaders. This is useful in cases where class
135      * loaders are thrown away to implement class reloading.
136      *
137      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
138      *
139      * @see PropertyUtilsBean#clearDescriptors
140      */

141     public static void clearDescriptors() {
142
143         PropertyUtilsBean.getInstance().clearDescriptors();
144
145     }
146
147
148     /**
149      * <p>Copy property values from the "origin" bean to the "destination" bean
150      * for all cases where the property names are the same (even though the
151      * actual getter and setter methods might have been customized via
152      * <code>BeanInfo</code> classes).</p>
153      *
154      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
155      *
156      * @see PropertyUtilsBean#copyProperties
157      */

158     public static void copyProperties(Object JavaDoc dest, Object JavaDoc orig)
159             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
160             NoSuchMethodException JavaDoc {
161
162         PropertyUtilsBean.getInstance().copyProperties(dest, orig);
163     }
164
165
166     /**
167      * <p>Return the entire set of properties for which the specified bean
168      * provides a read method.</p>
169      *
170      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
171      *
172      * @see PropertyUtilsBean#describe
173      */

174     public static Map JavaDoc describe(Object JavaDoc bean)
175             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
176             NoSuchMethodException JavaDoc {
177
178         return (PropertyUtilsBean.getInstance().describe(bean));
179
180     }
181
182
183     /**
184      * <p>Return the value of the specified indexed property of the specified
185      * bean, with no type conversions.</p>
186      *
187      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
188      *
189      * @see PropertyUtilsBean#getIndexedProperty(Object,String)
190      */

191     public static Object JavaDoc getIndexedProperty(Object JavaDoc bean, String JavaDoc name)
192             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
193             NoSuchMethodException JavaDoc {
194
195         return (PropertyUtilsBean.getInstance().getIndexedProperty(bean, name));
196
197     }
198
199
200     /**
201      * <p>Return the value of the specified indexed property of the specified
202      * bean, with no type conversions.</p>
203      *
204      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
205      *
206      * @see PropertyUtilsBean#getIndexedProperty(Object,String, int)
207      */

208     public static Object JavaDoc getIndexedProperty(Object JavaDoc bean,
209                                             String JavaDoc name, int index)
210             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
211             NoSuchMethodException JavaDoc {
212
213         return (PropertyUtilsBean.getInstance().getIndexedProperty(bean, name, index));
214     }
215
216
217     /**
218      * <p>Return the value of the specified mapped property of the
219      * specified bean, with no type conversions.</p>
220      *
221      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
222      *
223      * @see PropertyUtilsBean#getMappedProperty(Object,String)
224      */

225     public static Object JavaDoc getMappedProperty(Object JavaDoc bean, String JavaDoc name)
226             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
227             NoSuchMethodException JavaDoc {
228
229         return (PropertyUtilsBean.getInstance().getMappedProperty(bean, name));
230
231     }
232
233
234     /**
235      * <p>Return the value of the specified mapped property of the specified
236      * bean, with no type conversions.</p>
237      *
238      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
239      *
240      * @see PropertyUtilsBean#getMappedProperty(Object,String, String)
241      */

242     public static Object JavaDoc getMappedProperty(Object JavaDoc bean,
243                                            String JavaDoc name, String JavaDoc key)
244             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
245             NoSuchMethodException JavaDoc {
246
247         return PropertyUtilsBean.getInstance().getMappedProperty(bean, name, key);
248
249     }
250
251
252     /**
253      * <p>Return the mapped property descriptors for this bean class.</p>
254      *
255      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
256      *
257      * @see PropertyUtilsBean#getMappedPropertyDescriptors(Class)
258      */

259     public static Map JavaDoc getMappedPropertyDescriptors(Class JavaDoc beanClass) {
260
261         return PropertyUtilsBean.getInstance().getMappedPropertyDescriptors(beanClass);
262
263     }
264
265
266     /**
267      * <p>Return the mapped property descriptors for this bean.</p>
268      *
269      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
270      *
271      * @see PropertyUtilsBean#getMappedPropertyDescriptors(Object)
272      */

273     public static Map JavaDoc getMappedPropertyDescriptors(Object JavaDoc bean) {
274
275     return PropertyUtilsBean.getInstance().getMappedPropertyDescriptors(bean);
276
277     }
278
279
280     /**
281      * <p>Return the value of the (possibly nested) property of the specified
282      * name, for the specified bean, with no type conversions.</p>
283      *
284      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
285      *
286      * @see PropertyUtilsBean#getNestedProperty
287      */

288     public static Object JavaDoc getNestedProperty(Object JavaDoc bean, String JavaDoc name)
289             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
290             NoSuchMethodException JavaDoc {
291
292         return PropertyUtilsBean.getInstance().getNestedProperty(bean, name);
293
294     }
295
296
297     /**
298      * <p>Return the value of the specified property of the specified bean,
299      * no matter which property reference format is used, with no
300      * type conversions.</p>
301      *
302      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
303      *
304      * @see PropertyUtilsBean#getProperty
305      */

306     public static Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc name)
307             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
308             NoSuchMethodException JavaDoc {
309
310         return (PropertyUtilsBean.getInstance().getProperty(bean, name));
311
312     }
313
314
315     /**
316      * <p>Retrieve the property descriptor for the specified property of the
317      * specified bean, or return <code>null</code> if there is no such
318      * descriptor.</p>
319      *
320      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
321      *
322      * @see PropertyUtilsBean#getPropertyDescriptor
323      */

324     public static PropertyDescriptor JavaDoc getPropertyDescriptor(Object JavaDoc bean,
325                                                            String JavaDoc name)
326             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
327             NoSuchMethodException JavaDoc {
328
329         return PropertyUtilsBean.getInstance().getPropertyDescriptor(bean, name);
330
331     }
332
333
334     /**
335      * <p>Retrieve the property descriptors for the specified class,
336      * introspecting and caching them the first time a particular bean class
337      * is encountered.</p>
338      *
339      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
340      *
341      * @see PropertyUtilsBean#getPropertyDescriptors(Class)
342      */

343     public static PropertyDescriptor JavaDoc[]
344             getPropertyDescriptors(Class JavaDoc beanClass) {
345
346         return PropertyUtilsBean.getInstance().getPropertyDescriptors(beanClass);
347
348     }
349
350
351     /**
352      * <p>Retrieve the property descriptors for the specified bean,
353      * introspecting and caching them the first time a particular bean class
354      * is encountered.</p>
355      *
356      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
357      *
358      * @see PropertyUtilsBean#getPropertyDescriptors(Object)
359      */

360     public static PropertyDescriptor JavaDoc[] getPropertyDescriptors(Object JavaDoc bean) {
361
362         return PropertyUtilsBean.getInstance().getPropertyDescriptors(bean);
363
364     }
365
366
367     /**
368      * <p>Return the Java Class repesenting the property editor class that has
369      * been registered for this property (if any).</p>
370      *
371      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
372      *
373      * @see PropertyUtilsBean#getPropertyEditorClass(Object,String)
374      */

375     public static Class JavaDoc getPropertyEditorClass(Object JavaDoc bean, String JavaDoc name)
376             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
377             NoSuchMethodException JavaDoc {
378
379     return PropertyUtilsBean.getInstance().getPropertyEditorClass(bean, name);
380
381     }
382
383
384     /**
385      * <p>Return the Java Class representing the property type of the specified
386      * property, or <code>null</code> if there is no such property for the
387      * specified bean.</p>
388      *
389      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
390      *
391      * @see PropertyUtilsBean#getPropertyType
392      */

393     public static Class JavaDoc getPropertyType(Object JavaDoc bean, String JavaDoc name)
394             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
395             NoSuchMethodException JavaDoc {
396
397         return PropertyUtilsBean.getInstance().getPropertyType(bean, name);
398     }
399
400
401     /**
402      * <p>Return an accessible property getter method for this property,
403      * if there is one; otherwise return <code>null</code>.</p>
404      *
405      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
406      *
407      * @see PropertyUtilsBean#getReadMethod
408      */

409     public static Method JavaDoc getReadMethod(PropertyDescriptor JavaDoc descriptor) {
410
411         return (PropertyUtilsBean.getInstance().getReadMethod(descriptor));
412
413     }
414
415
416     /**
417      * <p>Return the value of the specified simple property of the specified
418      * bean, with no type conversions.</p>
419      *
420      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
421      *
422      * @see PropertyUtilsBean#getSimpleProperty
423      */

424     public static Object JavaDoc getSimpleProperty(Object JavaDoc bean, String JavaDoc name)
425             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
426             NoSuchMethodException JavaDoc {
427
428         return PropertyUtilsBean.getInstance().getSimpleProperty(bean, name);
429
430     }
431
432
433     /**
434      * <p>Return an accessible property setter method for this property,
435      * if there is one; otherwise return <code>null</code>.</p>
436      *
437      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
438      *
439      * @see PropertyUtilsBean#getWriteMethod
440      */

441     public static Method JavaDoc getWriteMethod(PropertyDescriptor JavaDoc descriptor) {
442
443         return PropertyUtilsBean.getInstance().getWriteMethod(descriptor);
444
445     }
446
447
448     /**
449      * <p>Return <code>true</code> if the specified property name identifies
450      * a readable property on the specified bean; otherwise, return
451      * <code>false</code>.</p>
452      *
453      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
454      *
455      * @see PropertyUtilsBean#isReadable
456      * @since BeanUtils 1.6
457      */

458     public static boolean isReadable(Object JavaDoc bean, String JavaDoc name) {
459
460         return PropertyUtilsBean.getInstance().isReadable(bean, name);
461     }
462
463
464     /**
465      * <p>Return <code>true</code> if the specified property name identifies
466      * a writeable property on the specified bean; otherwise, return
467      * <code>false</code>.</p>
468      *
469      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
470      *
471      * @see PropertyUtilsBean#isWriteable
472      * @since BeanUtils 1.6
473      */

474     public static boolean isWriteable(Object JavaDoc bean, String JavaDoc name) {
475
476     return PropertyUtilsBean.getInstance().isWriteable(bean, name);
477     }
478
479
480     /**
481      * <p>Sets the value of the specified indexed property of the specified
482      * bean, with no type conversions.</p>
483      *
484      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
485      *
486      * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
487      */

488     public static void setIndexedProperty(Object JavaDoc bean, String JavaDoc name,
489                                           Object JavaDoc value)
490             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
491             NoSuchMethodException JavaDoc {
492
493         PropertyUtilsBean.getInstance().setIndexedProperty(bean, name, value);
494
495     }
496
497
498     /**
499      * <p>Sets the value of the specified indexed property of the specified
500      * bean, with no type conversions.</p>
501      *
502      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
503      *
504      * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
505      */

506     public static void setIndexedProperty(Object JavaDoc bean, String JavaDoc name,
507                                           int index, Object JavaDoc value)
508             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
509             NoSuchMethodException JavaDoc {
510
511         PropertyUtilsBean.getInstance().setIndexedProperty(bean, name, index, value);
512     }
513
514
515     /**
516      * <p>Sets the value of the specified mapped property of the
517      * specified bean, with no type conversions.</p>
518      *
519      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
520      *
521      * @see PropertyUtilsBean#setMappedProperty(Object, String, Object)
522      */

523     public static void setMappedProperty(Object JavaDoc bean, String JavaDoc name,
524                                          Object JavaDoc value)
525             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
526             NoSuchMethodException JavaDoc {
527
528         PropertyUtilsBean.getInstance().setMappedProperty(bean, name, value);
529     }
530
531
532     /**
533      * <p>Sets the value of the specified mapped property of the specified
534      * bean, with no type conversions.</p>
535      *
536      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
537      *
538      * @see PropertyUtilsBean#setMappedProperty(Object, String, String, Object)
539      */

540     public static void setMappedProperty(Object JavaDoc bean, String JavaDoc name,
541                                          String JavaDoc key, Object JavaDoc value)
542             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
543             NoSuchMethodException JavaDoc {
544
545         PropertyUtilsBean.getInstance().setMappedProperty(bean, name, key, value);
546     }
547
548
549     /**
550      * <p>Sets the value of the (possibly nested) property of the specified
551      * name, for the specified bean, with no type conversions.</p>
552      *
553      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
554      *
555      * @see PropertyUtilsBean#setNestedProperty
556      */

557     public static void setNestedProperty(Object JavaDoc bean,
558                                          String JavaDoc name, Object JavaDoc value)
559             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
560             NoSuchMethodException JavaDoc {
561
562         PropertyUtilsBean.getInstance().setNestedProperty(bean, name, value);
563     }
564
565
566     /**
567      * <p>Set the value of the specified property of the specified bean,
568      * no matter which property reference format is used, with no
569      * type conversions.</p>
570      *
571      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
572      *
573      * @see PropertyUtilsBean#setProperty
574      */

575     public static void setProperty(Object JavaDoc bean, String JavaDoc name, Object JavaDoc value)
576             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
577             NoSuchMethodException JavaDoc {
578
579         PropertyUtilsBean.getInstance().setProperty(bean, name, value);
580
581     }
582
583
584     /**
585      * <p>Set the value of the specified simple property of the specified bean,
586      * with no type conversions.</p>
587      *
588      * <p>For more details see <code>PropertyUtilsBean</code>.</p>
589      *
590      * @see PropertyUtilsBean#setSimpleProperty
591      */

592     public static void setSimpleProperty(Object JavaDoc bean,
593                                          String JavaDoc name, Object JavaDoc value)
594             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
595             NoSuchMethodException JavaDoc {
596
597         PropertyUtilsBean.getInstance().setSimpleProperty(bean, name, value);
598     }
599
600
601 }
602
Popular Tags