Class GenericTypes

java.lang.Object
org.jdbi.v3.core.generic.GenericTypes

public class GenericTypes extends Object
Utilities for working with generic types.
  • Method Details

    • getErasedType

      public static Class<?> getErasedType(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 Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype)
      Parameters:
      type - the type
      parameterizedSupertype - the parameterized supertype
      Returns:
      the first parameter type
      See Also:
    • findGenericParameter

      public static Optional<Type> findGenericParameter(Type type, 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:
      ArrayIndexOutOfBoundsException - if n > the number of type variables the type has
    • resolveType

      public static Type resolveType(Type type, 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(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 Type arrayType(Type type)
      Returns the element type for an Array Type.
      Parameters:
      type - the array's element type
      Returns:
      the array Type
    • resolveMapEntryType

      public static Type resolveMapEntryType(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 Type resolveMapEntryType(Type keyType, 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 Type parameterizeClass(Class<?> clazz, 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 Type box(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(Type superType, 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.