org.springframework.beans

Class BeanUtils

  • java.lang.Object
    • org.springframework.beans.BeanUtils


  • public abstract class BeanUtils
    extends java.lang.Object
    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.

    Author:
    Rod Johnson, Juergen Hoeller, Rob Harrop, Sam Brannen, Sebastien Deleuze
    • Constructor Summary

      Constructors 
      Constructor and Description
      BeanUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method and Description
      static void copyProperties(java.lang.Object source, java.lang.Object target)
      Copy the property values of the given source bean into the target bean.
      static void copyProperties(java.lang.Object source, java.lang.Object target, java.lang.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(java.lang.Object source, java.lang.Object target, java.lang.String... ignoreProperties)
      Copy the property values of the given source bean into the given target bean, ignoring the given "ignoreProperties".
      static java.lang.reflect.Method findDeclaredMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 java.lang.reflect.Method findDeclaredMethodWithMinimalParameters(java.lang.Class<?> clazz, java.lang.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 java.beans.PropertyEditor findEditorByConvention(java.lang.Class<?> targetType)
      Find a JavaBeans PropertyEditor following the 'Editor' suffix convention (e.g.
      static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 java.lang.reflect.Method findMethodWithMinimalParameters(java.lang.Class<?> clazz, java.lang.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 java.lang.reflect.Method findMethodWithMinimalParameters(java.lang.reflect.Method[] methods, java.lang.String methodName)
      Find a method with the given method name and minimal parameters (best case: none) in the given list of methods.
      static <T> java.lang.reflect.Constructor<T> findPrimaryConstructor(java.lang.Class<T> clazz)
      Return the primary constructor of the provided class.
      static java.beans.PropertyDescriptor findPropertyForMethod(java.lang.reflect.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 java.beans.PropertyDescriptor findPropertyForMethod(java.lang.reflect.Method method, java.lang.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 java.lang.Class<?> findPropertyType(java.lang.String propertyName, java.lang.Class<?>... beanClasses)
      Determine the bean property type for the given property from the given classes/interfaces, if possible.
      static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> clazz, java.lang.String propertyName)
      Retrieve the JavaBeans PropertyDescriptors for the given property.
      static java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class<?> clazz)
      Retrieve the JavaBeans PropertyDescriptors of a given class.
      static MethodParameter getWriteMethodParameter(java.beans.PropertyDescriptor pd)
      Obtain a new MethodParameter object for the write method of the specified property.
      static <T> T instantiate(java.lang.Class<T> clazz)
      Deprecated. 
      as of Spring 5.0, following the deprecation of Class.newInstance() in JDK 9
      static <T> T instantiateClass(java.lang.Class<?> clazz, java.lang.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(java.lang.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(java.lang.reflect.Constructor<T> ctor, java.lang.Object... args)
      Convenience method to instantiate a class using the given constructor.
      static boolean isSimpleProperty(java.lang.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(java.lang.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 java.lang.reflect.Method resolveSignature(java.lang.String signature, java.lang.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

      • BeanUtils

        public BeanUtils()
    • Method Detail

      • instantiate

        @Deprecated
        public static <T> T instantiate(java.lang.Class<T> clazz)
                                             throws BeanInstantiationException
        Deprecated. as of Spring 5.0, following the deprecation of Class.newInstance() in JDK 9
        Convenience 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:
        Class.newInstance()
      • instantiateClass

        public static <T> T instantiateClass(java.lang.Class<T> clazz)
                                      throws BeanInstantiationException
        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 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.
        See Also:
        Constructor.newInstance(java.lang.Object...)
      • instantiateClass

        public static <T> T instantiateClass(java.lang.Class<?> clazz,
                                             java.lang.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 instantiate
        assignableTo - type that clazz must be assignableTo
        Returns:
        the new instance
        Throws:
        BeanInstantiationException - if the bean cannot be instantiated
        See Also:
        Constructor.newInstance(java.lang.Object...)
      • instantiateClass

        public static <T> T instantiateClass(java.lang.reflect.Constructor<T> ctor,
                                             java.lang.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 instantiate
        args - the constructor arguments to apply (use null for an unspecified parameter if needed for Kotlin classes with optional parameters and default values)
        Returns:
        the new instance
        Throws:
        BeanInstantiationException - if the bean cannot be instantiated
        See Also:
        Constructor.newInstance(java.lang.Object...)
      • findPrimaryConstructor

        @Nullable
        public static <T> java.lang.reflect.Constructor<T> findPrimaryConstructor(java.lang.Class<T> clazz)
        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 returns null.
        Parameters:
        clazz - the class to check
        Since:
        5.0
        See Also:
        Kotlin docs
      • findMethod

        @Nullable
        public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz,
                                                                    java.lang.String methodName,
                                                                    java.lang.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 to findDeclaredMethod. This allows to find public methods without issues even in environments with restricted Java security settings.

        Parameters:
        clazz - the class to check
        methodName - the name of the method to find
        paramTypes - the parameter types of the method to find
        Returns:
        the Method object, or null if not found
        See Also:
        Class.getMethod(java.lang.String, java.lang.Class<?>...), findDeclaredMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
      • findDeclaredMethod

        @Nullable
        public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class<?> clazz,
                                                                            java.lang.String methodName,
                                                                            java.lang.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 check
        methodName - the name of the method to find
        paramTypes - the parameter types of the method to find
        Returns:
        the Method object, or null if not found
        See Also:
        Class.getDeclaredMethod(java.lang.String, java.lang.Class<?>...)
      • findMethodWithMinimalParameters

        @Nullable
        public static java.lang.reflect.Method findMethodWithMinimalParameters(java.lang.Class<?> clazz,
                                                                                         java.lang.String methodName)
                                                                                  throws java.lang.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 to findDeclaredMethodWithMinimalParameters. This allows for finding public methods without issues even in environments with restricted Java security settings.

        Parameters:
        clazz - the class to check
        methodName - the name of the method to find
        Returns:
        the Method object, or null if not found
        Throws:
        java.lang.IllegalArgumentException - if methods of the given name were found but could not be resolved to a unique method with minimal parameters
        See Also:
        Class.getMethods(), findDeclaredMethodWithMinimalParameters(java.lang.Class<?>, java.lang.String)
      • findDeclaredMethodWithMinimalParameters

        @Nullable
        public static java.lang.reflect.Method findDeclaredMethodWithMinimalParameters(java.lang.Class<?> clazz,
                                                                                                 java.lang.String methodName)
                                                                                          throws java.lang.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 check
        methodName - the name of the method to find
        Returns:
        the Method object, or null if not found
        Throws:
        java.lang.IllegalArgumentException - if methods of the given name were found but could not be resolved to a unique method with minimal parameters
        See Also:
        Class.getDeclaredMethods()
      • findMethodWithMinimalParameters

        @Nullable
        public static java.lang.reflect.Method findMethodWithMinimalParameters(java.lang.reflect.Method[] methods,
                                                                                         java.lang.String methodName)
                                                                                  throws java.lang.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 check
        methodName - the name of the method to find
        Returns:
        the Method object, or null if not found
        Throws:
        java.lang.IllegalArgumentException - if methods of the given name were found but could not be resolved to a unique method with minimal parameters
      • resolveSignature

        @Nullable
        public static java.lang.reflect.Method resolveSignature(java.lang.String signature,
                                                                          java.lang.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.

        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.

        Parameters:
        signature - the method signature as String representation
        clazz - the class to resolve the method signature against
        Returns:
        the resolved Method
        See Also:
        findMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...), findMethodWithMinimalParameters(java.lang.Class<?>, java.lang.String)
      • getPropertyDescriptors

        public static java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class<?> clazz)
                                                                      throws BeansException
        Retrieve the JavaBeans PropertyDescriptors 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 java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> clazz,
                                                                                    java.lang.String propertyName)
                                                                             throws BeansException
        Retrieve the JavaBeans PropertyDescriptors for the given property.
        Parameters:
        clazz - the Class to retrieve the PropertyDescriptor for
        propertyName - the name of the property
        Returns:
        the corresponding PropertyDescriptor, or null if none
        Throws:
        BeansException - if PropertyDescriptor lookup fails
      • findPropertyForMethod

        @Nullable
        public static java.beans.PropertyDescriptor findPropertyForMethod(java.lang.reflect.Method method)
                                                                             throws BeansException
        Find a JavaBeans PropertyDescriptor 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 java.beans.PropertyDescriptor findPropertyForMethod(java.lang.reflect.Method method,
                                                                                    java.lang.Class<?> clazz)
                                                                             throws BeansException
        Find a JavaBeans PropertyDescriptor 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
        clazz - 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

        @Nullable
        public static java.beans.PropertyEditor findEditorByConvention(@Nullable
                                                                                 java.lang.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.

        Parameters:
        targetType - the type to find an editor for
        Returns:
        the corresponding editor, or null if none found
      • findPropertyType

        public static java.lang.Class<?> findPropertyType(java.lang.String propertyName,
                                                          @Nullable
                                                          java.lang.Class<?>... beanClasses)
        Determine the bean property type for the given property from the given classes/interfaces, if possible.
        Parameters:
        propertyName - the name of the bean property
        beanClasses - the classes to check against
        Returns:
        the property type, or Object.class as fallback
      • getWriteMethodParameter

        public static MethodParameter getWriteMethodParameter(java.beans.PropertyDescriptor pd)
        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
      • isSimpleValueType

        public static boolean isSimpleValueType(java.lang.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.
        Parameters:
        clazz - the type to check
        Returns:
        whether the given type represents a "simple" value type
      • copyProperties

        public static void copyProperties(java.lang.Object source,
                                          java.lang.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.

        Parameters:
        source - the source bean
        target - the target bean
        Throws:
        BeansException - if the copying failed
        See Also:
        BeanWrapper
      • copyProperties

        public static void copyProperties(java.lang.Object source,
                                          java.lang.Object target,
                                          java.lang.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.

        Parameters:
        source - the source bean
        target - the target bean
        editable - the class (or interface) to restrict property setting to
        Throws:
        BeansException - if the copying failed
        See Also:
        BeanWrapper
      • copyProperties

        public static void copyProperties(java.lang.Object source,
                                          java.lang.Object target,
                                          java.lang.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.

        Parameters:
        source - the source bean
        target - the target bean
        ignoreProperties - array of property names to ignore
        Throws:
        BeansException - if the copying failed
        See Also:
        BeanWrapper