Package org.jdbi.v3.core.generic
Class GenericTypes
java.lang.Object
org.jdbi.v3.core.generic.GenericTypes
Utilities for working with generic types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic TypeReturns the element type for an Array Type.static TypePerform boxing conversion on aType, if it is a primitive type.findGenericParameter(Type type, Class<?> parameterizedSupertype) Same asfindGenericParameter(Type, Class, int)with n = 0.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.static Class<?> getErasedType(Type type) Returns the erased class for the given type.static booleanChecks whether a givenTypeis an Array Type.static booleanisSuperType(Type superType, Type subType) Tests whether a given type is a supertype of another typestatic TypeparameterizeClass(Class<?> clazz, Type... arguments) Creates a type of classclazzwithargumentsas type arguments.static TyperesolveMapEntryType(Type mapType) Given a subtype ofMap<K,V>, returns the corresponding map entry typeMap.Entry<K,V>.static TyperesolveMapEntryType(Type keyType, Type valueType) Given a key and value type, returns the map entry typeMap.Entry<keyType,valueType>.static TyperesolveType(Type type, Type contextType) Resolves thetypeparameter in the context ofcontextType.
-
Method Details
-
getErasedType
Returns the erased class for the given type.Example: if type is
parameterList<String>, returnsList.class- Parameters:
type- the type- Returns:
- the erased class
-
findGenericParameter
Same asfindGenericParameter(Type, Class, int)with n = 0.- Parameters:
type- the typeparameterizedSupertype- 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
typeisArrayList<String>,parameterizedSupertypeisList.class, andnis0, returnsOptional.of(String.class). - if
typeisMap<String, Integer>,parameterizedSupertypeisMap.class, andnis1, returnsOptional.of(Integer.class). - if
typeisArrayList.class(raw),parameterizedSupertypeisList.class, andnis0, returnsOptional.empty().
- Parameters:
type- the subtype of parameterizedSupertypeparameterizedSupertype- the parameterized supertype from which we want the generic parametern- the index inFoo<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
- if
-
resolveType
Resolves thetypeparameter in the context ofcontextType. For example, iftypeisList.class.getMethod("get", int.class).getGenericReturnType(), andcontextTypeisList<String>, this method returnsString.class- Parameters:
type- the type to be resolved in the scope ofcontextTypecontextType- the context type in whichtypeis interpreted to resolve the type.- Returns:
- the resolved type.
-
isArray
Checks whether a givenTypeis an Array Type.- Parameters:
type- a type- Returns:
- whether the given
Typeis an Array type.
-
arrayType
Returns the element type for an Array Type.- Parameters:
type- the array's element type- Returns:
- the array Type
-
resolveMapEntryType
Given a subtype ofMap<K,V>, returns the corresponding map entry typeMap.Entry<K,V>.- Parameters:
mapType- the map subtype- Returns:
- the map entry type
-
resolveMapEntryType
Given a key and value type, returns the map entry typeMap.Entry<keyType,valueType>.- Parameters:
keyType- the key typevalueType- the value type- Returns:
- the map entry type
-
parameterizeClass
Creates a type of classclazzwithargumentsas type arguments.For example:
parameterizedClass(Map.class, Integer.class, String.class)returns the typeMap<Integer, String>.- Parameters:
clazz- Type class of the type to createarguments- Type arguments for the variables ofclazz, or null if these are not known.- Returns:
- A
ParameterizedType, or simplyclazzifargumentsisnullor empty.
-
box
Perform boxing conversion on aType, 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
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.
-