类 ObjectUtils


  • public abstract class ObjectUtils
    extends Object
    Miscellaneous object utility methods.

    Mainly for internal use within the framework.

    Thanks to Alex Ruiz for contributing several enhancements to this class!

    从以下版本开始:
    19.03.2004
    作者:
    Juergen Hoeller, Keith Donald, Rod Johnson, Rob Harrop, Chris Beams, Sam Brannen
    另请参阅:
    ClassUtils, CollectionUtils, StringUtils
    • 构造器概要

      构造器 
      构造器说明
      ObjectUtils() 
    • 方法概要

      所有方法 静态方法 具体方法 已过时的方法 
      修饰符和类型方法说明
      static <A,​O extends A>
      A[]
      addObjectToArray​(A[] array, O obj)
      Append the given object to the given array, returning a new array consisting of the input array contents plus the given object.
      static <E extends Enum<?>>
      E
      caseInsensitiveValueOf​(E[] enumValues, String constant)
      Case insensitive alternative to Enum.valueOf(Class, String).
      static booleancontainsConstant​(Enum<?>[] enumValues, String constant)
      Check whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.
      static booleancontainsConstant​(Enum<?>[] enumValues, String constant, boolean caseSensitive)
      Check whether the given array of enum constants contains a constant with the given name.
      static booleancontainsElement​(Object[] array, Object element)
      Check whether the given array contains the given element.
      static StringgetDisplayString​(Object obj)
      Return a content-based String representation if obj is not null; otherwise returns an empty String.
      static StringgetIdentityHexString​(Object obj)
      Return a hex String form of an object's identity hash code.
      static inthashCode​(boolean bool)
      已过时。
      as of Spring Framework 5.0, in favor of the native JDK 8 variant
      static inthashCode​(double dbl)
      已过时。
      as of Spring Framework 5.0, in favor of the native JDK 8 variant
      static inthashCode​(float flt)
      已过时。
      as of Spring Framework 5.0, in favor of the native JDK 8 variant
      static inthashCode​(long lng)
      已过时。
      as of Spring Framework 5.0, in favor of the native JDK 8 variant
      static StringidentityToString​(Object obj)
      Return a String representation of an object's overall identity.
      static booleanisArray​(Object obj)
      Determine whether the given object is an array: either an Object array or a primitive array.
      static booleanisCheckedException​(Throwable ex)
      Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.
      static booleanisCompatibleWithThrowsClause​(Throwable ex, Class<?>... declaredExceptions)
      Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.
      static booleanisEmpty​(Object obj)
      Determine whether the given object is empty.
      static booleanisEmpty​(Object[] array)
      Determine whether the given array is empty: i.e.
      static StringnullSafeClassName​(Object obj)
      Determine the class name for the given object.
      static booleannullSafeEquals​(Object o1, Object o2)
      Determine if the given objects are equal, returning true if both are null or false if only one is null.
      static intnullSafeHashCode​(boolean[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(byte[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(char[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(double[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(float[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(int[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(long[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(short[] array)
      Return a hash code based on the contents of the specified array.
      static intnullSafeHashCode​(Object obj)
      Return as hash code for the given object; typically the value of Object#hashCode()}.
      static intnullSafeHashCode​(Object[] array)
      Return a hash code based on the contents of the specified array.
      static StringnullSafeToString​(boolean[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(byte[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(char[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(double[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(float[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(int[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(long[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(short[] array)
      Return a String representation of the contents of the specified array.
      static StringnullSafeToString​(Object obj)
      Return a String representation of the specified Object.
      static StringnullSafeToString​(Object[] array)
      Return a String representation of the contents of the specified array.
      static Object[]toObjectArray​(Object source)
      Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).
      static ObjectunwrapOptional​(Object obj)
      Unwrap the given object which is potentially a Optional.
    • 方法详细资料

      • isCheckedException

        public static boolean isCheckedException​(Throwable ex)
        Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.
        参数:
        ex - the throwable to check
        返回:
        whether the throwable is a checked exception
        另请参阅:
        Exception, RuntimeException, Error
      • isCompatibleWithThrowsClause

        public static boolean isCompatibleWithThrowsClause​(Throwable ex,
                                                           @Nullable
                                                           Class<?>... declaredExceptions)
        Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.
        参数:
        ex - the exception to check
        declaredExceptions - the exception types declared in the throws clause
        返回:
        whether the given exception is compatible
      • isArray

        public static boolean isArray​(@Nullable
                                      Object obj)
        Determine whether the given object is an array: either an Object array or a primitive array.
        参数:
        obj - the object to check
      • isEmpty

        public static boolean isEmpty​(@Nullable
                                      Object[] array)
        Determine whether the given array is empty: i.e. null or of zero length.
        参数:
        array - the array to check
        另请参阅:
        isEmpty(Object)
      • unwrapOptional

        @Nullable
        public static Object unwrapOptional​(@Nullable
                                            Object obj)
        Unwrap the given object which is potentially a Optional.
        参数:
        obj - the candidate object
        返回:
        either the value held within the Optional, null if the Optional is empty, or simply the given object as-is
        从以下版本开始:
        5.0
      • containsElement

        public static boolean containsElement​(@Nullable
                                              Object[] array,
                                              Object element)
        Check whether the given array contains the given element.
        参数:
        array - the array to check (may be null, in which case the return value will always be false)
        element - the element to check for
        返回:
        whether the element has been found in the given array
      • containsConstant

        public static boolean containsConstant​(Enum<?>[] enumValues,
                                               String constant)
        Check whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.
        参数:
        enumValues - the enum values to check, typically obtained via MyEnum.values()
        constant - the constant name to find (must not be null or empty string)
        返回:
        whether the constant has been found in the given array
      • containsConstant

        public static boolean containsConstant​(Enum<?>[] enumValues,
                                               String constant,
                                               boolean caseSensitive)
        Check whether the given array of enum constants contains a constant with the given name.
        参数:
        enumValues - the enum values to check, typically obtained via MyEnum.values()
        constant - the constant name to find (must not be null or empty string)
        caseSensitive - whether case is significant in determining a match
        返回:
        whether the constant has been found in the given array
      • addObjectToArray

        public static <A,​O extends A> A[] addObjectToArray​(@Nullable
                                                                 A[] array,
                                                                 @Nullable
                                                                 O obj)
        Append the given object to the given array, returning a new array consisting of the input array contents plus the given object.
        参数:
        array - the array to append to (can be null)
        obj - the object to append
        返回:
        the new array (of the same component type; never null)
      • toObjectArray

        public static Object[] toObjectArray​(@Nullable
                                             Object source)
        Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).

        A null source value will be converted to an empty Object array.

        参数:
        source - the (potentially primitive) array
        返回:
        the corresponding object array (never null)
        抛出:
        IllegalArgumentException - if the parameter is not an array
      • nullSafeEquals

        public static boolean nullSafeEquals​(@Nullable
                                             Object o1,
                                             @Nullable
                                             Object o2)
        Determine if the given objects are equal, returning true if both are null or false if only one is null.

        Compares arrays with Arrays.equals, performing an equality check based on the array elements rather than the array reference.

        参数:
        o1 - first Object to compare
        o2 - second Object to compare
        返回:
        whether the given objects are equal
        另请参阅:
        Object.equals(Object), Arrays.equals(long[], long[])
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           Object[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           boolean[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           byte[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           char[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           double[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           float[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           int[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           long[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • nullSafeHashCode

        public static int nullSafeHashCode​(@Nullable
                                           short[] array)
        Return a hash code based on the contents of the specified array. If array is null, this method returns 0.
      • identityToString

        public static String identityToString​(@Nullable
                                              Object obj)
        Return a String representation of an object's overall identity.
        参数:
        obj - the object (may be null)
        返回:
        the object's identity as String representation, or an empty String if the object was null
      • getIdentityHexString

        public static String getIdentityHexString​(Object obj)
        Return a hex String form of an object's identity hash code.
        参数:
        obj - the object
        返回:
        the object's identity code in hex notation
      • getDisplayString

        public static String getDisplayString​(@Nullable
                                              Object obj)
        Return a content-based String representation if obj is not null; otherwise returns an empty String.

        Differs from nullSafeToString(Object) in that it returns an empty String rather than "null" for a null value.

        参数:
        obj - the object to build a display String for
        返回:
        a display String representation of obj
        另请参阅:
        nullSafeToString(Object)
      • nullSafeClassName

        public static String nullSafeClassName​(@Nullable
                                               Object obj)
        Determine the class name for the given object.

        Returns a "null" String if obj is null.

        参数:
        obj - the object to introspect (may be null)
        返回:
        the corresponding class name
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              Object obj)
        Return a String representation of the specified Object.

        Builds a String representation of the contents in case of an array. Returns a "null" String if obj is null.

        参数:
        obj - the object to build a String representation for
        返回:
        a String representation of obj
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              Object[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              boolean[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              byte[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              char[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              double[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              float[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              int[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              long[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array
      • nullSafeToString

        public static String nullSafeToString​(@Nullable
                                              short[] array)
        Return a String representation of the contents of the specified array.

        The String representation consists of a list of the array's elements, enclosed in curly braces ("{}"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Returns a "null" String if array is null.

        参数:
        array - the array to build a String representation for
        返回:
        a String representation of array