Class TypeResolver
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.
- Since:
- 15.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static class
A TypeTable maintains mapping fromTypeVariable
to types.(package private) static final class
Wraps aroundTypeVariable<?>
to ensure that any two type variables are equal as long as they are declared by the sameGenericDeclaration
and have the same name, even if their bounds differ.private static class
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static TypeResolver
covariantly
(Type contextType) Returns a resolver that resolves types "covariantly".private static <T> T
expectArgument
(Class<T> type, Object arg) (package private) static TypeResolver
invariantly
(Type contextType) Returns a resolver that resolves types "invariantly".private static void
populateTypeMappings
(Map<TypeResolver.TypeVariableKey, Type> mappings, Type from, Type to) private Type
private ParameterizedType
resolveType
(Type type) Resolves all type variables intype
and all downstream types and returns a corresponding type with type variables resolved.private Type[]
resolveTypes
(Type[] types) (package private) Type[]
resolveTypesInPlace
(Type[] types) private WildcardType
Returns a newTypeResolver
with type variables informal
mapping to types inactual
.(package private) TypeResolver
where
(Map<TypeResolver.TypeVariableKey, ? extends Type> mappings) Returns a newTypeResolver
withvariable
mapping totype
.
-
Field Details
-
typeTable
-
-
Constructor Details
-
TypeResolver
public TypeResolver() -
TypeResolver
-
-
Method Details
-
covariantly
Returns a resolver that resolves types "covariantly".For example, when resolving
List<T>
in the context ofArrayList<?>
,<T>
is covariantly resolved to<?>
such that return type ofList::get
is<?>
. -
invariantly
Returns a resolver that resolves types "invariantly".For example, when resolving
List<T>
in the context ofArrayList<?>
,<T>
cannot be invariantly resolved to<?>
because otherwise the parameter type ofList::set
will be<?>
and it'll falsely say any object can be passed intoArrayList<?>::set
.Instead,
<?>
will be resolved to a capture in the form of a type variable<capture-of-? extends Object>
, effectively preventingset
from accepting any type. -
where
Returns a newTypeResolver
with type variables informal
mapping to types inactual
.For example, if
formal
is aTypeVariable T
, andactual
isString.class
, thennew TypeResolver().where(formal, actual)
will resolveParameterizedType List<T>
toList<String>
, and resolveMap<T, Something>
toMap<String, Something>
etc. Similarly,formal
andactual
can beMap<K, V>
andMap<String, Integer>
respectively, or they can beE[]
andString[]
respectively, or even any arbitrary combination thereof.- Parameters:
formal
- The type whose type variables or itself is mapped to other type(s). It's almost always a bug ifformal
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 currentTypeResolver
instance.
-
where
Returns a newTypeResolver
withvariable
mapping totype
. -
populateTypeMappings
private static void populateTypeMappings(Map<TypeResolver.TypeVariableKey, Type> mappings, Type from, Type to) -
resolveType
Resolves all type variables intype
and all downstream types and returns a corresponding type with type variables resolved. -
resolveTypesInPlace
-
resolveTypes
-
resolveWildcardType
-
resolveGenericArrayType
-
resolveParameterizedType
-
expectArgument
-