@Beta public final class TypeResolver extends java.lang.Object
where(java.lang.reflect.Type, java.lang.reflect.Type)
and types are resolved using resolveType(java.lang.reflect.Type)
.
Note that usually type mappings are already implied by the static type hierarchy (for example,
the E
type variable declared by class List
naturally maps to String
in
the context of class MyStringList implements List<String>
. In such case, prefer to use
TypeToken.resolveType(java.lang.reflect.Type)
since it's simpler and more type safe. This class should only be
used when the type mapping isn't implied by the static type hierarchy, but provided through other
means such as an annotation or external configuration file.
Modifier and Type | Class and Description |
---|---|
private static class |
TypeResolver.TypeMappingIntrospector |
private static class |
TypeResolver.TypeTable
A TypeTable maintains mapping from
TypeVariable to types. |
(package private) static class |
TypeResolver.TypeVariableKey
Wraps around
TypeVariable<?> to ensure that any two type variables are equal as long as
they are declared by the same GenericDeclaration and have the same
name, even if their bounds differ. |
private static class |
TypeResolver.WildcardCapturer |
Modifier and Type | Field and Description |
---|---|
private TypeResolver.TypeTable |
typeTable |
Modifier | Constructor and Description |
---|---|
|
TypeResolver() |
private |
TypeResolver(TypeResolver.TypeTable typeTable) |
Modifier and Type | Method and Description |
---|---|
(package private) static TypeResolver |
accordingTo(java.lang.reflect.Type type) |
private static <T> T |
expectArgument(java.lang.Class<T> type,
java.lang.Object arg) |
private static void |
populateTypeMappings(java.util.Map<TypeResolver.TypeVariableKey,java.lang.reflect.Type> mappings,
java.lang.reflect.Type from,
java.lang.reflect.Type to) |
private java.lang.reflect.Type |
resolveGenericArrayType(java.lang.reflect.GenericArrayType type) |
private java.lang.reflect.ParameterizedType |
resolveParameterizedType(java.lang.reflect.ParameterizedType type) |
java.lang.reflect.Type |
resolveType(java.lang.reflect.Type type)
Resolves all type variables in
type and all downstream types and
returns a corresponding type with type variables resolved. |
private java.lang.reflect.Type[] |
resolveTypes(java.lang.reflect.Type[] types) |
private java.lang.reflect.WildcardType |
resolveWildcardType(java.lang.reflect.WildcardType type) |
(package private) TypeResolver |
where(java.util.Map<TypeResolver.TypeVariableKey,? extends java.lang.reflect.Type> mappings)
Returns a new
TypeResolver with variable mapping to type . |
TypeResolver |
where(java.lang.reflect.Type formal,
java.lang.reflect.Type actual)
Returns a new
TypeResolver with type variables in formal mapping to types in
actual . |
private final TypeResolver.TypeTable typeTable
public TypeResolver()
private TypeResolver(TypeResolver.TypeTable typeTable)
static TypeResolver accordingTo(java.lang.reflect.Type type)
public TypeResolver where(java.lang.reflect.Type formal, java.lang.reflect.Type actual)
TypeResolver
with type variables in formal
mapping to types in
actual
.
For example, if formal
is a TypeVariable T
, and actual
is String.class
, then new TypeResolver().where(formal, actual)
will resolve ParameterizedType List<T>
to List<String>
, and resolve
Map<T, Something>
to Map<String, Something>
etc. Similarly, formal
and
actual
can be Map<K, V>
and Map<String, Integer>
respectively, or they
can be E[]
and String[]
respectively, or even any arbitrary combination
thereof.
formal
- The type whose type variables or itself is mapped to other type(s). It's almost
always a bug if formal
isn't a type variable and contains no type variable. Make
sure you are passing the two parameters in the right order.actual
- The type that the formal type variable(s) are mapped to. It can be or contain yet
other type variables, in which case these type variables will be further resolved if
corresponding mappings exist in the current TypeResolver
instance.TypeResolver where(java.util.Map<TypeResolver.TypeVariableKey,? extends java.lang.reflect.Type> mappings)
TypeResolver
with variable
mapping to type
.private static void populateTypeMappings(java.util.Map<TypeResolver.TypeVariableKey,java.lang.reflect.Type> mappings, java.lang.reflect.Type from, java.lang.reflect.Type to)
public java.lang.reflect.Type resolveType(java.lang.reflect.Type type)
type
and all downstream types and
returns a corresponding type with type variables resolved.private java.lang.reflect.Type[] resolveTypes(java.lang.reflect.Type[] types)
private java.lang.reflect.WildcardType resolveWildcardType(java.lang.reflect.WildcardType type)
private java.lang.reflect.Type resolveGenericArrayType(java.lang.reflect.GenericArrayType type)
private java.lang.reflect.ParameterizedType resolveParameterizedType(java.lang.reflect.ParameterizedType type)
private static <T> T expectArgument(java.lang.Class<T> type, java.lang.Object arg)