|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.BeanUtils
public abstract class BeanUtils
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.
Constructor Summary | |
---|---|
BeanUtils()
|
Method Summary | |
---|---|
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 Class |
findPropertyType(String propertyName,
Class[] beanClasses)
Determine the bean property type for the given property from the given classes/interfaces, if possible. |
static PropertyDescriptor |
getPropertyDescriptor(Class clazz,
String propertyName)
Retrieve the JavaBeans PropertyDescriptors for the given property. |
static PropertyDescriptor[] |
getPropertyDescriptors(Class clazz)
Retrieve the JavaBeans PropertyDescriptor s 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)
Deprecated. as of Spring 2.0, in favor of ClassUtils.isAssignable |
static boolean |
isAssignable(Class type,
Object value)
Deprecated. as of Spring 2.0, in favor of ClassUtils.isAssignableValue |
static boolean |
isSimpleProperty(Class clazz)
Check if the given type represents a "simple" property: a primitive, a String, a Class, or a corresponding array. |
static Method |
resolveSignature(String signature,
Class clazz)
Parse a method signature in the form methodName[([arg_list])] ,
where arg_list is an optional, comma-separated list of fully-qualified
type names, and attempts to resolve that signature against the supplied Class . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BeanUtils()
Method Detail |
---|
public static Object instantiateClass(Class clazz) throws BeanInstantiationException
Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
clazz
- class to instantiate
BeanInstantiationException
- if the bean cannot be instantiatedpublic static Object instantiateClass(Constructor ctor, Object[] args) throws BeanInstantiationException
Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
ctor
- the constructor to instantiateargs
- the constructor arguments to apply
BeanInstantiationException
- if the bean cannot be instantiatedpublic static Method findMethod(Class clazz, String methodName, Class[] paramTypes)
Checks Class.getMethod
first, falling back to
findDeclaredMethod
. This allows to find public methods
without issues even in environments with restricted Java security settings.
clazz
- the class to checkmethodName
- the name of the method to findparamTypes
- the parameter types of the method to find
null
if not foundClass.getMethod(java.lang.String, java.lang.Class>...)
,
findDeclaredMethod(java.lang.Class, java.lang.String, java.lang.Class[])
public static Method findDeclaredMethod(Class clazz, String methodName, Class[] paramTypes)
Checks Class.getDeclaredMethod
, cascading upwards to all superclasses.
clazz
- the class to checkmethodName
- the name of the method to findparamTypes
- the parameter types of the method to find
null
if not foundClass.getDeclaredMethod(java.lang.String, java.lang.Class>...)
public static Method findMethodWithMinimalParameters(Class clazz, String methodName) throws IllegalArgumentException
Checks Class.getMethods
first, falling back to
findDeclaredMethodWithMinimalParameters
. This allows to find public
methods without issues even in environments with restricted Java security settings.
clazz
- the class to checkmethodName
- the name of the method to find
null
if not found
IllegalArgumentException
- if methods of the given name were found but
could not be resolved to a unique method with minimal parametersClass.getMethods()
,
findDeclaredMethodWithMinimalParameters(java.lang.Class, java.lang.String)
public static Method findDeclaredMethodWithMinimalParameters(Class clazz, String methodName) throws IllegalArgumentException
Checks Class.getDeclaredMethods
, cascading upwards to all superclasses.
clazz
- the class to checkmethodName
- the name of the method to find
null
if not found
IllegalArgumentException
- if methods of the given name were found but
could not be resolved to a unique method with minimal parametersClass.getDeclaredMethods()
public static Method resolveSignature(String signature, Class clazz)
methodName[([arg_list])]
,
where arg_list
is an optional, comma-separated list of fully-qualified
type names, and attempts to resolve that signature against the supplied Class
.
When not supplying an argument list (methodName
) the method whose name
matches and has the least number of parameters will be returned. When supplying an
argument type list, only the method whose name and argument types match will be returned.
Note then that methodName
and methodName()
are not
resolved in the same way. The signature methodName
means the method called
methodName
with the least number of arguments, whereas methodName()
means the method called methodName
with exactly 0 arguments.
If no method can be found, then null
is returned.
signature
- the method signature as String representationclazz
- the class to resolve the method signature against
findMethod(java.lang.Class, java.lang.String, java.lang.Class[])
,
findMethodWithMinimalParameters(java.lang.Class, java.lang.String)
public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException
PropertyDescriptor
s of a given class.
clazz
- the Class to retrieve the PropertyDescriptors for
PropertyDescriptors
for the given class
BeansException
- if PropertyDescriptor look failspublic static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) throws BeansException
PropertyDescriptors
for the given property.
clazz
- the Class to retrieve the PropertyDescriptor forpropertyName
- the name of the property
null
if none
BeansException
- if PropertyDescriptor lookup failspublic static PropertyDescriptor findPropertyForMethod(Method method) throws BeansException
PropertyDescriptor
for the given method,
with the method either being the read method or the write method for
that bean property.
method
- the method to find a corresponding PropertyDescriptor for
null
if none
BeansException
- if PropertyDescriptor lookup failspublic static Class findPropertyType(String propertyName, Class[] beanClasses)
propertyName
- the name of the bean propertybeanClasses
- the classes to check against
Object.class
as fallbackpublic static boolean isSimpleProperty(Class clazz)
Used to determine properties to check for a "simple" dependency-check.
clazz
- the type to check
AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE
,
AbstractAutowireCapableBeanFactory.checkDependencies(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.beans.PropertyDescriptor[], org.springframework.beans.PropertyValues)
public static boolean isAssignable(Class targetType, Class valueType)
ClassUtils.isAssignable
targetType
- the target typevalueType
- the value type that should be assigned to the target type
ClassUtils.isAssignable(Class, Class)
public static boolean isAssignable(Class type, Object value)
ClassUtils.isAssignableValue
type
- the target typevalue
- the value that should be assigned to the type
ClassUtils.isAssignableValue(Class, Object)
public static void copyProperties(Object source, Object target) throws BeansException
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.
source
- the source beantarget
- the target bean
BeansException
- if the copying failedBeanWrapper
public static void copyProperties(Object source, Object target, Class editable) throws BeansException
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.
source
- the source beantarget
- the target beaneditable
- the class (or interface) to restrict property setting to
BeansException
- if the copying failedBeanWrapper
public static void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException
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.
source
- the source beantarget
- the target beanignoreProperties
- array of property names to ignore
BeansException
- if the copying failedBeanWrapper
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |