public class GenericTypes extends Object
| Constructor and Description |
|---|
GenericTypes() |
| Modifier and Type | Method and Description |
|---|---|
static Optional<Type> |
findGenericParameter(Type type,
Class<?> parameterizedSupertype)
For the given type which extends parameterizedSupertype, returns the
first generic parameter for 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 |
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 Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype)
Example:
type is ArrayList<String> and
parameterizedSuperType is List.class, returns
Optional.of(String.class).type is ArrayList.class (raw) and
parameterizedSuperType is List.class, returns
Optional.empty().type - the subtype of parameterizedSupertypeparameterizedSupertype - the parameterized supertype from which we want the generic
parameterpublic 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 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 © 2017. All rights reserved.