Class GenericTypes


  • public class GenericTypes
    extends java.lang.Object
    Utilities for working with generic types.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.reflect.TypeVariable<java.lang.Class<java.util.Map>> KEY  
      private static java.lang.reflect.TypeVariable<java.lang.Class<java.util.Map>> VALUE  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GenericTypes()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type)
      Returns the element type for an Array Type.
      static java.lang.reflect.Type box​(java.lang.reflect.Type type)
      Perform boxing conversion on a Type, if it is a primitive type.
      static java.util.Optional<java.lang.reflect.Type> findGenericParameter​(java.lang.reflect.Type type, java.lang.Class<?> parameterizedSupertype)
      static java.util.Optional<java.lang.reflect.Type> findGenericParameter​(java.lang.reflect.Type type, java.lang.Class<?> parameterizedSupertype, int n)
      For the given type which extends parameterizedSupertype, returns the nth generic parameter for the parameterized supertype, if concretely expressed.
      static java.lang.Class<?> getErasedType​(java.lang.reflect.Type type)
      Returns the erased class for the given type.
      static boolean isArray​(java.lang.reflect.Type type)
      Checks whether a given Type is an Array Type.
      static boolean isSuperType​(java.lang.reflect.Type superType, java.lang.reflect.Type subType)
      Tests whether a given type is a supertype of another type
      static java.lang.reflect.Type parameterizeClass​(java.lang.Class<?> clazz, java.lang.reflect.Type... arguments)
      Creates a type of class clazz with arguments as type arguments.
      static java.lang.reflect.Type resolveMapEntryType​(java.lang.reflect.Type mapType)
      Given a subtype of Map<K,V>, returns the corresponding map entry type Map.Entry<K,V>.
      static java.lang.reflect.Type resolveMapEntryType​(java.lang.reflect.Type keyType, java.lang.reflect.Type valueType)
      Given a key and value type, returns the map entry type Map.Entry<keyType,valueType>.
      static java.lang.reflect.Type resolveType​(java.lang.reflect.Type type, java.lang.reflect.Type contextType)
      Resolves the type parameter in the context of contextType.
      • Methods inherited from class java.lang.Object

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

      • KEY

        private static final java.lang.reflect.TypeVariable<java.lang.Class<java.util.Map>> KEY
      • VALUE

        private static final java.lang.reflect.TypeVariable<java.lang.Class<java.util.Map>> VALUE
    • Constructor Detail

      • GenericTypes

        private GenericTypes()
    • Method Detail

      • getErasedType

        public static java.lang.Class<?> getErasedType​(java.lang.reflect.Type type)
        Returns the erased class for the given type.

        Example: if type is List<String>, returns List.class

        parameter
        Parameters:
        type - the type
        Returns:
        the erased class
      • findGenericParameter

        public static java.util.Optional<java.lang.reflect.Type> findGenericParameter​(java.lang.reflect.Type type,
                                                                                      java.lang.Class<?> parameterizedSupertype)
        Parameters:
        type - the type
        parameterizedSupertype - the parameterized supertype
        Returns:
        the first parameter type
        See Also:
        findGenericParameter(Type, Class, int)
      • findGenericParameter

        public static java.util.Optional<java.lang.reflect.Type> findGenericParameter​(java.lang.reflect.Type type,
                                                                                      java.lang.Class<?> parameterizedSupertype,
                                                                                      int n)
        For the given type which extends parameterizedSupertype, returns the nth generic parameter for the parameterized supertype, if concretely expressed.

        Example:

        • if type is ArrayList<String>, parameterizedSupertype is List.class, and n is 0, returns Optional.of(String.class).
        • if type is Map<String, Integer>, parameterizedSupertype is Map.class, and n is 1, returns Optional.of(Integer.class).
        • if type is ArrayList.class (raw), parameterizedSupertype is List.class, and n is 0, returns Optional.empty().
        Parameters:
        type - the subtype of parameterizedSupertype
        parameterizedSupertype - the parameterized supertype from which we want the generic parameter
        n - the index in Foo<X, Y, Z, ...>
        Returns:
        the parameter on the supertype, if it is concretely defined.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if n > the number of type variables the type has
      • resolveType

        public static java.lang.reflect.Type resolveType​(java.lang.reflect.Type type,
                                                         java.lang.reflect.Type contextType)
        Resolves the type parameter in the context of contextType. For example, if type is List.class.getMethod("get", int.class).getGenericReturnType(), and contextType is List<String>, this method returns String.class
        Parameters:
        type - the type to be resolved in the scope of contextType
        contextType - the context type in which type is interpreted to resolve the type.
        Returns:
        the resolved type.
      • isArray

        public static boolean isArray​(java.lang.reflect.Type type)
        Checks whether a given Type is an Array Type.
        Parameters:
        type - a type
        Returns:
        whether the given Type is an Array type.
      • arrayType

        public static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type)
        Returns the element type for an Array Type.
        Parameters:
        type - the array's element type
        Returns:
        the array Type
      • resolveMapEntryType

        public static java.lang.reflect.Type resolveMapEntryType​(java.lang.reflect.Type mapType)
        Given a subtype of Map<K,V>, returns the corresponding map entry type Map.Entry<K,V>.
        Parameters:
        mapType - the map subtype
        Returns:
        the map entry type
      • resolveMapEntryType

        public static java.lang.reflect.Type resolveMapEntryType​(java.lang.reflect.Type keyType,
                                                                 java.lang.reflect.Type valueType)
        Given a key and value type, returns the map entry type Map.Entry<keyType,valueType>.
        Parameters:
        keyType - the key type
        valueType - the value type
        Returns:
        the map entry type
      • parameterizeClass

        public static java.lang.reflect.Type parameterizeClass​(java.lang.Class<?> clazz,
                                                               java.lang.reflect.Type... arguments)
        Creates a type of class clazz with arguments as type arguments.

        For example: parameterizedClass(Map.class, Integer.class, String.class) returns the type Map<Integer, String>.

        Parameters:
        clazz - Type class of the type to create
        arguments - Type arguments for the variables of clazz, or null if these are not known.
        Returns:
        A ParameterizedType, or simply clazz if arguments is null or empty.
      • box

        public static java.lang.reflect.Type box​(java.lang.reflect.Type type)
        Perform boxing conversion on a Type, if it is a primitive type. Otherwise return the input argument.
        Parameters:
        type - the type to box
        Returns:
        the boxed type, or the input type if it is not a primitive
      • isSuperType

        public static boolean isSuperType​(java.lang.reflect.Type superType,
                                          java.lang.reflect.Type subType)
        Tests whether a given type is a supertype of another type
        Parameters:
        superType - The supertype to check.
        subType - The subtype to check.
        Returns:
        True if supertype is a supertype of subtype.