Class TypeToken<T>
- All Implemented Interfaces:
Serializable
Type with generics.
Operations that are otherwise only available in Class are implemented to support
Type, for example isSubtypeOf(com.google.common.reflect.TypeToken<?>), isArray() and getComponentType().
It also provides additional utilities such as getTypes(), resolveType(java.lang.reflect.Type), etc.
There are three ways to get a TypeToken instance:
- Wrap a
Typeobtained via reflection. For example:TypeToken.of(method.getGenericReturnType()). - Capture a generic type with a (usually anonymous) subclass. For example:
new TypeToken<List<String>>() {}Note that it's critical that the actual type argument is carried by a subclass. The following code is wrong because it only captures the
<T>type variable of thelistType()method signature; while<String>is lost in erasure:class Util { static <T> TypeToken<List<T>> listType() { return new TypeToken<List<T>>() {}; } } TypeToken<List<String>> stringListType = Util.<String>listType(); - Capture a generic type with a (usually anonymous) subclass and resolve it against a context
class that knows what the type parameters are. For example:
abstract class IKnowMyType<T> { TypeToken<T> type = new TypeToken<T>(getClass()) {}; } new IKnowMyType<String>() {}.type => String
TypeToken is serializable when no type variable is contained in the type.
Note to Guice users: TypeToken is similar to Guice's TypeLiteral class except
that it is serializable and offers numerous additional utility methods.
- Since:
- 12.0
- Author:
- Bob Lee, Sven Mawson, Ben Yu
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThe set of interfaces and classes thatTis or is a subtype of. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconstructor(Constructor<?> constructor) booleanReturns the array component type if this type represents an array (int[],T[],<? extends Map<String, Integer>[]>etc.), or elsenullis returned.Returns the raw type ofT.getSubtype(Class<?> subclass) Returns subtype ofthiswithsubclassas the raw class.getSupertype(Class<? super T> superclass) Returns the generic form ofsuperclass.final TypegetType()Returns the represented type.getTypes()Returns the set of interfaces and classes that this type is or is a subtype of.inthashCode()final booleanisArray()Returns true if this type is known to be an array type, such asint[],T[],<? extends Map<String, Integer>[]>etc.final booleanReturns true if this type is one of the nine primitive types (includingvoid).final booleanisSubtypeOf(TypeToken<?> type) Returns true if this type is a subtype of the giventype.final booleanisSubtypeOf(Type supertype) Returns true if this type is a subtype of the giventype.final booleanisSupertypeOf(TypeToken<?> type) Returns true if this type is a supertype of the giventype.final booleanisSupertypeOf(Type type) Returns true if this type is a supertype of the giventype.static <T> TypeToken<T> Returns an instance of type token that wrapstype.static TypeToken<?> Returns an instance of type token that wrapstype.final TypeToken<?> resolveType(Type type) Resolves the giventypeagainst the type context represented by this type.toString()unwrap()Returns the corresponding primitive type if this is a wrapper type; otherwise returnsthisitself.where(TypeParameter<X> typeParam, TypeToken<X> typeArg) Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg.where(TypeParameter<X> typeParam, Class<X> typeArg) Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg.wrap()Returns the corresponding wrapper type if this is a primitive type; otherwise returnsthisitself.protected ObjectImplemented to support serialization of subclasses.
-
Constructor Details
-
TypeToken
protected TypeToken()Constructs a new type token ofT.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
For example:
TypeToken<List<String>> t = new TypeToken<List<String>>() {}; -
TypeToken
Constructs a new type token ofTwhile resolving free type variables in the context ofdeclaringClass.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
For example:
abstract class IKnowMyType<T> { TypeToken<T> getMyType() { return new TypeToken<T>(getClass()) {}; } } new IKnowMyType<String>() {}.getMyType() => String
-
-
Method Details
-
of
-
of
-
getRawType
Returns the raw type ofT. Formally speaking, ifTis returned byMethod.getGenericReturnType(), the raw type is what's returned byMethod.getReturnType()of the same method object. Specifically:- If
Tis aClassitself,Titself is returned. - If
Tis aParameterizedType, the raw type of the parameterized type is returned. - If
Tis aGenericArrayType, the returned type is the corresponding array class. For example:List<Integer>[] => List[]. - If
Tis a type variable or a wildcard type, the raw type of the first upper bound is returned. For example:<X extends Foo> => Foo.
- If
-
getType
Returns the represented type. -
where
Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg. For example, it can be used to constructMap<K, V>for anyKandVtype:static <K, V> TypeToken<Map<K, V>> mapOf( TypeToken<K> keyType, TypeToken<V> valueType) { return new TypeToken<Map<K, V>>() {} .where(new TypeParameter<K>() {}, keyType) .where(new TypeParameter<V>() {}, valueType); }- Type Parameters:
X- The parameter type- Parameters:
typeParam- the parameter type variabletypeArg- the actual type to substitute
-
where
Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg. For example, it can be used to constructMap<K, V>for anyKandVtype:static <K, V> TypeToken<Map<K, V>> mapOf( Class<K> keyType, Class<V> valueType) { return new TypeToken<Map<K, V>>() {} .where(new TypeParameter<K>() {}, keyType) .where(new TypeParameter<V>() {}, valueType); }- Type Parameters:
X- The parameter type- Parameters:
typeParam- the parameter type variabletypeArg- the actual type to substitute
-
resolveType
-
getTypes
Returns the set of interfaces and classes that this type is or is a subtype of. The returned types are parameterized with proper type arguments.Subtypes are always listed before supertypes. But the reverse is not true. A type isn't necessarily a subtype of all the types following. Order between types without subtype relationship is arbitrary and not guaranteed.
If this type is a type variable or wildcard, upper bounds that are themselves type variables aren't included (their super interfaces and superclasses are).
-
getSupertype
-
getSubtype
-
isSupertypeOf
Returns true if this type is a supertype of the giventype. "Supertype" is defined according to the rules for type arguments introduced with Java generics.- Since:
- 19.0
-
isSupertypeOf
Returns true if this type is a supertype of the giventype. "Supertype" is defined according to the rules for type arguments introduced with Java generics.- Since:
- 19.0
-
isSubtypeOf
Returns true if this type is a subtype of the giventype. "Subtype" is defined according to the rules for type arguments introduced with Java generics.- Since:
- 19.0
-
isSubtypeOf
Returns true if this type is a subtype of the giventype. "Subtype" is defined according to the rules for type arguments introduced with Java generics.- Since:
- 19.0
-
isArray
public final boolean isArray()Returns true if this type is known to be an array type, such asint[],T[],<? extends Map<String, Integer>[]>etc. -
isPrimitive
public final boolean isPrimitive()Returns true if this type is one of the nine primitive types (includingvoid).- Since:
- 15.0
-
wrap
-
unwrap
-
getComponentType
-
method
-
constructor
- Since:
- 14.0
-
equals
-
hashCode
-
toString
-
writeReplace
Implemented to support serialization of subclasses.
-