org.springframework.beans
Class BeanUtils

java.lang.Object
  extended by org.springframework.beans.BeanUtils

public abstract class BeanUtils
extends Object

Static convenience methods for JavaBeans: for instantiating beans, checking bean property types, copying bean properties, etc.

Mainly for use within the framework, but to some degree also useful for application classes.

Author:
Rod Johnson, Juergen Hoeller, Rob Harrop

Constructor Summary
BeanUtils()
           
 
Method Summary
static String canonicalName(String propertyName)
          Deprecated. as of Spring 1.2.9, in favor of PropertyAccessorUtils.canonicalPropertyName(String)
static void copyProperties(Object source, Object target)
          Copy the property values of the given source bean into the target bean.
static void copyProperties(Object source, Object target, Class editable)
          Copy the property values of the given source bean into the given target bean, only setting properties defined in the given "editable" class (or interface).
static void copyProperties(Object source, Object target, String[] ignoreProperties)
          Copy the property values of the given source bean into the given target bean, ignoring the given "ignoreProperties".
static Method findDeclaredMethod(Class clazz, String methodName, Class[] paramTypes)
          Find a method with the given method name and the given parameter types, declared on the given class or one of its superclasses.
static Method findDeclaredMethodWithMinimalParameters(Class clazz, String methodName)
          Find a method with the given method name and minimal parameters (best case: none), declared on the given class or one of its superclasses.
static Method findMethod(Class clazz, String methodName, Class[] paramTypes)
          Find a method with the given method name and the given parameter types, declared on the given class or one of its superclasses.
static Method findMethodWithMinimalParameters(Class clazz, String methodName)
          Find a method with the given method name and minimal parameters (best case: none), declared on the given class or one of its superclasses.
static PropertyDescriptor findPropertyForMethod(Method method)
          Find a JavaBeans PropertyDescriptor for the given method, with the method either being the read method or the write method for that bean property.
static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName)
          Retrieve the JavaBeans PropertyDescriptors for the given property.
static PropertyDescriptor[] getPropertyDescriptors(Class clazz)
          Retrieve the JavaBeans PropertyDescriptors of a given class.
static Object instantiateClass(Class clazz)
          Convenience method to instantiate a class using its no-arg constructor.
static Object instantiateClass(Constructor ctor, Object[] args)
          Convenience method to instantiate a class using the given constructor.
static boolean isAssignable(Class targetType, Class valueType)
          Determine if the given target type is assignable from the given value type, assuming setting by reflection.
static boolean isAssignable(Class type, Object value)
          Determine if the given type is assignable from the given value, assuming setting by reflection.
static boolean isPrimitiveArray(Class clazz)
          Check if the given class represents an array of primitives, i.e. boolean, byte, char, short, int, long, float, or double.
static boolean isPrimitiveWrapper(Class clazz)
          Check if the given class represents a primitive wrapper, i.e.
static boolean isPrimitiveWrapperArray(Class clazz)
          Check if the given class represents an array of primitive wrappers, i.e.
static boolean isSimpleProperty(Class clazz)
          Check if the given class represents a "simple" property, i.e. a primitive, a String, a Class, or a corresponding array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanUtils

public BeanUtils()
Method Detail

isAssignable

public static boolean isAssignable(Class type,
                                   Object value)
Determine if the given type is assignable from the given value, assuming setting by reflection. Considers primitive wrapper classes as assignable to the corresponding primitive types.

For example used in a bean factory's constructor resolution.

Parameters:
type - the target type
value - the value that should be assigned to the type
Returns:
if the type is assignable from the value

isAssignable

public static boolean isAssignable(Class targetType,
                                   Class valueType)
Determine if the given target type is assignable from the given value type, assuming setting by reflection. Considers primitive wrapper classes as assignable to the corresponding primitive types.

For example used in BeanWrapperImpl's custom editor matching.

Parameters:
targetType - the target type
valueType - the value type that should be assigned to the target type
Returns:
if the target type is assignable from the value type

isSimpleProperty

public static boolean isSimpleProperty(Class clazz)
Check if the given class represents a "simple" property, i.e. a primitive, a String, a Class, or a corresponding array. Used to determine properties to check for a "simple" dependency-check.

See Also:
AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE, AbstractAutowireCapableBeanFactory.checkDependencies(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.beans.BeanWrapper, org.springframework.beans.PropertyValues)

isPrimitiveArray

public static boolean isPrimitiveArray(Class clazz)
Check if the given class represents an array of primitives, i.e. boolean, byte, char, short, int, long, float, or double.


isPrimitiveWrapper

public static boolean isPrimitiveWrapper(Class clazz)
Check if the given class represents a primitive wrapper, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.


isPrimitiveWrapperArray

public static boolean isPrimitiveWrapperArray(Class clazz)
Check if the given class represents an array of primitive wrappers, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.


instantiateClass

public static Object instantiateClass(Class clazz)
                               throws BeanInstantiationException
Convenience method to instantiate a class using its no-arg constructor. As this method doesn't try to load classes by name, it should avoid class-loading issues.

Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.

Parameters:
clazz - class to instantiate
Returns:
the new instance
Throws:
BeanInstantiationException - if the bean cannot be instantiated

instantiateClass

public static Object instantiateClass(Constructor ctor,
                                      Object[] args)
                               throws BeanInstantiationException
Convenience method to instantiate a class using the given constructor. As this method doesn't try to load classes by name, it should avoid class-loading issues.

Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.

Parameters:
ctor - constructor to instantiate
Returns:
the new instance
Throws:
BeanInstantiationException - if the bean cannot be instantiated

findMethod

public static Method findMethod(Class clazz,
                                String methodName,
                                Class[] paramTypes)
Find a method with the given method name and the given parameter types, declared on the given class or one of its superclasses. Prefers public methods, but will return a protected, package access, or private method too.

Checks Class.getMethod first, falling back to findDeclaredMethod. This allows to find public methods without issues even in environments with restricted Java security settings.

Parameters:
clazz - the class to check
methodName - the name of the method to find
paramTypes - the parameter types of the method to find
Returns:
the Method object, or null if not found
See Also:
Class.getMethod(java.lang.String, java.lang.Class...), findDeclaredMethod(java.lang.Class, java.lang.String, java.lang.Class[])

findDeclaredMethod

public static Method findDeclaredMethod(Class clazz,
                                        String methodName,
                                        Class[] paramTypes)
Find a method with the given method name and the given parameter types, declared on the given class or one of its superclasses. Will return a public, protected, package access, or private method.

Checks Class.getDeclaredMethod, cascading upwards to all superclasses.

Parameters:
clazz - the class to check
methodName - the name of the method to find
paramTypes - the parameter types of the method to find
Returns:
the Method object, or null if not found
See Also:
Class.getDeclaredMethod(java.lang.String, java.lang.Class...)

findMethodWithMinimalParameters

public static Method findMethodWithMinimalParameters(Class clazz,
                                                     String methodName)
Find a method with the given method name and minimal parameters (best case: none), declared on the given class or one of its superclasses. Prefers public methods, but will return a protected, package access, or private method too.

Checks Class.getMethods first, falling back to findDeclaredMethodWithMinimalParameters. This allows to find public methods without issues even in environments with restricted Java security settings.

Parameters:
clazz - the class to check
methodName - the name of the method to find
Returns:
the Method object, or null if not found
See Also:
Class.getMethods(), findDeclaredMethodWithMinimalParameters(java.lang.Class, java.lang.String)

findDeclaredMethodWithMinimalParameters

public static Method findDeclaredMethodWithMinimalParameters(Class clazz,
                                                             String methodName)
Find a method with the given method name and minimal parameters (best case: none), declared on the given class or one of its superclasses. Will return a public, protected, package access, or private method.

Checks Class.getDeclaredMethods, cascading upwards to all superclasses.

Parameters:
clazz - the class to check
methodName - the name of the method to find
Returns:
the method object, or null if not found
See Also:
Class.getDeclaredMethods()

getPropertyDescriptors

public static PropertyDescriptor[] getPropertyDescriptors(Class clazz)
                                                   throws BeansException
Retrieve the JavaBeans PropertyDescriptors of a given class.

Parameters:
clazz - the Class to retrieve the PropertyDescriptors for
Returns:
an array of PropertyDescriptors for the given class
Throws:
BeansException - if PropertyDescriptor look fails

getPropertyDescriptor

public static PropertyDescriptor getPropertyDescriptor(Class clazz,
                                                       String propertyName)
                                                throws BeansException
Retrieve the JavaBeans PropertyDescriptors for the given property.

Parameters:
clazz - the Class to retrieve the PropertyDescriptor for
propertyName - the name of the property
Returns:
the corresponding PropertyDescriptor, or null if none
Throws:
BeansException - if PropertyDescriptor lookup fails

findPropertyForMethod

public static PropertyDescriptor findPropertyForMethod(Method method)
                                                throws BeansException
Find a JavaBeans PropertyDescriptor for the given method, with the method either being the read method or the write method for that bean property.

Parameters:
method - the method to find a corresponding PropertyDescriptor for
Returns:
the corresponding PropertyDescriptor, or null if none
Throws:
BeansException - if PropertyDescriptor lookup fails

canonicalName

public static String canonicalName(String propertyName)
Deprecated. as of Spring 1.2.9, in favor of PropertyAccessorUtils.canonicalPropertyName(String)

Determine the canonical name for the given property path. Removes surrounding quotes from map keys:
map['key'] -> map[key]
map["key"] -> map[key]

Parameters:
propertyName - the bean property path
Returns:
the canonical representation of the property path

copyProperties

public static void copyProperties(Object source,
                                  Object target)
                           throws BeansException
Copy the property values of the given source bean into the target bean.

Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean properties that the source bean exposes but the target bean does not will silently be ignored.

This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.

Parameters:
source - the source bean
target - the target bean
Throws:
BeansException - if the copying failed
See Also:
BeanWrapper

copyProperties

public static void copyProperties(Object source,
                                  Object target,
                                  Class editable)
                           throws BeansException
Copy the property values of the given source bean into the given target bean, only setting properties defined in the given "editable" class (or interface).

Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean properties that the source bean exposes but the target bean does not will silently be ignored.

This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.

Parameters:
source - the source bean
target - the target bean
editable - the class (or interface) to restrict property setting to
Throws:
BeansException - if the copying failed
See Also:
BeanWrapper

copyProperties

public static void copyProperties(Object source,
                                  Object target,
                                  String[] ignoreProperties)
                           throws BeansException
Copy the property values of the given source bean into the given target bean, ignoring the given "ignoreProperties".

Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean properties that the source bean exposes but the target bean does not will silently be ignored.

This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.

Parameters:
source - the source bean
target - the target bean
ignoreProperties - array of property names to ignore
Throws:
BeansException - if the copying failed
See Also:
BeanWrapper


Copyright (c) 2002-2007 The Spring Framework Project.