Class TypeToken<T>
- java.lang.Object
-
- com.google.common.reflect.TypeToken<T>
-
- All Implemented Interfaces:
java.io.Serializable
@Beta public abstract class TypeToken<T> extends java.lang.Object implements java.io.Serializable
ATypewith generics.Operations that are otherwise only available in
Classare implemented to supportType, for exampleisAssignableFrom(com.google.common.reflect.TypeToken<?>),isArray()andgetComponentType(). It also provides additional utilities such asgetTypes()andresolveType(java.lang.reflect.Type)etc.There are three ways to get a
TypeTokeninstance:- 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
TypeTokenis serializable when no type variable is contained in the type.Note to Guice users: TypeToken is similar to Guice's
TypeLiteralclass except that it is serializable and offers numerous additional utility methods.- Since:
- 12.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classTypeToken.TypeSetThe set of interfaces and classes thatTis or is a subtype of.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Invokable<T,T>constructor(java.lang.reflect.Constructor<?> constructor)booleanequals(java.lang.Object o)Returns true ifois anotherTypeTokenthat represents the sameType.TypeToken<?>getComponentType()Returns the array component type if this type represents an array (int[],T[],<? extends Map<String, Integer>[]>etc.), or elsenullis returned.java.lang.Class<? super T>getRawType()Returns the raw type ofT.TypeToken<? extends T>getSubtype(java.lang.Class<?> subclass)Returns subtype ofthiswithsubclassas the raw class.TypeToken<? super T>getSupertype(java.lang.Class<? super T> superclass)Returns the generic form ofsuperclass.java.lang.reflect.TypegetType()Returns the represented type.TypeToken.TypeSetgetTypes()Returns the set of interfaces and classes that this type is or is a subtype of.inthashCode()booleanisArray()Returns true if this type is known to be an array type, such asint[],T[],<? extends Map<String, Integer>[]>etc.booleanisAssignableFrom(TypeToken<?> type)Returns true if this type is assignable from the giventype.booleanisAssignableFrom(java.lang.reflect.Type type)Check if this type is assignable from the giventype.booleanisPrimitive()Returns true if this type is one of the nine primitive types (includingvoid).Invokable<T,java.lang.Object>method(java.lang.reflect.Method method)static <T> TypeToken<T>of(java.lang.Class<T> type)Returns an instance of type token that wrapstype.static TypeToken<?>of(java.lang.reflect.Type type)Returns an instance of type token that wrapstype.TypeToken<?>resolveType(java.lang.reflect.Type type)Resolves the giventypeagainst the type context represented by this type.java.lang.StringtoString()TypeToken<T>unwrap()Returns the corresponding primitive type if this is a wrapper type; otherwise returnsthisitself.<X> TypeToken<T>where(TypeParameter<X> typeParam, TypeToken<X> typeArg)Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg.<X> TypeToken<T>where(TypeParameter<X> typeParam, java.lang.Class<X> typeArg)Returns a newTypeTokenwhere type variables represented bytypeParamare substituted bytypeArg.TypeToken<T>wrap()Returns the corresponding wrapper type if this is a primitive type; otherwise returnsthisitself.
-
-
-
Method Detail
-
of
public static <T> TypeToken<T> of(java.lang.Class<T> type)
Returns an instance of type token that wrapstype.
-
of
public static TypeToken<?> of(java.lang.reflect.Type type)
Returns an instance of type token that wrapstype.
-
getRawType
public final java.lang.Class<? super T> 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
public final java.lang.reflect.Type getType()
Returns the represented type.
-
where
public final <X> TypeToken<T> where(TypeParameter<X> typeParam, TypeToken<X> typeArg)
Returns a new
TypeTokenwhere 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
public final <X> TypeToken<T> where(TypeParameter<X> typeParam, java.lang.Class<X> typeArg)
Returns a new
TypeTokenwhere 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
public final TypeToken<?> resolveType(java.lang.reflect.Type type)
Resolves the given
typeagainst the type context represented by this type. For example:new TypeToken<List<String>>() {}.resolveType( List.class.getMethod("get", int.class).getGenericReturnType()) => String.class
-
getTypes
public final TypeToken.TypeSet 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
public final TypeToken<? super T> getSupertype(java.lang.Class<? super T> superclass)
Returns the generic form ofsuperclass. For example, if this isArrayList<String>,Iterable<String>is returned given the inputIterable.class.
-
getSubtype
public final TypeToken<? extends T> getSubtype(java.lang.Class<?> subclass)
Returns subtype ofthiswithsubclassas the raw class. For example, if this isIterable<String>andsubclassisList,List<String>is returned.
-
isAssignableFrom
public final boolean isAssignableFrom(TypeToken<?> type)
Returns true if this type is assignable from the giventype.
-
isAssignableFrom
public final boolean isAssignableFrom(java.lang.reflect.Type type)
Check if this type is assignable from the giventype.
-
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
public final TypeToken<T> wrap()
Returns the corresponding wrapper type if this is a primitive type; otherwise returnsthisitself. Idempotent.- Since:
- 15.0
-
unwrap
public final TypeToken<T> unwrap()
Returns the corresponding primitive type if this is a wrapper type; otherwise returnsthisitself. Idempotent.- Since:
- 15.0
-
getComponentType
@Nullable public final TypeToken<?> getComponentType()
Returns the array component type if this type represents an array (int[],T[],<? extends Map<String, Integer>[]>etc.), or elsenullis returned.
-
method
public final Invokable<T,java.lang.Object> method(java.lang.reflect.Method method)
- Since:
- 14.0
-
constructor
public final Invokable<T,T> constructor(java.lang.reflect.Constructor<?> constructor)
- Since:
- 14.0
-
equals
public boolean equals(@Nullable java.lang.Object o)Returns true ifois anotherTypeTokenthat represents the sameType.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-