public abstract class BeanUtils extends Object
Mainly for internal use within the framework, but to some degree also useful for application classes. Consider Apache Commons BeanUtils, BULL - Bean Utils Light Library, or similar third-party frameworks for more comprehensive bean utilities.
Constructor and Description |
---|
BeanUtils() |
Modifier and Type | Method and Description |
---|---|
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 PropertyEditor |
findEditorByConvention(Class<?> targetType)
Find a JavaBeans PropertyEditor following the 'Editor' suffix convention
(e.g.
|
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 Method |
findMethodWithMinimalParameters(Method[] methods,
String methodName)
Find a method with the given method name and minimal parameters (best case: none)
in the given list of methods.
|
static <T> Constructor<T> |
findPrimaryConstructor(Class<T> clazz)
Return the primary constructor of the provided class.
|
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 |
findPropertyForMethod(Method method,
Class<?> clazz)
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 MethodParameter |
getWriteMethodParameter(PropertyDescriptor pd)
Obtain a new MethodParameter object for the write method of the
specified property.
|
static <T> T |
instantiate(Class<T> clazz)
Deprecated.
as of Spring 5.0, following the deprecation of
Class.newInstance() in JDK 9 |
static <T> T |
instantiateClass(Class<?> clazz,
Class<T> assignableTo)
Instantiate a class using its no-arg constructor and return the new instance
as the specified assignable type.
|
static <T> T |
instantiateClass(Class<T> clazz)
Instantiate a class using its 'primary' constructor (for Kotlin classes,
potentially having default arguments declared) or its default constructor
(for regular Java classes, expecting a standard no-arg setup).
|
static <T> T |
instantiateClass(Constructor<T> ctor,
Object... args)
Convenience method to instantiate a class using the given constructor.
|
static boolean |
isSimpleProperty(Class<?> type)
Check if the given type represents a "simple" property: a simple value
type or an array of simple value types.
|
static boolean |
isSimpleValueType(Class<?> type)
Check if the given type represents a "simple" value type: a primitive or
primitive wrapper, an enum, a String or other CharSequence, a Number, a
Date, a URI, a URL, a Locale, or a Class.
|
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 . |
@Deprecated public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException
Class.newInstance()
in JDK 9clazz
- class to instantiateBeanInstantiationException
- if the bean cannot be instantiatedClass.newInstance()
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException
Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
clazz
- the class to instantiateBeanInstantiationException
- if the bean cannot be instantiated.
The cause may notably indicate a NoSuchMethodException
if no
primary/default constructor was found, a NoClassDefFoundError
or other LinkageError
in case of an unresolvable class definition
(e.g. due to a missing dependency at runtime), or an exception thrown
from the constructor invocation itself.Constructor.newInstance(java.lang.Object...)
public static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo) throws BeanInstantiationException
Useful in cases where the type of the class to instantiate (clazz) is not available, but the type desired (assignableTo) is known.
Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
clazz
- class to instantiateassignableTo
- type that clazz must be assignableToBeanInstantiationException
- if the bean cannot be instantiatedConstructor.newInstance(java.lang.Object...)
public static <T> T instantiateClass(Constructor<T> 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, and supports Kotlin classes with optional parameters and default values.
ctor
- the constructor to instantiateargs
- the constructor arguments to apply (use null
for an unspecified
parameter if needed for Kotlin classes with optional parameters and default values)BeanInstantiationException
- if the bean cannot be instantiatedConstructor.newInstance(java.lang.Object...)
@Nullable public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz)
null
.clazz
- the class to check@Nullable public 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 findnull
if not foundClass.getMethod(java.lang.String, java.lang.Class<?>...)
,
findDeclaredMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
@Nullable 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 findnull
if not foundClass.getDeclaredMethod(java.lang.String, java.lang.Class<?>...)
@Nullable public static Method findMethodWithMinimalParameters(Class<?> clazz, String methodName) throws IllegalArgumentException
Checks Class.getMethods
first, falling back to
findDeclaredMethodWithMinimalParameters
. This allows for finding public
methods without issues even in environments with restricted Java security settings.
clazz
- the class to checkmethodName
- the name of the method to findnull
if not foundIllegalArgumentException
- 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)
@Nullable 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 findnull
if not foundIllegalArgumentException
- if methods of the given name were found but
could not be resolved to a unique method with minimal parametersClass.getDeclaredMethods()
@Nullable public static Method findMethodWithMinimalParameters(Method[] methods, String methodName) throws IllegalArgumentException
methods
- the methods to checkmethodName
- the name of the method to findnull
if not foundIllegalArgumentException
- if methods of the given name were found but
could not be resolved to a unique method with minimal parameters@Nullable 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 againstfindMethod(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 forPropertyDescriptors
for the given classBeansException
- if PropertyDescriptor look fails@Nullable public 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 propertynull
if noneBeansException
- if PropertyDescriptor lookup fails@Nullable public 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,
introspecting its declaring classnull
if noneBeansException
- if PropertyDescriptor lookup fails@Nullable public static PropertyDescriptor findPropertyForMethod(Method method, Class<?> clazz) 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 forclazz
- the (most specific) class to introspect for descriptorsnull
if noneBeansException
- if PropertyDescriptor lookup fails@Nullable public static PropertyEditor findEditorByConvention(@Nullable Class<?> targetType)
Compatible to the standard JavaBeans convention as implemented by
PropertyEditorManager
but isolated from the latter's
registered default editors for primitive types.
targetType
- the type to find an editor fornull
if none foundpublic static Class<?> findPropertyType(String propertyName, @Nullable Class<?>... beanClasses)
propertyName
- the name of the bean propertybeanClasses
- the classes to check againstObject.class
as fallbackpublic static MethodParameter getWriteMethodParameter(PropertyDescriptor pd)
pd
- the PropertyDescriptor for the propertypublic static boolean isSimpleProperty(Class<?> type)
See isSimpleValueType(Class)
for the definition of simple
value type.
Used to determine properties to check for a "simple" dependency-check.
type
- the type to checkAbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE
,
AbstractAutowireCapableBeanFactory.checkDependencies(java.lang.String, org.springframework.beans.factory.support.AbstractBeanDefinition, java.beans.PropertyDescriptor[], org.springframework.beans.PropertyValues)
,
isSimpleValueType(Class)
public static boolean isSimpleValueType(Class<?> type)
Void
and void
are not considered simple value types.
type
- the type to checkisSimpleProperty(Class)
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 beanBeansException
- 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 toBeansException
- 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 ignoreBeansException
- if the copying failedBeanWrapper