Class TypeResolver
- java.lang.Object
-
- com.google.common.reflect.TypeResolver
-
public final class TypeResolver extends java.lang.ObjectAn object of this class encapsulates type mappings from type variables. Mappings are established withwhere(java.lang.reflect.Type, java.lang.reflect.Type)and types are resolved usingresolveType(java.lang.reflect.Type).Note that usually type mappings are already implied by the static type hierarchy (for example, the
Etype variable declared by classListnaturally maps toStringin the context ofclass MyStringList implements List<String>. In such case, prefer to useTypeToken.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 Classes Modifier and Type Class Description private static classTypeResolver.TypeMappingIntrospectorprivate static classTypeResolver.TypeTableA TypeTable maintains mapping fromTypeVariableto types.(package private) static classTypeResolver.TypeVariableKeyWraps aroundTypeVariable<?>to ensure that any two type variables are equal as long as they are declared by the sameGenericDeclarationand have the same name, even if their bounds differ.private static classTypeResolver.WildcardCapturer
-
Field Summary
Fields Modifier and Type Field Description private TypeResolver.TypeTabletypeTable
-
Constructor Summary
Constructors Modifier Constructor Description TypeResolver()privateTypeResolver(TypeResolver.TypeTable typeTable)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static TypeResolvercovariantly(java.lang.reflect.Type contextType)Returns a resolver that resolves types "covariantly".private static <T> TexpectArgument(java.lang.Class<T> type, java.lang.Object arg)(package private) static TypeResolverinvariantly(java.lang.reflect.Type contextType)Returns a resolver that resolves types "invariantly".private static voidpopulateTypeMappings(java.util.Map<TypeResolver.TypeVariableKey,java.lang.reflect.Type> mappings, java.lang.reflect.Type from, java.lang.reflect.Type to)private java.lang.reflect.TyperesolveGenericArrayType(java.lang.reflect.GenericArrayType type)private java.lang.reflect.ParameterizedTyperesolveParameterizedType(java.lang.reflect.ParameterizedType type)java.lang.reflect.TyperesolveType(java.lang.reflect.Type type)Resolves all type variables intypeand all downstream types and returns a corresponding type with type variables resolved.private java.lang.reflect.Type[]resolveTypes(java.lang.reflect.Type[] types)(package private) java.lang.reflect.Type[]resolveTypesInPlace(java.lang.reflect.Type[] types)private java.lang.reflect.WildcardTyperesolveWildcardType(java.lang.reflect.WildcardType type)TypeResolverwhere(java.lang.reflect.Type formal, java.lang.reflect.Type actual)Returns a newTypeResolverwith type variables informalmapping to types inactual.(package private) TypeResolverwhere(java.util.Map<TypeResolver.TypeVariableKey,? extends java.lang.reflect.Type> mappings)Returns a newTypeResolverwithvariablemapping totype.
-
-
-
Field Detail
-
typeTable
private final TypeResolver.TypeTable typeTable
-
-
Constructor Detail
-
TypeResolver
public TypeResolver()
-
TypeResolver
private TypeResolver(TypeResolver.TypeTable typeTable)
-
-
Method Detail
-
covariantly
static TypeResolver covariantly(java.lang.reflect.Type contextType)
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::getis<?>.
-
invariantly
static TypeResolver invariantly(java.lang.reflect.Type contextType)
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::setwill 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 preventingsetfrom accepting any type.
-
where
public TypeResolver where(java.lang.reflect.Type formal, java.lang.reflect.Type actual)
Returns a newTypeResolverwith type variables informalmapping to types inactual.For example, if
formalis aTypeVariable T, andactualisString.class, thennew TypeResolver().where(formal, actual)will resolveParameterizedType List<T>toList<String>, and resolveMap<T, Something>toMap<String, Something>etc. Similarly,formalandactualcan 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 ifformalisn'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 currentTypeResolverinstance.
-
where
TypeResolver where(java.util.Map<TypeResolver.TypeVariableKey,? extends java.lang.reflect.Type> mappings)
Returns a newTypeResolverwithvariablemapping totype.
-
populateTypeMappings
private static void populateTypeMappings(java.util.Map<TypeResolver.TypeVariableKey,java.lang.reflect.Type> mappings, java.lang.reflect.Type from, java.lang.reflect.Type to)
-
resolveType
public java.lang.reflect.Type resolveType(java.lang.reflect.Type type)
Resolves all type variables intypeand all downstream types and returns a corresponding type with type variables resolved.
-
resolveTypesInPlace
java.lang.reflect.Type[] resolveTypesInPlace(java.lang.reflect.Type[] types)
-
resolveTypes
private java.lang.reflect.Type[] resolveTypes(java.lang.reflect.Type[] types)
-
resolveWildcardType
private java.lang.reflect.WildcardType resolveWildcardType(java.lang.reflect.WildcardType type)
-
resolveGenericArrayType
private java.lang.reflect.Type resolveGenericArrayType(java.lang.reflect.GenericArrayType type)
-
resolveParameterizedType
private java.lang.reflect.ParameterizedType resolveParameterizedType(java.lang.reflect.ParameterizedType type)
-
expectArgument
private static <T> T expectArgument(java.lang.Class<T> type, java.lang.Object arg)
-
-