Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.commons.beanutils
Class PropertyUtilsBean  view PropertyUtilsBean download PropertyUtilsBean.java

java.lang.Object
  extended byorg.apache.commons.beanutils.PropertyUtilsBean

public class PropertyUtilsBean
extends java.lang.Object

Utility methods for using Java Reflection APIs to facilitate generic property getter and setter operations on Java objects. Much of this code was originally included in BeanUtils, but has been separated because of the volume of code involved.

In general, the objects that are examined and modified using these methods are expected to conform to the property getter and setter method naming conventions described in the JavaBeans Specification (Version 1.0.1). No data type conversions are performed, and there are no usage of any PropertyEditor classes that have been registered, although a convenient way to access the registered classes themselves is included.

For the purposes of this class, five formats for referencing a particular property value of a bean are defined, with the layout of an identifying String in parentheses:

Since:
1.7
Version:
$Revision: 1.14.2.1 $ $Date: 2004/07/27 21:31:00 $

Field Summary
private  org.apache.commons.collections.FastHashMap descriptorsCache
          The cache of PropertyDescriptor arrays for beans we have already introspected, keyed by the java.lang.Class of this object.
private  org.apache.commons.logging.Log log
          Log instance
private  org.apache.commons.collections.FastHashMap mappedDescriptorsCache
           
 
Constructor Summary
PropertyUtilsBean()
          Base constructor
 
Method Summary
 void clearDescriptors()
          Clear any cached property descriptors information for all classes loaded by any class loaders.
 void copyProperties(java.lang.Object dest, java.lang.Object orig)
          Copy property values from the "origin" bean to the "destination" bean for all cases where the property names are the same (even though the actual getter and setter methods might have been customized via BeanInfo classes).
 java.util.Map describe(java.lang.Object bean)
          Return the entire set of properties for which the specified bean provides a read method.
private  int findNextNestedIndex(java.lang.String expression)
           
 java.lang.Object getIndexedProperty(java.lang.Object bean, java.lang.String name)
          Return the value of the specified indexed property of the specified bean, with no type conversions.
 java.lang.Object getIndexedProperty(java.lang.Object bean, java.lang.String name, int index)
          Return the value of the specified indexed property of the specified bean, with no type conversions.
protected static PropertyUtilsBean getInstance()
           
 java.lang.Object getMappedProperty(java.lang.Object bean, java.lang.String name)
          Return the value of the specified mapped property of the specified bean, with no type conversions.
 java.lang.Object getMappedProperty(java.lang.Object bean, java.lang.String name, java.lang.String key)
          Return the value of the specified mapped property of the specified bean, with no type conversions.
 org.apache.commons.collections.FastHashMap getMappedPropertyDescriptors(java.lang.Class beanClass)
          Deprecated. This method should not be exposed
 org.apache.commons.collections.FastHashMap getMappedPropertyDescriptors(java.lang.Object bean)
          Deprecated. This method should not be exposed
 java.lang.Object getNestedProperty(java.lang.Object bean, java.lang.String name)
          Return the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.
 java.lang.Object getProperty(java.lang.Object bean, java.lang.String name)
          Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
 java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Object bean, java.lang.String name)
          Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor.
 java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class beanClass)
          Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.
 java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Object bean)
          Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.
 java.lang.Class getPropertyEditorClass(java.lang.Object bean, java.lang.String name)
          Return the Java Class repesenting the property editor class that has been registered for this property (if any).
 java.lang.Class getPropertyType(java.lang.Object bean, java.lang.String name)
          Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean.
 java.lang.reflect.Method getReadMethod(java.beans.PropertyDescriptor descriptor)
          Return an accessible property getter method for this property, if there is one; otherwise return null.
 java.lang.Object getSimpleProperty(java.lang.Object bean, java.lang.String name)
          Return the value of the specified simple property of the specified bean, with no type conversions.
 java.lang.reflect.Method getWriteMethod(java.beans.PropertyDescriptor descriptor)
          Return an accessible property setter method for this property, if there is one; otherwise return null.
private  java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object bean, java.lang.Object[] values)
          This just catches and wraps IllegalArgumentException.
 boolean isReadable(java.lang.Object bean, java.lang.String name)
          Return true if the specified property name identifies a readable property on the specified bean; otherwise, return false.
 boolean isWriteable(java.lang.Object bean, java.lang.String name)
          Return true if the specified property name identifies a writeable property on the specified bean; otherwise, return false.
 void setIndexedProperty(java.lang.Object bean, java.lang.String name, int index, java.lang.Object value)
          Set the value of the specified indexed property of the specified bean, with no type conversions.
 void setIndexedProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Set the value of the specified indexed property of the specified bean, with no type conversions.
 void setMappedProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Set the value of the specified mapped property of the specified bean, with no type conversions.
 void setMappedProperty(java.lang.Object bean, java.lang.String name, java.lang.String key, java.lang.Object value)
          Set the value of the specified mapped property of the specified bean, with no type conversions.
 void setNestedProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Set the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.
 void setProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
 void setSimpleProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Set the value of the specified simple property of the specified bean, with no type conversions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

descriptorsCache

private org.apache.commons.collections.FastHashMap descriptorsCache
The cache of PropertyDescriptor arrays for beans we have already introspected, keyed by the java.lang.Class of this object.


mappedDescriptorsCache

private org.apache.commons.collections.FastHashMap mappedDescriptorsCache

log

private org.apache.commons.logging.Log log
Log instance

Constructor Detail

PropertyUtilsBean

public PropertyUtilsBean()
Base constructor

Method Detail

getInstance

protected static PropertyUtilsBean getInstance()

clearDescriptors

public void clearDescriptors()
Clear any cached property descriptors information for all classes loaded by any class loaders. This is useful in cases where class loaders are thrown away to implement class reloading.


copyProperties

public void copyProperties(java.lang.Object dest,
                           java.lang.Object orig)
                    throws java.lang.IllegalAccessException,
                           java.lang.reflect.InvocationTargetException,
                           java.lang.NoSuchMethodException

Copy property values from the "origin" bean to the "destination" bean for all cases where the property names are the same (even though the actual getter and setter methods might have been customized via BeanInfo classes). No conversions are performed on the actual property values -- it is assumed that the values retrieved from the origin bean are assignment-compatible with the types expected by the destination bean.

If the origin "bean" is actually a Map, it is assumed to contain String-valued simple property names as the keys, pointing at the corresponding property values that will be set in the destination bean.Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.


describe

public java.util.Map describe(java.lang.Object bean)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException

Return the entire set of properties for which the specified bean provides a read method. This map contains the unconverted property values for all properties for which a read method is provided (i.e. where the getReadMethod() returns non-null).

FIXME - Does not account for mapped properties.


getIndexedProperty

public java.lang.Object getIndexedProperty(java.lang.Object bean,
                                           java.lang.String name)
                                    throws java.lang.IllegalAccessException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException
Return the value of the specified indexed property of the specified bean, with no type conversions. The zero-relative index of the required value must be included (in square brackets) as a suffix to the property name, or IllegalArgumentException will be thrown. In addition to supporting the JavaBeans specification, this method has been extended to support List objects as well.


getIndexedProperty

public java.lang.Object getIndexedProperty(java.lang.Object bean,
                                           java.lang.String name,
                                           int index)
                                    throws java.lang.IllegalAccessException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException
Return the value of the specified indexed property of the specified bean, with no type conversions. In addition to supporting the JavaBeans specification, this method has been extended to support List objects as well.


getMappedProperty

public java.lang.Object getMappedProperty(java.lang.Object bean,
                                          java.lang.String name)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.lang.NoSuchMethodException
Return the value of the specified mapped property of the specified bean, with no type conversions. The key of the required value must be included (in brackets) as a suffix to the property name, or IllegalArgumentException will be thrown.


getMappedProperty

public java.lang.Object getMappedProperty(java.lang.Object bean,
                                          java.lang.String name,
                                          java.lang.String key)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.lang.NoSuchMethodException
Return the value of the specified mapped property of the specified bean, with no type conversions.


getMappedPropertyDescriptors

public org.apache.commons.collections.FastHashMap getMappedPropertyDescriptors(java.lang.Class beanClass)
Deprecated. This method should not be exposed

Return the mapped property descriptors for this bean class.

FIXME - Does not work with DynaBeans.


getMappedPropertyDescriptors

public org.apache.commons.collections.FastHashMap getMappedPropertyDescriptors(java.lang.Object bean)
Deprecated. This method should not be exposed

Return the mapped property descriptors for this bean.

FIXME - Does not work with DynaBeans.


getNestedProperty

public java.lang.Object getNestedProperty(java.lang.Object bean,
                                          java.lang.String name)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.lang.NoSuchMethodException
Return the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.


getProperty

public java.lang.Object getProperty(java.lang.Object bean,
                                    java.lang.String name)
                             throws java.lang.IllegalAccessException,
                                    java.lang.reflect.InvocationTargetException,
                                    java.lang.NoSuchMethodException
Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.


getPropertyDescriptor

public java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Object bean,
                                                           java.lang.String name)
                                                    throws java.lang.IllegalAccessException,
                                                           java.lang.reflect.InvocationTargetException,
                                                           java.lang.NoSuchMethodException

Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor. This method resolves indexed and nested property references in the same manner as other methods in this class, except that if the last (or only) name element is indexed, the descriptor for the last resolved property itself is returned.

FIXME - Does not work with DynaBeans.


findNextNestedIndex

private int findNextNestedIndex(java.lang.String expression)

getPropertyDescriptors

public java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class beanClass)

Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.

FIXME - Does not work with DynaBeans.


getPropertyDescriptors

public java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Object bean)

Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.

FIXME - Does not work with DynaBeans.


getPropertyEditorClass

public java.lang.Class getPropertyEditorClass(java.lang.Object bean,
                                              java.lang.String name)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.NoSuchMethodException

Return the Java Class repesenting the property editor class that has been registered for this property (if any). This method follows the same name resolution rules used by getPropertyDescriptor(), so if the last element of a name reference is indexed, the property editor for the underlying property's class is returned.

Note that null will be returned if there is no property, or if there is no registered property editor class. Because this return value is ambiguous, you should determine the existence of the property itself by other means.

FIXME - Does not work with DynaBeans.


getPropertyType

public java.lang.Class getPropertyType(java.lang.Object bean,
                                       java.lang.String name)
                                throws java.lang.IllegalAccessException,
                                       java.lang.reflect.InvocationTargetException,
                                       java.lang.NoSuchMethodException
Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean. This method follows the same name resolution rules used by getPropertyDescriptor(), so if the last element of a name reference is indexed, the type of the property itself will be returned. If the last (or only) element has no property with the specified name, null is returned.


getReadMethod

public java.lang.reflect.Method getReadMethod(java.beans.PropertyDescriptor descriptor)

Return an accessible property getter method for this property, if there is one; otherwise return null.

FIXME - Does not work with DynaBeans.


getSimpleProperty

public java.lang.Object getSimpleProperty(java.lang.Object bean,
                                          java.lang.String name)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.lang.NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.


getWriteMethod

public java.lang.reflect.Method getWriteMethod(java.beans.PropertyDescriptor descriptor)

Return an accessible property setter method for this property, if there is one; otherwise return null.

FIXME - Does not work with DynaBeans.


isReadable

public boolean isReadable(java.lang.Object bean,
                          java.lang.String name)

Return true if the specified property name identifies a readable property on the specified bean; otherwise, return false.

Since:
BeanUtils 1.6

isWriteable

public boolean isWriteable(java.lang.Object bean,
                           java.lang.String name)

Return true if the specified property name identifies a writeable property on the specified bean; otherwise, return false.

Since:
BeanUtils 1.6

setIndexedProperty

public void setIndexedProperty(java.lang.Object bean,
                               java.lang.String name,
                               java.lang.Object value)
                        throws java.lang.IllegalAccessException,
                               java.lang.reflect.InvocationTargetException,
                               java.lang.NoSuchMethodException
Set the value of the specified indexed property of the specified bean, with no type conversions. The zero-relative index of the required value must be included (in square brackets) as a suffix to the property name, or IllegalArgumentException will be thrown. In addition to supporting the JavaBeans specification, this method has been extended to support List objects as well.


setIndexedProperty

public void setIndexedProperty(java.lang.Object bean,
                               java.lang.String name,
                               int index,
                               java.lang.Object value)
                        throws java.lang.IllegalAccessException,
                               java.lang.reflect.InvocationTargetException,
                               java.lang.NoSuchMethodException
Set the value of the specified indexed property of the specified bean, with no type conversions. In addition to supporting the JavaBeans specification, this method has been extended to support List objects as well.


setMappedProperty

public void setMappedProperty(java.lang.Object bean,
                              java.lang.String name,
                              java.lang.Object value)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException
Set the value of the specified mapped property of the specified bean, with no type conversions. The key of the value to set must be included (in brackets) as a suffix to the property name, or IllegalArgumentException will be thrown.


setMappedProperty

public void setMappedProperty(java.lang.Object bean,
                              java.lang.String name,
                              java.lang.String key,
                              java.lang.Object value)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException
Set the value of the specified mapped property of the specified bean, with no type conversions.


setNestedProperty

public void setNestedProperty(java.lang.Object bean,
                              java.lang.String name,
                              java.lang.Object value)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException
Set the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.


setProperty

public void setProperty(java.lang.Object bean,
                        java.lang.String name,
                        java.lang.Object value)
                 throws java.lang.IllegalAccessException,
                        java.lang.reflect.InvocationTargetException,
                        java.lang.NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.


setSimpleProperty

public void setSimpleProperty(java.lang.Object bean,
                              java.lang.String name,
                              java.lang.Object value)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException
Set the value of the specified simple property of the specified bean, with no type conversions.


invokeMethod

private java.lang.Object invokeMethod(java.lang.reflect.Method method,
                                      java.lang.Object bean,
                                      java.lang.Object[] values)
                               throws java.lang.IllegalAccessException,
                                      java.lang.reflect.InvocationTargetException
This just catches and wraps IllegalArgumentException.