类 BeanUtils
- java.lang.Object
- 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 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.
- 作者:
- Rod Johnson, Juergen Hoeller, Rob Harrop, Sam Brannen
构造器概要
构造器 构造器 说明 BeanUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 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 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 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 MethodParameter
getWriteMethodParameter(PropertyDescriptor pd)
Obtain a new MethodParameter object for the write method of the specified property.static <T> T
instantiate(Class<T> clazz)
Convenience method to instantiate a class using its no-arg constructor.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 no-arg constructor.static <T> T
instantiateClass(Constructor<T> ctor, Object... args)
Convenience method to instantiate a class using the given constructor.static boolean
isSimpleProperty(Class<?> clazz)
Check if the given type represents a "simple" property: a primitive, a String or other CharSequence, a Number, a Date, a URI, a URL, a Locale, a Class, or a corresponding array.static boolean
isSimpleValueType(Class<?> clazz)
Check if the given type represents a "simple" value type: a primitive, 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 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
.
构造器详细资料
BeanUtils
public BeanUtils()
方法详细资料
instantiate
public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException
Convenience method to instantiate a class using its no-arg constructor.- 参数:
clazz
- class to instantiate- 返回:
- the new instance
- 抛出:
BeanInstantiationException
- if the bean cannot be instantiated- 另请参阅:
Class.newInstance()
instantiateClass
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException
Instantiate a class using its no-arg constructor.Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.
- 参数:
clazz
- class to instantiate- 返回:
- the new instance
- 抛出:
BeanInstantiationException
- if the bean cannot be instantiated- 另请参阅:
Constructor.newInstance(java.lang.Object...)
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.
- 参数:
clazz
- class to instantiateassignableTo
- type that clazz must be assignableTo- 返回:
- the new instance
- 抛出:
BeanInstantiationException
- if the bean cannot be instantiated- 另请参阅:
Constructor.newInstance(java.lang.Object...)
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.
- 参数:
ctor
- the constructor to instantiateargs
- the constructor arguments to apply- 返回:
- the new instance
- 抛出:
BeanInstantiationException
- if the bean cannot be instantiated- 另请参阅:
Constructor.newInstance(java.lang.Object...)
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 tofindDeclaredMethod
. 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- 返回:
- the Method object, or
null
if not found - 另请参阅:
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.- 参数:
clazz
- the class to checkmethodName
- the name of the method to findparamTypes
- the parameter types of the method to find- 返回:
- the Method object, or
null
if not found - 另请参阅:
Class.getDeclaredMethod(java.lang.String, java.lang.Class<?>...)
findMethodWithMinimalParameters
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.- 参数:
clazz
- the class to checkmethodName
- the name of the method to find- 返回:
- the Method object, or
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)
findDeclaredMethodWithMinimalParameters
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.- 参数:
clazz
- the class to checkmethodName
- the name of the method to find- 返回:
- the Method object, or
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()
findMethodWithMinimalParameters
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.- 参数:
methods
- the methods to checkmethodName
- the name of the method to find- 返回:
- the Method object, or
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
resolveSignature
public 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
.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.- 参数:
signature
- the method signature as String representationclazz
- the class to resolve the method signature against- 返回:
- the resolved Method
- 另请参阅:
findMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
,findMethodWithMinimalParameters(java.lang.Class<?>, java.lang.String)
getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException
Retrieve the JavaBeansPropertyDescriptor
s of a given class.- 参数:
clazz
- the Class to retrieve the PropertyDescriptors for- 返回:
- an array of
PropertyDescriptors
for the given class - 抛出:
BeansException
- if PropertyDescriptor look fails
getPropertyDescriptor
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException
Retrieve the JavaBeansPropertyDescriptors
for the given property.- 参数:
clazz
- the Class to retrieve the PropertyDescriptor forpropertyName
- the name of the property- 返回:
- the corresponding PropertyDescriptor, or
null
if none - 抛出:
BeansException
- if PropertyDescriptor lookup fails
findPropertyForMethod
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.- 参数:
method
- the method to find a corresponding PropertyDescriptor for, introspecting its declaring class- 返回:
- the corresponding PropertyDescriptor, or
null
if none - 抛出:
BeansException
- if PropertyDescriptor lookup fails
findPropertyForMethod
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.- 参数:
method
- the method to find a corresponding PropertyDescriptor forclazz
- the (most specific) class to introspect for descriptors- 返回:
- the corresponding PropertyDescriptor, or
null
if none - 抛出:
BeansException
- if PropertyDescriptor lookup fails- 从以下版本开始:
- 3.2.13
findEditorByConvention
public static PropertyEditor findEditorByConvention(Class<?> targetType)
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.- 参数:
targetType
- the type to find an editor for- 返回:
- the corresponding editor, or
null
if none found
findPropertyType
public static Class<?> findPropertyType(String propertyName, Class<?>... beanClasses)
Determine the bean property type for the given property from the given classes/interfaces, if possible.- 参数:
propertyName
- the name of the bean propertybeanClasses
- the classes to check against- 返回:
- the property type, or
Object.class
as fallback
getWriteMethodParameter
public static MethodParameter getWriteMethodParameter(PropertyDescriptor pd)
Obtain a new MethodParameter object for the write method of the specified property.- 参数:
pd
- the PropertyDescriptor for the property- 返回:
- a corresponding MethodParameter object
isSimpleProperty
public static boolean isSimpleProperty(Class<?> clazz)
Check if the given type represents a "simple" property: a primitive, a String or other CharSequence, a Number, a Date, a URI, a URL, a Locale, a Class, or a corresponding array.Used to determine properties to check for a "simple" dependency-check.
- 参数:
clazz
- the type to check- 返回:
- whether the given type represents a "simple" property
- 另请参阅:
AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE
,AbstractAutowireCapableBeanFactory.checkDependencies(java.lang.String, org.springframework.beans.factory.support.AbstractBeanDefinition, java.beans.PropertyDescriptor[], org.springframework.beans.PropertyValues)
isSimpleValueType
public static boolean isSimpleValueType(Class<?> clazz)
Check if the given type represents a "simple" value type: a primitive, an enum, a String or other CharSequence, a Number, a Date, a URI, a URL, a Locale or a Class.- 参数:
clazz
- the type to check- 返回:
- whether the given type represents a "simple" value type
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.
- 参数:
source
- the source beantarget
- the target bean- 抛出:
BeansException
- if the copying failed- 另请参阅:
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.
- 参数:
source
- the source beantarget
- the target beaneditable
- the class (or interface) to restrict property setting to- 抛出:
BeansException
- if the copying failed- 另请参阅:
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.
- 参数:
source
- the source beantarget
- the target beanignoreProperties
- array of property names to ignore- 抛出:
BeansException
- if the copying failed- 另请参阅:
BeanWrapper