Top Description Inners Fields Constructors Methods
com.google.common.reflect

public abstract Class TypeToken<T>

extends TypeCapture<T>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
com.google.common.reflect.TypeToken.SimpleTypeToken
Annotations
@SuppressWarnings:serial
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, java.util.Objects.requireNonNull

A Type with generics.

Operations that are otherwise only available in Class are implemented to support Type, for example isSubtypeOf, isArray and getComponentType. It also provides additional utilities such as getTypes, resolveType, etc.

There are three ways to get a TypeToken instance:

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.

Authors
Bob Lee, Sven Mawson, Ben Yu
Since
12.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
private class
private static class
private abstract static class
TypeToken.TypeCollector<
The type "kind". Either a TypeToken, or Class.
K
>

Collects parent types from a subtype.

private static enum
public class
TypeToken.TypeSet

The set of interfaces and classes that T is or is a subtype of.

Field Summary

Modifier and TypeField and Description
private transient TypeResolver
covariantTypeResolver

Resolver for resolving covariant types with runtimeType as context.

private transient TypeResolver
invariantTypeResolver

Resolver for resolving parameter and field types with runtimeType as context.

private final Type
private static final long

Constructor Summary

AccessConstructor and Description
protected
TypeToken()

Constructs a new type token of T.

protected
TypeToken(Class<?> declaringClass)

Constructs a new type token of T while resolving free type variables in the context of declaringClass.

private

Method Summary

Modifier and TypeMethod and Description
private static TypeToken.Bounds
any(Type[] bounds)

private TypeToken<? super T>
private ImmutableList<TypeToken<? super T>>
private static Type
canonicalizeTypeArg(TypeVariable<?> declaration, Type typeArg)

In reflection, Foo<?

private static ParameterizedType
private static Type
private static WildcardType
public final Invokable<T, T>
constructor(Constructor<?> constructor)

Returns the Invokable for constructor, which must be a member of T.

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Returns true if o is another TypeToken that represents the same Type.
private static TypeToken.Bounds
every(Type[] bounds)

private TypeToken<? extends T>
getArraySubtype(Class<?> subclass)

private TypeToken<? super T>
getArraySupertype(Class<? super T> supertype)

public final TypeToken<?>
getComponentType()

Returns the array component type if this type represents an array (int[], T[], <?

private TypeResolver
pack-priv final ImmutableList<TypeToken<? super T>>
getGenericInterfaces()

Returns the generic interfaces that this type directly implements.

pack-priv final TypeToken<? super T>
getGenericSuperclass()

Returns the generic superclass of this type or null if the type represents Object or an interface.

private TypeResolver
private Type
getOwnerTypeIfPresent()

Returns the owner type of a ParameterizedType or enclosing class of a Class, or null otherwise.

public final Class<? super T>
getRawType()

Returns the raw type of T.

private ImmutableSet<Class<? super T>>
public final TypeToken<? extends T>
getSubtype(Class<?> subclass)

Returns subtype of this with subclass as the raw class.

private TypeToken<? extends T>
getSubtypeFromLowerBounds(Class<?> subclass, Type[] lowerBounds)

public final TypeToken<? super T>
getSupertype(Class<? super T> superclass)

Returns the generic form of superclass.

private TypeToken<? super T>
getSupertypeFromUpperBounds(Class<? super T> supertype, Type[] upperBounds)

public final Type
getType()

Returns the represented type.

public final TypeToken<T>.TypeSet
getTypes()

Returns the set of interfaces and classes that this type is or is a subtype of.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
private boolean
is(Type
is Foo<formalType> a supertype of Foo<T>?
formalType
,
TypeVariable<?>
The type variable in the context of a parameterized type. Used to infer type bound when formalType is a wildcard with implicit upper bound.
declaration
)

A.is(B) is defined as Foo<A>.isSubtypeOf(Foo<B>).

public final boolean
isArray()

Returns true if this type is known to be an array type, such as int[], T[], <?

private boolean
public final boolean
isPrimitive()

Returns true if this type is one of the nine primitive types (including void).

public final boolean
isSubtypeOf(TypeToken<?> type)

Returns true if this type is a subtype of the given type.

public final boolean
isSubtypeOf(Type supertype)

Returns true if this type is a subtype of the given type.

private boolean
private boolean
public final boolean
isSupertypeOf(TypeToken<?> type)

Returns true if this type is a supertype of the given type.

public final boolean
isSupertypeOf(Type type)

Returns true if this type is a supertype of the given type.

private boolean
private boolean
public final Invokable<T, Object>
method(Method method)

Returns the Invokable for method, which must be a member of T.

private static Type
newArrayClassOrGenericArrayType(Type componentType)

Creates an array class if componentType is a class, or else, a GenericArrayType.

public static <T> TypeToken<T>
of(Class<T> type)

Returns an instance of type token that wraps type.

public static TypeToken<?>
of(Type type)

Returns an instance of type token that wraps type.

pack-priv final TypeToken<T>
rejectTypeVariables()

Ensures that this type token doesn't contain type variables, which can cause unchecked type errors for callers like TypeToInstanceMap.

private TypeToken<?>
public final TypeToken<?>
resolveType(Type type)

Resolves the given type against the type context represented by this type.

private Type
private boolean
pack-priv static <T> TypeToken<? extends T>
toGenericType(Class<T> cls)

Returns the type token representing the generic type declaration of cls.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public final TypeToken<T>
unwrap()

Returns the corresponding primitive type if this is a wrapper type; otherwise returns this itself.

public final <
The parameter type
X
>
TypeToken<T>
where(TypeParameter<X>
the parameter type variable
typeParam
,
TypeToken<X>
the actual type to substitute
typeArg
)

Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg.

public final <
The parameter type
X
>
TypeToken<T>
where(TypeParameter<X>
the parameter type variable
typeParam
,
Class<X>
the actual type to substitute
typeArg
)

Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg.

public final TypeToken<T>
wrap()

Returns the corresponding wrapper type if this is a primitive type; otherwise returns this itself.

protected Object
writeReplace()

Implemented to support serialization of subclasses.

Inherited from com.google.common.reflect.TypeCapture:
capture