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 Type
Returns the element type for an Array Type.static Type
Perform 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 boolean
Checks whether a givenType
is an Array Type.static boolean
isSuperType
(Type superType, Type subType) Tests whether a given type is a supertype of another typestatic Type
parameterizeClass
(Class<?> clazz, Type... arguments) Creates a type of classclazz
witharguments
as type arguments.static Type
resolveMapEntryType
(Type mapType) Given a subtype ofMap<K,V>
, returns the corresponding map entry typeMap.Entry<K,V>
.static Type
resolveMapEntryType
(Type keyType, Type valueType) Given a key and value type, returns the map entry typeMap.Entry<keyType,valueType>
.static Type
resolveType
(Type type, Type contextType) Resolves thetype
parameter 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
type
isArrayList<String>
,parameterizedSupertype
isList.class
, andn
is0
, returnsOptional.of(String.class)
. - if
type
isMap<String, Integer>
,parameterizedSupertype
isMap.class
, andn
is1
, returnsOptional.of(Integer.class)
. - if
type
isArrayList.class
(raw),parameterizedSupertype
isList.class
, andn
is0
, 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 thetype
parameter in the context ofcontextType
. For example, iftype
isList.class.getMethod("get", int.class).getGenericReturnType()
, andcontextType
isList<String>
, this method returnsString.class
- Parameters:
type
- the type to be resolved in the scope ofcontextType
contextType
- the context type in whichtype
is interpreted to resolve the type.- Returns:
- the resolved type.
-
isArray
Checks whether a givenType
is an Array Type.- Parameters:
type
- a type- Returns:
- whether the given
Type
is 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 classclazz
witharguments
as 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 simplyclazz
ifarguments
isnull
or 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.
-