Class TypeUtils


  • public abstract class TypeUtils
    extends java.lang.Object
    Utility methods focusing on type inspection, particularly with regard to generics.
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> getPrimitiveWrapper​(java.lang.Class<?> primitiveType)
      Gets the wrapper object class for the given primitive type class.
      static boolean isAssignable​(java.lang.Class<?> lhsType, java.lang.Class<?> rhsType)
      Check if the right-hand side type may be assigned to the left-hand side type, assuming setting by reflection.
      static boolean isAssignableValue​(java.lang.Class<?> type, java.lang.Object value)
      Determine if the given type is assignable from the given value, assuming setting by reflection.
      static boolean isPrimitiveArray​(java.lang.Class<?> clazz)
      Check if the given class represents an array of primitives, i.e.
      static boolean isPrimitiveWrapper​(java.lang.Class<?> clazz)
      Check if the given class represents a primitive wrapper, i.e.
      static boolean isPrimitiveWrapperArray​(java.lang.Class<?> clazz)
      Check if the given class represents an array of primitive wrappers, i.e.
      static java.lang.Class<?>[] wrappersToPrimitives​(java.lang.Class<?>[] classes)
      Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.
      static java.lang.Class<?> wrapperToPrimitive​(java.lang.Class<?> cls)
      Converts the specified wrapper class to its corresponding primitive class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TypeUtils

        public TypeUtils()
    • Method Detail

      • isPrimitiveWrapper

        public static boolean isPrimitiveWrapper​(java.lang.Class<?> clazz)
        Check if the given class represents a primitive wrapper, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
        Parameters:
        clazz - the class to check
        Returns:
        whether the given class is a primitive wrapper class
      • isPrimitiveArray

        public static boolean isPrimitiveArray​(java.lang.Class<?> clazz)
        Check if the given class represents an array of primitives, i.e. boolean, byte, char, short, int, long, float, or double.
        Parameters:
        clazz - the class to check
        Returns:
        whether the given class is a primitive array class
      • isPrimitiveWrapperArray

        public static boolean isPrimitiveWrapperArray​(java.lang.Class<?> clazz)
        Check if the given class represents an array of primitive wrappers, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
        Parameters:
        clazz - the class to check
        Returns:
        whether the given class is a primitive wrapper array class
      • isAssignable

        public static boolean isAssignable​(java.lang.Class<?> lhsType,
                                           java.lang.Class<?> rhsType)
        Check if the right-hand side type may be assigned to the left-hand side type, assuming setting by reflection. Considers primitive wrapper classes as assignable to the corresponding primitive types.
        Parameters:
        lhsType - the target type
        rhsType - the value type that should be assigned to the target type
        Returns:
        if the target type is assignable from the value type
      • isAssignableValue

        public static boolean isAssignableValue​(java.lang.Class<?> type,
                                                java.lang.Object value)
        Determine if the given type is assignable from the given value, assuming setting by reflection. Considers primitive wrapper classes as assignable to the corresponding primitive types.
        Parameters:
        type - the target type
        value - the value that should be assigned to the type
        Returns:
        if the type is assignable from the value
      • getPrimitiveWrapper

        public static java.lang.Class<?> getPrimitiveWrapper​(java.lang.Class<?> primitiveType)
        Gets the wrapper object class for the given primitive type class. For example, passing boolean.class returns Boolean.class
        Parameters:
        primitiveType - the primitive type class for which a match is to be found
        Returns:
        the wrapper type associated with the given primitive or null if no match is found
      • wrapperToPrimitive

        public static java.lang.Class<?> wrapperToPrimitive​(java.lang.Class<?> cls)

        Converts the specified wrapper class to its corresponding primitive class.

        This method is the counter part of primitiveToWrapper(). If the passed in class is a wrapper class for a primitive type, this primitive type will be returned (e.g. Integer.TYPE for Integer.class). For other classes, or if the parameter is null, the return value is null.

        Parameters:
        cls - the class to convert, may be null
        Returns:
        the corresponding primitive type if cls is a wrapper class, null otherwise
      • wrappersToPrimitives

        public static java.lang.Class<?>[] wrappersToPrimitives​(java.lang.Class<?>[] classes)

        Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.

        This method invokes wrapperToPrimitive() for each element of the passed in array.

        Parameters:
        classes - the class array to convert, may be null or empty
        Returns:
        an array which contains for each given class, the primitive class or null if the original class is not a wrapper class. null if null input. Empty array if an empty array passed in.
        See Also:
        wrapperToPrimitive(Class)