Class TypeResolver
- java.lang.Object
-
- com.google.common.reflect.TypeResolver
-
@Beta public final class TypeResolver extends Object
An 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
- Author:
- Ben Yu
-
-
Constructor Summary
Constructors Constructor Description TypeResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TyperesolveType(Type type)Resolves all type variables intypeand all downstream types and returns a corresponding type with type variables resolved.TypeResolverwhere(Type formal, Type actual)Returns a newTypeResolverwith type variables informalmapping to types inactual.
-
-
-
Constructor Detail
-
TypeResolver
public TypeResolver()
-
-
Method Detail
-
where
public TypeResolver where(Type formal, 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.
-
resolveType
public Type resolveType(Type type)
Resolves all type variables intypeand all downstream types and returns a corresponding type with type variables resolved.
-
-