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.
static void
copyProperties
(
Object
source,
Object
target,
String
... ignoreProperties)
static
Method
findDeclaredMethod
(
Class
<?> clazz,
String
methodName,
Class
<?>... paramTypes)
static
Method
findDeclaredMethodWithMinimalParameters
(
Class
<?> clazz,
String
methodName)
static
PropertyEditor
findEditorByConvention
(
Class
<?> targetType)
static
Method
findMethod
(
Class
<?> clazz,
String
methodName,
Class
<?>... paramTypes)
static
Method
findMethodWithMinimalParameters
(
Class
<?> clazz,
String
methodName)
static
Method
findMethodWithMinimalParameters
(
Method
[] methods,
String
methodName)
static <T>
Constructor
<T>
findPrimaryConstructor
(
Class
<T> clazz)
static
PropertyDescriptor
findPropertyForMethod
(
Method
method)
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)
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)
static
String
[]
getParameterNames
(
Constructor
<?> ctor)
ConstructorProperties
annotation
as well as Spring's
DefaultParameterNameDiscoverer
.
static
PropertyDescriptor
getPropertyDescriptor
(
Class
<?> clazz,
String
propertyName)
PropertyDescriptors
for the given property.
static
PropertyDescriptor
[]
getPropertyDescriptors
(
Class
<?> clazz)
PropertyDescriptor
s of a given class.
static <T>
Constructor
<T>
getResolvableConstructor
(
Class
<T> clazz)
static
MethodParameter
static boolean
static <T> T
instantiate
(
Class
<T> clazz)
static <T> T
instantiateClass
(
Class
<?> clazz,
Class
<T> assignableTo)
static <T> T
instantiateClass
(
Class
<T> clazz)
static <T> T
instantiateClass
(
Constructor
<T> ctor,
Object
... args)
static boolean
isSimpleProperty
(
Class
<?> type)
static boolean
isSimpleValueType
(
Class
<?> type)
Enum
, a
String
or other
CharSequence
, a
Number
, a
Date
,
a
Temporal
, a
UUID
, a
URI
, a
URL
,
a
Locale
, or a
Class
.
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
.
Class.newInstance()
in JDK 9
clazz
- class to instantiate
BeanInstantiationException
- if the bean cannot be instantiated
Class.newInstance()
Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
clazz
- the class to instantiate
BeanInstantiationException
- 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...)
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 instantiate
assignableTo
- type that clazz must be assignableTo
BeanInstantiationException
- if the bean cannot be instantiated
Constructor.newInstance(java.lang.Object...)
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 instantiate
args
- the constructor arguments to apply (use
null
for an unspecified
parameter, Kotlin optional parameters and Java primitive types are supported)
BeanInstantiationException
- if the bean cannot be instantiated
Constructor.newInstance(java.lang.Object...)
clazz
- the class to check
IllegalStateException
- in case of no unique constructor found at all
findPrimaryConstructor(java.lang.Class<T>)
null
.
clazz
- the class to check
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 check
methodName
- the name of the method to find
paramTypes
- the parameter types of the method to find
null
if not found
Class.getMethod(java.lang.String, java.lang.Class<?>...)
findDeclaredMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
Checks
Class.getDeclaredMethod
, cascading upwards to all superclasses.
clazz
- the class to check
methodName
- the name of the method to find
paramTypes
- the parameter types of the method to find
null
if not found
Class.getDeclaredMethod(java.lang.String, java.lang.Class<?>...)
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 check
methodName
- 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 parameters
Class.getMethods()
findDeclaredMethodWithMinimalParameters(java.lang.Class<?>, java.lang.String)
Checks
Class.getDeclaredMethods
, cascading upwards to all superclasses.
clazz
- the class to check
methodName
- 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 parameters
Class.getDeclaredMethods()
methods
- the methods to check
methodName
- 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 parameters
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 representation
clazz
- 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)
PropertyDescriptor
s of a given class.
clazz
- the Class to retrieve the PropertyDescriptors for
PropertyDescriptors
for the given class
BeansException
- if PropertyDescriptor look fails
PropertyDescriptors
for the given property.
clazz
- the Class to retrieve the PropertyDescriptor for
propertyName
- the name of the property
null
if none
BeansException
- if PropertyDescriptor lookup fails
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 class
null
if none
BeansException
- if PropertyDescriptor lookup fails
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
clazz
- the (most specific) class to introspect for descriptors
null
if none
BeansException
- if PropertyDescriptor lookup fails
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 for
null
if none found
propertyName
- the name of the bean property
beanClasses
- the classes to check against
Object.class
as fallback
pd
- the PropertyDescriptor for the property
true
if writable and unique,
false
otherwise
pd
- the PropertyDescriptor for the property
ConstructorProperties
annotation
as well as Spring's
DefaultParameterNameDiscoverer
.
ctor
- the constructor to find parameter names for
IllegalStateException
- if the parameter names are not resolvable
ConstructorProperties
DefaultParameterNameDiscoverer
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 check
AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE
AbstractAutowireCapableBeanFactory.checkDependencies(java.lang.String, org.springframework.beans.factory.support.AbstractBeanDefinition, java.beans.PropertyDescriptor[], org.springframework.beans.PropertyValues)
isSimpleValueType(Class)
Enum
, a
String
or other
CharSequence
, a
Number
, a
Date
,
a
Temporal
, a
UUID
, a
URI
, a
URL
,
a
Locale
, or a
Class
.
Void
and
void
are not considered simple value types.
As of 6.1, this method delegates to
ClassUtils.isSimpleValueType(java.lang.Class<?>)
as-is but could potentially add further rules for bean property purposes.
type
- the type to check
isSimpleProperty(Class)
ClassUtils.isSimpleValueType(Class)
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 typetarget property typecopy 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
source
- the source bean
target
- the target bean
BeansException
- if the copying failed
BeanWrapper
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.
source
- the source bean
target
- the target bean
editable
- the class (or interface) to restrict property setting to
BeansException
- if the copying failed
BeanWrapper
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.
source
- the source bean
target
- the target bean
ignoreProperties
- array of property names to ignore
BeansException
- if the copying failed
BeanWrapper
Class.newInstance()
in JDK 9