org.sape.carbon.core.util.reflection
Class BeanUtil

java.lang.Object
  |
  +--org.sape.carbon.core.util.reflection.BeanUtil

public final class BeanUtil
extends Object

This class implements the ability to get and set properties on a bean. This included the concept of embedded properties that may be referenced like Bean.property.property.property.

Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.11 $ ($Author: dvoet $)
Author:
Greg Hinkle, January 2002

Field Summary
private static Object[] NO_ARGUMENTS_ARRAY
          an empty object array used for null parameter method reflection
private static Class[] NO_PARAMETERS_ARRAY
          An empty class array used for null parameter method reflection
private static String PROPERTY_SEPARATOR
          String used to separate multiple properties inside of embedded beans.
 
Constructor Summary
private BeanUtil()
          The constructor is private so that new cannot be used.
 
Method Summary
static Object getObjectAttribute(Object bean, String propertyNames)
          Gets the specified attribute from the specified object.
private static PropertyDescriptor getPropertyDescriptor(String propertyName, Class beanClass)
          Retreives a property descriptor object for a given property.
static void setObjectAttribute(Object bean, String propertyNames, Object value)
          Sets the specified attribute on the specified object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_SEPARATOR

private static final String PROPERTY_SEPARATOR
String used to separate multiple properties inside of embedded beans.

See Also:
Constant Field Values

NO_PARAMETERS_ARRAY

private static Class[] NO_PARAMETERS_ARRAY
An empty class array used for null parameter method reflection


NO_ARGUMENTS_ARRAY

private static Object[] NO_ARGUMENTS_ARRAY
an empty object array used for null parameter method reflection

Constructor Detail

BeanUtil

private BeanUtil()
The constructor is private so that new cannot be used.

Method Detail

getPropertyDescriptor

private static final PropertyDescriptor getPropertyDescriptor(String propertyName,
                                                              Class beanClass)
Retreives a property descriptor object for a given property.

Uses the classes in java.beans to get back a descriptor for a property. Read-only and write-only properties will have a slower return time.

Parameters:
propertyName - The programmatic name of the property
beanClass - The Class object for the target bean. For example sun.beans.OurButton.class.
Returns:
a PropertyDescriptor for a property that follows the standard Java naming conventions.
Throws:
PropertyNotFoundException - indicates that the property could not be found on the bean class.

getObjectAttribute

public static Object getObjectAttribute(Object bean,
                                        String propertyNames)
                                 throws NoSuchMethodException,
                                        InvocationTargetException,
                                        IllegalAccessException

Gets the specified attribute from the specified object. For example, getObjectAttribute(o, "address.line1") will return the result of calling o.getAddress().getLine1().

The attribute specified may contain as many levels as you like. If at any time a null reference is acquired by calling one of the successive getter methods, then the return value from this method is also null.

When reading from a boolean property the underlying bean introspector first looks for an is<Property> read method, not finding one it will still look for a get<Property> read method. Not finding either, the property is considered write-only.

Parameters:
bean - the bean to set the property on
propertyNames - the name of the propertie(s) to retrieve. If this is null or the empty string, then bean will be returned.
Returns:
the object value of the bean attribute
Throws:
PropertyNotFoundException - indicates the the given property could not be found on the bean
NoSuchMethodException - Not thrown
InvocationTargetException - if a specified getter method throws an exception.
IllegalAccessException - if a getter method is not public or property is write-only.

setObjectAttribute

public static void setObjectAttribute(Object bean,
                                      String propertyNames,
                                      Object value)
                               throws IllegalAccessException,
                                      IllegalArgumentException,
                                      InvocationTargetException,
                                      NoSuchMethodException

Sets the specified attribute on the specified object. For example, getObjectAttribute(o, "address.line1", value) will call o.getAddress().setLine1(value).

The attribute specified may contain as many levels as you like. If at any time a null reference is acquired by calling one of the successive getter methods, then a NullPointerException is thrown.

Parameters:
bean - the bean to call the getters on
propertyNames - the name of the attribute(s) to set. If this is null or the empty string, then an exception is thrown.
value - the value of the object to set on the bean property
Throws:
PropertyNotFoundException - indicates the the given property could not be found on the bean
IllegalArgumentException - if the supplied parameter is not of a valid type
NoSuchMethodException - never
IllegalAccessException - if a getter or setter method is not public or property is read-only.
InvocationTargetException - if a specified getter method throws an exception.


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.