public class GenericTypes extends Object
| Modifier and Type | Method and Description |
|---|---|
static Type |
box(Type type)
Perform boxing conversion on a
Type, if it is a primitive type. |
static Optional<Type> |
findGenericParameter(Type type,
Class<?> parameterizedSupertype)
Same as
findGenericParameter(Type, Class, int) with n = 0. |
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.
|
static Class<?> |
getErasedType(Type type)
Returns the erased class for the given type.
|
static boolean |
isArray(Type type) |
static Type |
parameterizeClass(Class<?> clazz,
Type... arguments)
Creates a type of class
clazz with arguments as type arguments. |
static Type |
resolveMapEntryType(Type mapType)
Given a subtype of
Map<K,V>, returns the corresponding map entry type Map.Entry<K,V>. |
static Type |
resolveMapEntryType(Type keyType,
Type valueType)
Given a key and value type, returns the map entry type
Map.Entry<keyType,valueType>. |
static Type |
resolveType(Type type,
Type contextType)
Resolves the
type parameter in the context of contextType. |
public static Type box(Type type)
Type, if it is a primitive type.
Otherwise return the input argument.type - the type to boxpublic static Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype)
findGenericParameter(Type, Class, int) with n = 0.type - the typeparameterizedSupertype - the parameterized supertypefindGenericParameter(Type, Class, int)public static Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype, int n)
Example:
type is ArrayList<String>,
parameterizedSuperType is List.class,
and n is 0,
returns Optional.of(String.class).type is Map<String, Integer>,
parameterizedSuperType is Map.class,
and n is 1,
returns Optional.of(Integer.class).type is ArrayList.class (raw),
parameterizedSuperType is List.class,
and n is 0,
returns Optional.empty().type - the subtype of parameterizedSupertypeparameterizedSupertype - the parameterized supertype from which we want the generic parametern - the index in Foo<X, Y, Z, ...>ArrayIndexOutOfBoundsException - if n > the number of type variables the type haspublic static Class<?> getErasedType(Type type)
Example: if type is List<String>, returns
List.class
type - the typepublic static boolean isArray(Type type)
type - a typeType is an Array type.public static Type parameterizeClass(Class<?> clazz, Type... arguments)
clazz with arguments as type arguments.
For example: parameterizedClass(Map.class, Integer.class, String.class)
returns the type Map<Integer, String>.
clazz - Type class of the type to createarguments - Type arguments for the variables of clazz, or null if these are not
known.ParameterizedType, or simply clazz if arguments is
null or empty.public static Type resolveMapEntryType(Type mapType)
Map<K,V>, returns the corresponding map entry type Map.Entry<K,V>.mapType - the map subtypepublic static Type resolveMapEntryType(Type keyType, Type valueType)
Map.Entry<keyType,valueType>.keyType - the key typevalueType - the value typepublic static Type resolveType(Type type, Type contextType)
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.classtype - the type to be resolved in the scope of contextTypecontextType - the context type in which type is interpreted to resolve the type.Copyright © 2019. All rights reserved.