Class BeanUtils
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.
- Author:
- Rod Johnson, Juergen Hoeller, Rob Harrop, Sam Brannen, Sebastien Deleuze
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 JavaBeansPropertyDescriptor
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 JavaBeansPropertyDescriptor
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 String[]
getParameterNames
(Constructor<?> ctor) Determine required parameter names for the given constructor, considering the JavaBeansConstructorProperties
annotation as well as Spring'sDefaultParameterNameDiscoverer
.static PropertyDescriptor
getPropertyDescriptor
(Class<?> clazz, String propertyName) Retrieve the JavaBeansPropertyDescriptors
for the given property.static PropertyDescriptor[]
getPropertyDescriptors
(Class<?> clazz) Retrieve the JavaBeansPropertyDescriptor
s of a given class.static <T> Constructor<T>
getResolvableConstructor
(Class<T> clazz) Return a resolvable constructor for the provided class, either a primary or single public constructor with arguments, or a single non-public constructor with arguments, or simply a default constructor.static MethodParameter
Obtain a new MethodParameter object for the write method of the specified property.static <T> T
instantiate
(Class<T> clazz) Deprecated.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 Temporal, a URI, a URL, a Locale, or a Class.static Method
resolveSignature
(String signature, Class<?> clazz) Parse a method signature in the formmethodName[([arg_list])]
, wherearg_list
is an optional, comma-separated list of fully-qualified type names, and attempts to resolve that signature against the suppliedClass
.
-
Constructor Details
-
BeanUtils
public BeanUtils()
-
-
Method Details
-
instantiate
Deprecated.as of Spring 5.0, following the deprecation ofClass.newInstance()
in JDK 9Convenience method to instantiate a class using its no-arg constructor.- Parameters:
clazz
- class to instantiate- Returns:
- the new instance
- Throws:
BeanInstantiationException
- if the bean cannot be instantiated- See Also:
-
instantiateClass
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).Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
- Parameters:
clazz
- the class to instantiate- Returns:
- the new instance
- Throws:
BeanInstantiationException
- if the bean cannot be instantiated. The cause may notably indicate aNoSuchMethodException
if no primary/default constructor was found, aNoClassDefFoundError
or otherLinkageError
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.- See Also:
-
instantiateClass
public static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo) throws BeanInstantiationException Instantiate a class using its no-arg constructor and return the new instance as the specified assignable type.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.
- Parameters:
clazz
- class to instantiateassignableTo
- type that clazz must be assignableTo- Returns:
- the new instance
- Throws:
BeanInstantiationException
- if the bean cannot be instantiated- See Also:
-
instantiateClass
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException Convenience method to instantiate a class using the given constructor.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.
- Parameters:
ctor
- the constructor to instantiateargs
- the constructor arguments to apply (usenull
for an unspecified parameter, Kotlin optional parameters and Java primitive types are supported)- Returns:
- the new instance
- Throws:
BeanInstantiationException
- if the bean cannot be instantiated- See Also:
-
getResolvableConstructor
Return a resolvable constructor for the provided class, either a primary or single public constructor with arguments, or a single non-public constructor with arguments, or simply a default constructor. Callers have to be prepared to resolve arguments for the returned constructor's parameters, if any.- Parameters:
clazz
- the class to check- Throws:
IllegalStateException
- in case of no unique constructor found at all- Since:
- 5.3
- See Also:
-
findPrimaryConstructor
Return the primary constructor of the provided class. For Kotlin classes, this returns the Java constructor corresponding to the Kotlin primary constructor (as defined in the Kotlin specification). Otherwise, in particular for non-Kotlin classes, this simply returnsnull
.- Parameters:
clazz
- the class to check- Since:
- 5.0
- See Also:
-
findMethod
@Nullable 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 tofindDeclaredMethod
. This allows to find public methods without issues even in environments with restricted Java security settings.- Parameters:
clazz
- the class to checkmethodName
- the name of the method to findparamTypes
- the parameter types of the method to find- Returns:
- the Method object, or
null
if not found - See Also:
-
findDeclaredMethod
@Nullable 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 checkmethodName
- the name of the method to findparamTypes
- the parameter types of the method to find- Returns:
- the Method object, or
null
if not found - See Also:
-
findMethodWithMinimalParameters
@Nullable public static Method findMethodWithMinimalParameters(Class<?> clazz, String methodName) throws IllegalArgumentException 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 tofindDeclaredMethodWithMinimalParameters
. This allows for finding public methods without issues even in environments with restricted Java security settings.- Parameters:
clazz
- the class to checkmethodName
- the name of the method to find- Returns:
- the Method object, or
null
if not found - Throws:
IllegalArgumentException
- if methods of the given name were found but could not be resolved to a unique method with minimal parameters- See Also:
-
findDeclaredMethodWithMinimalParameters
@Nullable public static Method findDeclaredMethodWithMinimalParameters(Class<?> clazz, String methodName) throws IllegalArgumentException 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 checkmethodName
- the name of the method to find- Returns:
- the Method object, or
null
if not found - Throws:
IllegalArgumentException
- if methods of the given name were found but could not be resolved to a unique method with minimal parameters- See Also:
-
findMethodWithMinimalParameters
@Nullable public static Method findMethodWithMinimalParameters(Method[] methods, String methodName) throws IllegalArgumentException Find a method with the given method name and minimal parameters (best case: none) in the given list of methods.- Parameters:
methods
- the methods to checkmethodName
- the name of the method to find- Returns:
- the Method object, or
null
if not found - Throws:
IllegalArgumentException
- if methods of the given name were found but could not be resolved to a unique method with minimal parameters
-
resolveSignature
Parse a method signature in the formmethodName[([arg_list])]
, wherearg_list
is an optional, comma-separated list of fully-qualified type names, and attempts to resolve that signature against the suppliedClass
.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
andmethodName()
are not resolved in the same way. The signaturemethodName
means the method calledmethodName
with the least number of arguments, whereasmethodName()
means the method calledmethodName
with exactly 0 arguments.If no method can be found, then
null
is returned.- Parameters:
signature
- the method signature as String representationclazz
- the class to resolve the method signature against- Returns:
- the resolved Method
- See Also:
-
getPropertyDescriptors
Retrieve the JavaBeansPropertyDescriptor
s 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
@Nullable public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException Retrieve the JavaBeansPropertyDescriptors
for the given property.- Parameters:
clazz
- the Class to retrieve the PropertyDescriptor forpropertyName
- the name of the property- Returns:
- the corresponding PropertyDescriptor, or
null
if none - Throws:
BeansException
- if PropertyDescriptor lookup fails
-
findPropertyForMethod
@Nullable public static PropertyDescriptor findPropertyForMethod(Method method) throws BeansException Find a JavaBeansPropertyDescriptor
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, introspecting its declaring class- Returns:
- the corresponding PropertyDescriptor, or
null
if none - Throws:
BeansException
- if PropertyDescriptor lookup fails
-
findPropertyForMethod
@Nullable public static PropertyDescriptor findPropertyForMethod(Method method, Class<?> clazz) throws BeansException Find a JavaBeansPropertyDescriptor
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 forclazz
- the (most specific) class to introspect for descriptors- Returns:
- the corresponding PropertyDescriptor, or
null
if none - Throws:
BeansException
- if PropertyDescriptor lookup fails- Since:
- 3.2.13
-
findEditorByConvention
Find a JavaBeans PropertyEditor following the 'Editor' suffix convention (e.g. "mypackage.MyDomainClass" → "mypackage.MyDomainClassEditor").Compatible to the standard JavaBeans convention as implemented by
PropertyEditorManager
but isolated from the latter's registered default editors for primitive types.- Parameters:
targetType
- the type to find an editor for- Returns:
- the corresponding editor, or
null
if none found
-
findPropertyType
Determine the bean property type for the given property from the given classes/interfaces, if possible.- Parameters:
propertyName
- the name of the bean propertybeanClasses
- the classes to check against- Returns:
- the property type, or
Object.class
as fallback
-
getWriteMethodParameter
Obtain a new MethodParameter object for the write method of the specified property.- Parameters:
pd
- the PropertyDescriptor for the property- Returns:
- a corresponding MethodParameter object
-
getParameterNames
Determine required parameter names for the given constructor, considering the JavaBeansConstructorProperties
annotation as well as Spring'sDefaultParameterNameDiscoverer
.- Parameters:
ctor
- the constructor to find parameter names for- Returns:
- the parameter names (matching the constructor's parameter count)
- Throws:
IllegalStateException
- if the parameter names are not resolvable- Since:
- 5.3
- See Also:
-
isSimpleProperty
Check if the given type represents a "simple" property: a simple value type or an array of simple value types.See
isSimpleValueType(Class)
for the definition of simple value type.Used to determine properties to check for a "simple" dependency-check.
- Parameters:
type
- the type to check- Returns:
- whether the given type represents a "simple" property
- See Also:
-
isSimpleValueType
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 Temporal, a URI, a URL, a Locale, or a Class.Void
andvoid
are not considered simple value types.- Parameters:
type
- the type to check- Returns:
- whether the given type represents a "simple" value type
- See Also:
-
copyProperties
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
.As of Spring Framework 5.3, this method honors generic type information when matching properties in the source and target objects.
The following table provides a non-exhaustive set of examples of source and target property types that can be copied as well as source and target property types that cannot be copied.
source property type target property type copy supported Integer
Integer
yes Integer
Number
yes List<Integer>
List<Integer>
yes List<?>
List<?>
yes List<Integer>
List<?>
yes List<Integer>
List<? extends Number>
yes String
Integer
no Number
Integer
no List<Integer>
List<Long>
no List<Integer>
List<Number>
no - Parameters:
source
- the source beantarget
- the target bean- Throws:
BeansException
- if the copying failed- See Also:
-
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
.As of Spring Framework 5.3, this method honors generic type information when matching properties in the source and target objects. See the documentation for
copyProperties(Object, Object)
for details.- Parameters:
source
- the source beantarget
- the target beaneditable
- the class (or interface) to restrict property setting to- Throws:
BeansException
- if the copying failed- See Also:
-
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
.As of Spring Framework 5.3, this method honors generic type information when matching properties in the source and target objects. See the documentation for
copyProperties(Object, Object)
for details.- Parameters:
source
- the source beantarget
- the target beanignoreProperties
- array of property names to ignore- Throws:
BeansException
- if the copying failed- See Also:
-
Class.newInstance()
in JDK 9