001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgement may appear in the software itself,
024: * if and wherever such third-party acknowledgements normally appear.
025: *
026: * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache",
032: * "Apache" nor may "Apache" appear in their names without prior
033: * written permission of the Apache Software Foundation.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.opensymphony.workflow.designer.beanutils;
057:
058: import java.beans.PropertyDescriptor;
059: import java.lang.reflect.InvocationTargetException;
060: import java.lang.reflect.Method;
061: import java.util.Map;
062:
063: /**
064: * <p>Utility methods for using Java Reflection APIs to facilitate generic
065: * property getter and setter operations on Java objects.</p>
066: *
067: * <p>The implementations for these methods are provided by <code>PropertyUtilsBean</code>.
068: * For more details see {@link PropertyUtilsBean}.</p>
069: *
070: * @author Craig R. McClanahan
071: * @author Ralph Schaer
072: * @author Chris Audley
073: * @author Rey Fran�ois
074: * @author Gregor Ra�man
075: * @author Jan Sorensen
076: * @author Scott Sanders
077: * @see PropertyUtilsBean
078: */
079:
080: public class PropertyUtils {
081:
082: // ----------------------------------------------------- Manifest Constants
083:
084: /**
085: * The delimiter that preceeds the zero-relative subscript for an
086: * indexed reference.
087: */
088: public static final char INDEXED_DELIM = '[';
089:
090: /**
091: * The delimiter that follows the zero-relative subscript for an
092: * indexed reference.
093: */
094: public static final char INDEXED_DELIM2 = ']';
095:
096: /**
097: * The delimiter that preceeds the key of a mapped property.
098: */
099: public static final char MAPPED_DELIM = '(';
100:
101: /**
102: * The delimiter that follows the key of a mapped property.
103: */
104: public static final char MAPPED_DELIM2 = ')';
105:
106: /**
107: * The delimiter that separates the components of a nested reference.
108: */
109: public static final char NESTED_DELIM = '.';
110:
111: // ------------------------------------------------------- Static Variables
112:
113: // --------------------------------------------------------- Public Methods
114:
115: /**
116: * Clear any cached property descriptors information for all classes
117: * loaded by any class loaders. This is useful in cases where class
118: * loaders are thrown away to implement class reloading.
119: *
120: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
121: *
122: * @see PropertyUtilsBean#clearDescriptors
123: */
124: public static void clearDescriptors() {
125:
126: PropertyUtilsBean.getInstance().clearDescriptors();
127:
128: }
129:
130: /**
131: * <p>Copy property values from the "origin" bean to the "destination" bean
132: * for all cases where the property names are the same (even though the
133: * actual getter and setter methods might have been customized via
134: * <code>BeanInfo</code> classes).</p>
135: *
136: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
137: *
138: * @see PropertyUtilsBean#copyProperties
139: */
140: public static void copyProperties(Object dest, Object orig)
141: throws IllegalAccessException, InvocationTargetException,
142: NoSuchMethodException {
143:
144: PropertyUtilsBean.getInstance().copyProperties(dest, orig);
145: }
146:
147: /**
148: * <p>Return the entire set of properties for which the specified bean
149: * provides a read method.</p>
150: *
151: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
152: *
153: * @see PropertyUtilsBean#describe
154: */
155: public static Map describe(Object bean)
156: throws IllegalAccessException, InvocationTargetException,
157: NoSuchMethodException {
158:
159: return (PropertyUtilsBean.getInstance().describe(bean));
160:
161: }
162:
163: /**
164: * <p>Return the value of the specified indexed property of the specified
165: * bean, with no type conversions.</p>
166: *
167: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
168: *
169: * @see PropertyUtilsBean#getIndexedProperty(Object,String)
170: */
171: public static Object getIndexedProperty(Object bean, String name)
172: throws IllegalAccessException, InvocationTargetException,
173: NoSuchMethodException {
174:
175: return (PropertyUtilsBean.getInstance().getIndexedProperty(
176: bean, name));
177:
178: }
179:
180: /**
181: * <p>Return the value of the specified indexed property of the specified
182: * bean, with no type conversions.</p>
183: *
184: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
185: *
186: * @see PropertyUtilsBean#getIndexedProperty(Object,String, int)
187: */
188: public static Object getIndexedProperty(Object bean, String name,
189: int index) throws IllegalAccessException,
190: InvocationTargetException, NoSuchMethodException {
191:
192: return (PropertyUtilsBean.getInstance().getIndexedProperty(
193: bean, name, index));
194: }
195:
196: /**
197: * <p>Return the value of the specified mapped property of the
198: * specified bean, with no type conversions.</p>
199: *
200: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
201: *
202: * @see PropertyUtilsBean#getMappedProperty(Object,String)
203: */
204: public static Object getMappedProperty(Object bean, String name)
205: throws IllegalAccessException, InvocationTargetException,
206: NoSuchMethodException {
207:
208: return (PropertyUtilsBean.getInstance().getMappedProperty(bean,
209: name));
210:
211: }
212:
213: /**
214: * <p>Return the value of the specified mapped property of the specified
215: * bean, with no type conversions.</p>
216: *
217: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
218: *
219: * @see PropertyUtilsBean#getMappedProperty(Object,String, String)
220: */
221: public static Object getMappedProperty(Object bean, String name,
222: String key) throws IllegalAccessException,
223: InvocationTargetException, NoSuchMethodException {
224:
225: return PropertyUtilsBean.getInstance().getMappedProperty(bean,
226: name, key);
227:
228: }
229:
230: /**
231: * <p>Return the mapped property descriptors for this bean class.</p>
232: *
233: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
234: *
235: * @see PropertyUtilsBean#getMappedPropertyDescriptors(Class)
236: */
237: public static Map getMappedPropertyDescriptors(Class beanClass) {
238:
239: return PropertyUtilsBean.getInstance()
240: .getMappedPropertyDescriptors(beanClass);
241:
242: }
243:
244: /**
245: * <p>Return the mapped property descriptors for this bean.</p>
246: *
247: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
248: *
249: * @see PropertyUtilsBean#getMappedPropertyDescriptors(Object)
250: */
251: public static Map getMappedPropertyDescriptors(Object bean) {
252:
253: return PropertyUtilsBean.getInstance()
254: .getMappedPropertyDescriptors(bean);
255:
256: }
257:
258: /**
259: * <p>Return the value of the (possibly nested) property of the specified
260: * name, for the specified bean, with no type conversions.</p>
261: *
262: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
263: *
264: * @see PropertyUtilsBean#getNestedProperty
265: */
266: public static Object getNestedProperty(Object bean, String name)
267: throws IllegalAccessException, InvocationTargetException,
268: NoSuchMethodException {
269:
270: return PropertyUtilsBean.getInstance().getNestedProperty(bean,
271: name);
272:
273: }
274:
275: /**
276: * <p>Return the value of the specified property of the specified bean,
277: * no matter which property reference format is used, with no
278: * type conversions.</p>
279: *
280: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
281: *
282: * @see PropertyUtilsBean#getProperty
283: */
284: public static Object getProperty(Object bean, String name)
285: throws IllegalAccessException, InvocationTargetException,
286: NoSuchMethodException {
287:
288: return (PropertyUtilsBean.getInstance().getProperty(bean, name));
289:
290: }
291:
292: /**
293: * <p>Retrieve the property descriptor for the specified property of the
294: * specified bean, or return <code>null</code> if there is no such
295: * descriptor.</p>
296: *
297: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
298: *
299: * @see PropertyUtilsBean#getPropertyDescriptor
300: */
301: public static PropertyDescriptor getPropertyDescriptor(Object bean,
302: String name) throws IllegalAccessException,
303: InvocationTargetException, NoSuchMethodException {
304:
305: return PropertyUtilsBean.getInstance().getPropertyDescriptor(
306: bean, name);
307:
308: }
309:
310: /**
311: * <p>Retrieve the property descriptors for the specified class,
312: * introspecting and caching them the first time a particular bean class
313: * is encountered.</p>
314: *
315: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
316: *
317: * @see PropertyUtilsBean#getPropertyDescriptors(Class)
318: */
319: public static PropertyDescriptor[] getPropertyDescriptors(
320: Class beanClass) {
321:
322: return PropertyUtilsBean.getInstance().getPropertyDescriptors(
323: beanClass);
324:
325: }
326:
327: /**
328: * <p>Retrieve the property descriptors for the specified bean,
329: * introspecting and caching them the first time a particular bean class
330: * is encountered.</p>
331: *
332: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
333: *
334: * @see PropertyUtilsBean#getPropertyDescriptors(Object)
335: */
336: public static PropertyDescriptor[] getPropertyDescriptors(
337: Object bean) {
338:
339: return PropertyUtilsBean.getInstance().getPropertyDescriptors(
340: bean);
341:
342: }
343:
344: /**
345: * <p>Return the Java Class repesenting the property editor class that has
346: * been registered for this property (if any).</p>
347: *
348: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
349: *
350: * @see PropertyUtilsBean#getPropertyEditorClass(Object,String)
351: */
352: public static Class getPropertyEditorClass(Object bean, String name)
353: throws IllegalAccessException, InvocationTargetException,
354: NoSuchMethodException {
355:
356: return PropertyUtilsBean.getInstance().getPropertyEditorClass(
357: bean, name);
358:
359: }
360:
361: /**
362: * <p>Return the Java Class representing the property type of the specified
363: * property, or <code>null</code> if there is no such property for the
364: * specified bean.</p>
365: *
366: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
367: *
368: * @see PropertyUtilsBean#getPropertyType
369: */
370: public static Class getPropertyType(Object bean, String name)
371: throws IllegalAccessException, InvocationTargetException,
372: NoSuchMethodException {
373:
374: return PropertyUtilsBean.getInstance().getPropertyType(bean,
375: name);
376: }
377:
378: /**
379: * <p>Return an accessible property getter method for this property,
380: * if there is one; otherwise return <code>null</code>.</p>
381: *
382: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
383: *
384: * @see PropertyUtilsBean#getReadMethod
385: */
386: public static Method getReadMethod(PropertyDescriptor descriptor) {
387:
388: return (PropertyUtilsBean.getInstance()
389: .getReadMethod(descriptor));
390:
391: }
392:
393: /**
394: * <p>Return the value of the specified simple property of the specified
395: * bean, with no type conversions.</p>
396: *
397: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
398: *
399: * @see PropertyUtilsBean#getSimpleProperty
400: */
401: public static Object getSimpleProperty(Object bean, String name)
402: throws IllegalAccessException, InvocationTargetException,
403: NoSuchMethodException {
404:
405: return PropertyUtilsBean.getInstance().getSimpleProperty(bean,
406: name);
407:
408: }
409:
410: /**
411: * <p>Return an accessible property setter method for this property,
412: * if there is one; otherwise return <code>null</code>.</p>
413: *
414: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
415: *
416: * @see PropertyUtilsBean#getWriteMethod
417: */
418: public static Method getWriteMethod(PropertyDescriptor descriptor) {
419:
420: return PropertyUtilsBean.getInstance().getWriteMethod(
421: descriptor);
422:
423: }
424:
425: /**
426: * <p>Return <code>true</code> if the specified property name identifies
427: * a readable property on the specified bean; otherwise, return
428: * <code>false</code>.</p>
429: *
430: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
431: *
432: * @see PropertyUtilsBean#isReadable
433: * @since BeanUtils 1.6
434: */
435: public static boolean isReadable(Object bean, String name) {
436:
437: return PropertyUtilsBean.getInstance().isReadable(bean, name);
438: }
439:
440: /**
441: * <p>Return <code>true</code> if the specified property name identifies
442: * a writeable property on the specified bean; otherwise, return
443: * <code>false</code>.</p>
444: *
445: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
446: *
447: * @see PropertyUtilsBean#isWriteable
448: * @since BeanUtils 1.6
449: */
450: public static boolean isWriteable(Object bean, String name) {
451:
452: return PropertyUtilsBean.getInstance().isWriteable(bean, name);
453: }
454:
455: /**
456: * <p>Sets the value of the specified indexed property of the specified
457: * bean, with no type conversions.</p>
458: *
459: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
460: *
461: * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
462: */
463: public static void setIndexedProperty(Object bean, String name,
464: Object value) throws IllegalAccessException,
465: InvocationTargetException, NoSuchMethodException {
466:
467: PropertyUtilsBean.getInstance().setIndexedProperty(bean, name,
468: value);
469:
470: }
471:
472: /**
473: * <p>Sets the value of the specified indexed property of the specified
474: * bean, with no type conversions.</p>
475: *
476: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
477: *
478: * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
479: */
480: public static void setIndexedProperty(Object bean, String name,
481: int index, Object value) throws IllegalAccessException,
482: InvocationTargetException, NoSuchMethodException {
483:
484: PropertyUtilsBean.getInstance().setIndexedProperty(bean, name,
485: index, value);
486: }
487:
488: /**
489: * <p>Sets the value of the specified mapped property of the
490: * specified bean, with no type conversions.</p>
491: *
492: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
493: *
494: * @see PropertyUtilsBean#setMappedProperty(Object, String, Object)
495: */
496: public static void setMappedProperty(Object bean, String name,
497: Object value) throws IllegalAccessException,
498: InvocationTargetException, NoSuchMethodException {
499:
500: PropertyUtilsBean.getInstance().setMappedProperty(bean, name,
501: value);
502: }
503:
504: /**
505: * <p>Sets the value of the specified mapped property of the specified
506: * bean, with no type conversions.</p>
507: *
508: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
509: *
510: * @see PropertyUtilsBean#setMappedProperty(Object, String, String, Object)
511: */
512: public static void setMappedProperty(Object bean, String name,
513: String key, Object value) throws IllegalAccessException,
514: InvocationTargetException, NoSuchMethodException {
515:
516: PropertyUtilsBean.getInstance().setMappedProperty(bean, name,
517: key, value);
518: }
519:
520: /**
521: * <p>Sets the value of the (possibly nested) property of the specified
522: * name, for the specified bean, with no type conversions.</p>
523: *
524: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
525: *
526: * @see PropertyUtilsBean#setNestedProperty
527: */
528: public static void setNestedProperty(Object bean, String name,
529: Object value) throws IllegalAccessException,
530: InvocationTargetException, NoSuchMethodException {
531:
532: PropertyUtilsBean.getInstance().setNestedProperty(bean, name,
533: value);
534: }
535:
536: /**
537: * <p>Set the value of the specified property of the specified bean,
538: * no matter which property reference format is used, with no
539: * type conversions.</p>
540: *
541: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
542: *
543: * @see PropertyUtilsBean#setProperty
544: */
545: public static void setProperty(Object bean, String name,
546: Object value) throws IllegalAccessException,
547: InvocationTargetException, NoSuchMethodException {
548:
549: PropertyUtilsBean.getInstance().setProperty(bean, name, value);
550:
551: }
552:
553: /**
554: * <p>Set the value of the specified simple property of the specified bean,
555: * with no type conversions.</p>
556: *
557: * <p>For more details see <code>PropertyUtilsBean</code>.</p>
558: *
559: * @see PropertyUtilsBean#setSimpleProperty
560: */
561: public static void setSimpleProperty(Object bean, String name,
562: Object value) throws IllegalAccessException,
563: InvocationTargetException, NoSuchMethodException {
564:
565: PropertyUtilsBean.getInstance().setSimpleProperty(bean, name,
566: value);
567: }
568:
569: }
|