Class Types


  • public class Types
    extends java.lang.Object
    Utilities for working with Java types.
    Since:
    1.4
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Types()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.reflect.Type getActualParameterAtPosition​(java.lang.reflect.Type type, java.lang.Class<?> superClass, int position)  
      static java.lang.reflect.Type getArrayComponentType​(java.lang.reflect.Type array)
      Returns the component type of the given array type, assuming isArray(Type).
      static java.lang.reflect.Type getBound​(java.lang.reflect.WildcardType wildcardType)
      Returns the only bound of the given wildcard type.
      static java.lang.reflect.Type getIterableParameter​(java.lang.reflect.Type iterableType)
      Returns the type parameter of Iterable that is assignable from the given iterable type.
      static java.lang.reflect.Type getMapValueParameter​(java.lang.reflect.Type mapType)
      Returns the value type parameter of Map that is assignable from the given map type.
      static java.lang.Class<?> getRawArrayComponentType​(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.Type componentType)
      Returns the raw array component type to use -- for example for the first parameter of Array.newInstance(Class, int) -- for the given component type.
      static java.lang.Class<?> getRawClass​(java.lang.reflect.ParameterizedType parameterType)
      Returns the raw class for the given parameter type as defined in ParameterizedType.getRawType().
      static java.lang.reflect.ParameterizedType getSuperParameterizedType​(java.lang.reflect.Type type, java.lang.Class<?> superClass)
      Returns the parameterized type that is or extends the given type that matches the given super class.
      private static java.lang.IllegalArgumentException handleExceptionForNewInstance​(java.lang.Exception e, java.lang.Class<?> clazz)  
      static boolean isArray​(java.lang.reflect.Type type)
      Returns whether the given type is an array.
      static boolean isAssignableToOrFrom​(java.lang.Class<?> classToCheck, java.lang.Class<?> anotherClass)
      Returns whether a class is either assignable to or from another class.
      static <T> java.lang.Iterable<T> iterableOf​(java.lang.Object value)
      Returns an iterable for an input iterable or array value.
      static <T> T newInstance​(java.lang.Class<T> clazz)
      Creates a new instance of the given class by invoking its default constructor.
      static java.lang.reflect.Type resolveTypeVariable​(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.TypeVariable<?> typeVariable)
      Resolves the actual type of the given type variable that comes from a field type based on the given context list.
      static java.lang.Object toArray​(java.util.Collection<?> collection, java.lang.Class<?> componentType)
      Returns a new array of the given component type (possibly a Java primitive) that is a copy of the content of the given collection.
      • Methods inherited from class java.lang.Object

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

      • Types

        private Types()
    • Method Detail

      • getSuperParameterizedType

        public static java.lang.reflect.ParameterizedType getSuperParameterizedType​(java.lang.reflect.Type type,
                                                                                    java.lang.Class<?> superClass)
        Returns the parameterized type that is or extends the given type that matches the given super class.

        For example, if the input type is HashMap<String,Integer> and the input super class is Map.class, it will return the extended parameterized type Map, but which retains the actual type information from the original HashMap.

        Parameters:
        type - class or parameterized type
        superClass - super class
        Returns:
        matching parameterized type or null
      • isAssignableToOrFrom

        public static boolean isAssignableToOrFrom​(java.lang.Class<?> classToCheck,
                                                   java.lang.Class<?> anotherClass)
        Returns whether a class is either assignable to or from another class.
        Parameters:
        classToCheck - class to check
        anotherClass - another class
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> clazz)
        Creates a new instance of the given class by invoking its default constructor.

        The given class must be public and must have a public default constructor, and must not be an array or an interface or be abstract. If an enclosing class, it must be static.

      • handleExceptionForNewInstance

        private static java.lang.IllegalArgumentException handleExceptionForNewInstance​(java.lang.Exception e,
                                                                                        java.lang.Class<?> clazz)
      • isArray

        public static boolean isArray​(java.lang.reflect.Type type)
        Returns whether the given type is an array.
      • getArrayComponentType

        public static java.lang.reflect.Type getArrayComponentType​(java.lang.reflect.Type array)
        Returns the component type of the given array type, assuming isArray(Type).

        Return type will either be class, parameterized type, generic array type, or type variable, but not a wildcard type.

        Throws:
        java.lang.ClassCastException - if isArray(Type) is false
      • getRawClass

        public static java.lang.Class<?> getRawClass​(java.lang.reflect.ParameterizedType parameterType)
        Returns the raw class for the given parameter type as defined in ParameterizedType.getRawType().
        Parameters:
        parameterType - parameter type
        Returns:
        raw class
      • getBound

        public static java.lang.reflect.Type getBound​(java.lang.reflect.WildcardType wildcardType)
        Returns the only bound of the given wildcard type.
        Parameters:
        wildcardType - wildcard type
        Returns:
        only bound or Object.class for none
      • resolveTypeVariable

        public static java.lang.reflect.Type resolveTypeVariable​(java.util.List<java.lang.reflect.Type> context,
                                                                 java.lang.reflect.TypeVariable<?> typeVariable)
        Resolves the actual type of the given type variable that comes from a field type based on the given context list.

        In case the type variable can be resolved partially, it will return the partially resolved type variable.

        Parameters:
        context - context list, ordering from least specific to most specific type context, for example container class and then its field
        typeVariable - type variable
        Returns:
        resolved or partially resolved actual type (type variable, class, parameterized type, or generic array type, but not wildcard type) or null if unable to resolve at all
      • getRawArrayComponentType

        public static java.lang.Class<?> getRawArrayComponentType​(java.util.List<java.lang.reflect.Type> context,
                                                                  java.lang.reflect.Type componentType)
        Returns the raw array component type to use -- for example for the first parameter of Array.newInstance(Class, int) -- for the given component type.
        Parameters:
        context - context list, ordering from least specific to most specific type context, for example container class and then its field
        componentType - array component type or null for Object.class result
        Returns:
        raw array component type
      • getIterableParameter

        public static java.lang.reflect.Type getIterableParameter​(java.lang.reflect.Type iterableType)
        Returns the type parameter of Iterable that is assignable from the given iterable type.

        For example, for the type ArrayList<Integer> -- or for a class that extends ArrayList<Integer> -- it will return Integer.

        Parameters:
        iterableType - iterable type (must extend Iterable)
        Returns:
        type parameter, which may be any type
      • getMapValueParameter

        public static java.lang.reflect.Type getMapValueParameter​(java.lang.reflect.Type mapType)
        Returns the value type parameter of Map that is assignable from the given map type.

        For example, for the type Map<String, Integer> -- or for a class that extends Map<String, Integer> -- it will return Integer.

        Parameters:
        mapType - map type (must extend Map)
        Returns:
        type parameter, which may be any type
      • getActualParameterAtPosition

        private static java.lang.reflect.Type getActualParameterAtPosition​(java.lang.reflect.Type type,
                                                                           java.lang.Class<?> superClass,
                                                                           int position)
      • iterableOf

        public static <T> java.lang.Iterable<T> iterableOf​(java.lang.Object value)
        Returns an iterable for an input iterable or array value.

        If the input value extends Iterable, it will just return the input value. Otherwise, it will return an iterable that can handle arrays of primitive and non-primitive component type.

        Parameters:
        value - iterable (extends Iterable) or array value
        Returns:
        iterable
      • toArray

        public static java.lang.Object toArray​(java.util.Collection<?> collection,
                                               java.lang.Class<?> componentType)
        Returns a new array of the given component type (possibly a Java primitive) that is a copy of the content of the given collection.
        Parameters:
        collection - collection
        componentType - component type (possibly a Java primitive)
        Returns:
        new array