public final class TypeResolver
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
TypeResolver.Unknown
An unknown type.
|
Modifier and Type | Method and Description |
---|---|
static void |
disableCache()
Disables the internal caching of resolved TypeVariables.
|
static void |
enableCache()
Enables the internal caching of resolved TypeVariables.
|
static <T,S extends T> |
reify(java.lang.Class<T> type,
java.lang.Class<S> context)
Traverses a generic type and replaces all type variables and wildcard types with concrete types (if possible),
by using the type information from given
context . |
static java.lang.reflect.Type |
reify(java.lang.reflect.Type type)
Traverses a generic type and replaces all type variables and wildcard types with concrete types (if possible).
|
static java.lang.reflect.Type |
reify(java.lang.reflect.Type type,
java.lang.Class<?> context)
Traverses a generic type and replaces all type variables and wildcard types with concrete types (if possible),
by using the type information from given
context . |
static java.lang.reflect.Type |
resolveBound(java.lang.reflect.TypeVariable<?> typeVariable)
Resolves the first bound for the
typeVariable , returning Unknown.class if none can be resolved. |
static java.lang.reflect.Type |
resolveGenericType(java.lang.Class<?> type,
java.lang.reflect.Type subType)
Returns the generic
type using type variable information from the subType else null if the
generic type cannot be resolved. |
static <T,S extends T> |
resolveRawArgument(java.lang.Class<T> type,
java.lang.Class<S> subType)
Returns the raw class representing the argument for the
type using type variable information from the
subType . |
static java.lang.Class<?> |
resolveRawArgument(java.lang.reflect.Type genericType,
java.lang.Class<?> subType)
Returns the raw class representing the argument for the
genericType using type variable information from
the subType . |
static <T,S extends T> |
resolveRawArguments(java.lang.Class<T> type,
java.lang.Class<S> subType)
Returns an array of raw classes representing arguments for the
type using type variable information from
the subType . |
static java.lang.Class<?>[] |
resolveRawArguments(java.lang.reflect.Type genericType,
java.lang.Class<?> subType)
Returns an array of raw classes representing arguments for the
genericType using type variable information
from the subType . |
static java.lang.Class<?> |
resolveRawClass(java.lang.reflect.Type genericType,
java.lang.Class<?> subType)
Resolves the raw class for the
genericType , using the type variable information from the subType
else TypeResolver.Unknown if the raw class cannot be resolved. |
public static void enableCache()
public static void disableCache()
public static <T,S extends T> java.lang.Class<?> resolveRawArgument(java.lang.Class<T> type, java.lang.Class<S> subType)
type
using type variable information from the
subType
. If no arguments can be resolved then Unknown.class
is returned.type
- to resolve argument forsubType
- to extract type variable information fromtype
else TypeResolver.Unknown
.class if no type arguments are declaredjava.lang.IllegalArgumentException
- if more or less than one argument is resolved for the type
public static java.lang.Class<?> resolveRawArgument(java.lang.reflect.Type genericType, java.lang.Class<?> subType)
genericType
using type variable information from
the subType
. If genericType
is an instance of class, then genericType
is returned. If no
arguments can be resolved then Unknown.class
is returned.genericType
- to resolve argument forsubType
- to extract type variable information fromgenericType
else TypeResolver.Unknown
.class if no type arguments are declaredjava.lang.IllegalArgumentException
- if more or less than one argument is resolved for the genericType
public static <T,S extends T> java.lang.Class<?>[] resolveRawArguments(java.lang.Class<T> type, java.lang.Class<S> subType)
type
using type variable information from
the subType
. Arguments for type
that cannot be resolved are returned as Unknown.class
. If
no arguments can be resolved then null
is returned.type
- to resolve arguments forsubType
- to extract type variable information fromtype
else null
if no type arguments are
declaredpublic static <T,S extends T> java.lang.reflect.Type reify(java.lang.Class<T> type, java.lang.Class<S> context)
context
.
A convenience method which largely works the same as reify(Type, Class)
, but first resolves the
generic type of type
.type
- the class whose generic type to traversecontext
- the class that serves as starting point to resolve replacements of type variablestype
, except that type variables and wildcard types
have been replaced with concrete typesjava.lang.UnsupportedOperationException
- if type
(or a type that it references) is not an instance of one of
the following types: Class
, TypeVariable
, WildcardType
, ParameterizedType
,
GenericArrayType
.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a WildcardType
that
does not have exactly one upper bound, or does not have no lower bounds.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a GenericArrayType
whose generic component type cannot be reified to an instance of Class
.public static java.lang.reflect.Type reify(java.lang.reflect.Type type, java.lang.Class<?> context)
context
.
Generic types used as input to this method are commonly obtained using reflection, e.g. via
Field.getGenericType()
, Method.getGenericReturnType()
, Method.getGenericParameterTypes()
.
Example:
Reifying the generic return type of the methodclass A<T> { public T something; public Optional<List<T>> compute() { ... } } class B extends A<Number> { public <? extends Collection<List<?>>> collect() { ... } }
compute
with B.class
as context
will
yield the parameterized type Optional<List<Number>>
. Note that not the raw type (Optional
is returned, but the input type is reified recursively.
Reifying the generic type of the field something
with B.class
as context
will yield
Number.class
.
Note that type variables with no explicit upper bound are reified to Object
, and Unknown.class
is
never returned.type
- the generic type to traversecontext
- the class that serves as starting point to resolve replacements of type variablestype
, except that type variables and wildcard types
have been replaced with concrete typesjava.lang.UnsupportedOperationException
- if type
(or a type that it references) is not an instance of one of
the following types: Class
, TypeVariable
, WildcardType
, ParameterizedType
,
GenericArrayType
.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a WildcardType
that
does not have exactly one upper bound, or does not have no lower bounds.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a GenericArrayType
whose generic component type cannot be reified to an instance of Class
.public static java.lang.reflect.Type reify(java.lang.reflect.Type type)
reify(Type, Class)
, for when no context is needed/available.
Generic types used as input to this method are commonly obtained using reflection, e.g. via
Field.getGenericType()
, Method.getGenericReturnType()
, Method.getGenericParameterTypes()
.
Example:
Reifying the generic return type of the methodclass X { public List<? extends Collection<List<? extends Number>>> collectList() { ... } public Set<?> collectSet() { ... } }
collectList
will yield the parameterized type
List<Collection<List<Number>>>
.
Reifying the generic return type of the method collectSet
will yield the parameterized type
Set<Object>
, since there is no explicit upper bound for the wildcard type given.type
- the generic type to traversetype
, except that type variables and wildcard types
have been replaced with concrete typesjava.lang.UnsupportedOperationException
- if type
(or a type that it references) is not an instance of one of
the following types: Class
, TypeVariable
, WildcardType
, ParameterizedType
,
GenericArrayType
.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a WildcardType
that
does not have exactly one upper bound, or does not have no lower bounds.java.lang.UnsupportedOperationException
- if type
(or a type that it references) is a GenericArrayType
whose generic component type cannot be reified to an instance of Class
.public static java.lang.Class<?>[] resolveRawArguments(java.lang.reflect.Type genericType, java.lang.Class<?> subType)
genericType
using type variable information
from the subType
. Arguments for genericType
that cannot be resolved are returned as
Unknown.class
. If no arguments can be resolved then null
is returned.genericType
- to resolve arguments forsubType
- to extract type variable information fromgenericType
else null
if no type
arguments are declaredpublic static java.lang.reflect.Type resolveGenericType(java.lang.Class<?> type, java.lang.reflect.Type subType)
type
using type variable information from the subType
else null
if the
generic type cannot be resolved.type
- to resolve generic type forsubType
- to extract type variable information fromtype
else null
if it cannot be resolvedpublic static java.lang.Class<?> resolveRawClass(java.lang.reflect.Type genericType, java.lang.Class<?> subType)
genericType
, using the type variable information from the subType
else TypeResolver.Unknown
if the raw class cannot be resolved.genericType
- to resolve raw class forsubType
- to extract type variable information fromgenericType
else TypeResolver.Unknown
if it cannot be resolvedpublic static java.lang.reflect.Type resolveBound(java.lang.reflect.TypeVariable<?> typeVariable)
typeVariable
, returning Unknown.class
if none can be resolved.Copyright © 2020. All Rights Reserved.