T. Java doesn't yet provide a way to represent generic types,
so this class does. Forces clients to create a subclass of this class which enables retrieval the
type information even at runtime.
For example, to create a type literal for List<String>, you can create an empty
anonymous class:
TypeToken<List<String>> list = new TypeToken<List<String>>() {};
Capturing a type variable as type argument of an anonymous TypeToken subclass is not
allowed, for example TypeToken<List<T>>. Due to type erasure the runtime type of a type
variable is not available to Gson and therefore it cannot provide the functionality one might
expect. This would give a false sense of type-safety at compile time and could lead to an
unexpected ClassCastException at runtime.
If the type arguments of the parameterized type are only available at runtime, for example
when you want to create a List<E> based on a Class<E> representing the element
type, the method getParameterized(Type, Type. can be used.
| Modifier and Type | Field and Description |
|---|---|
| private final int | |
| private final Class | |
| private final Type |
| Access | Constructor and Description |
|---|---|
| protected | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static IllegalArgumentException | |
| public final boolean | equals(Object o)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public static TypeToken | |
| public static <T> TypeToken | |
| public static TypeToken | |
| public static TypeToken | getParameterized(Type rawType, Type... typeArguments)
Gets a type literal for the parameterized type represented by applying |
| public final Class | |
| public final Type | |
| private Type | getTypeTokenTypeArgument()
Verifies that |
| public final int | |
| public boolean | isAssignableFrom(Class<?> cls)
Deprecated
this implementation may be inconsistent with javac for types with wildcards.
Check if this type is assignable from the given class object. |
| public boolean | isAssignableFrom(Type from)
Deprecated
this implementation may be inconsistent with javac for types with wildcards.
Check if this type is assignable from the given Type. |
| public boolean | isAssignableFrom(TypeToken<?> token)
Deprecated
this implementation may be inconsistent with javac for types with wildcards.
Check if this type is assignable from the given type token. |
| private static boolean | isAssignableFrom(Type from, GenericArrayType to)
Private helper function that performs some assignability checks for the provided GenericArrayType. |
| private static boolean | isAssignableFrom(Type from, ParameterizedType to, Map<String, Type> typeVarMap)
Private recursive helper function to actually do the type-safe checking of assignability. |
| private static boolean | |
| private static boolean | |
| public final String | |
| private static boolean | typeEquals(ParameterizedType from, ParameterizedType to, Map<String, Type> typeVarMap)
Checks if two parameterized types are exactly equal, under the variable replacement described in the typeVarMap. |
| private static void |
| hashCode | back to summary |
|---|---|
| private final int hashCode | |
| rawType | back to summary |
|---|---|
| private final Class<? super T> rawType | |
| type | back to summary |
|---|---|
| private final Type type | |
| TypeToken | back to summary |
|---|---|
| protected TypeToken() Constructs a new type literal. Derives represented class from type parameter. 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 | back to summary |
|---|---|
| private TypeToken(Type type) Unsafe. Constructs a type literal manually.
| |
| buildUnsupportedTypeException | back to summary |
|---|---|
| private static IllegalArgumentException buildUnsupportedTypeException(Type token, Class<?>... expected) | |
| equals | back to summary |
|---|---|
| public final boolean equals(Object o) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| get | back to summary |
|---|---|
| public static TypeToken Gets type literal for the given | |
| get | back to summary |
|---|---|
| public static <T> TypeToken Gets type literal for the given | |
| getArray | back to summary |
|---|---|
| public static TypeToken Gets type literal for the array type whose elements are all instances of | |
| getParameterized | back to summary |
|---|---|
| public static TypeToken Gets a type literal for the parameterized type represented by applying
As seen here the result is a TypeToken<?>; this method cannot provide any type-safety,
and care must be taken to pass in the correct number of type arguments.
If
| |
| getRawType | back to summary |
|---|---|
| public final Class Returns the raw (non-generic) type for this type. | |
| getType | back to summary |
|---|---|
| public final Type getType() Gets underlying | |
| getTypeTokenTypeArgument | back to summary |
|---|---|
| private Type getTypeTokenTypeArgument() Verifies that | |
| hashCode | back to summary |
|---|---|
| public final int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isAssignableFrom | back to summary |
|---|---|
| public boolean isAssignableFrom(Class<?> cls)
Deprecated this implementation may be inconsistent with javac for types with wildcards. Check if this type is assignable from the given class object.
| |
| isAssignableFrom | back to summary |
|---|---|
| public boolean isAssignableFrom(Type from)
Deprecated this implementation may be inconsistent with javac for types with wildcards. Check if this type is assignable from the given Type.
| |
| isAssignableFrom | back to summary |
|---|---|
| public boolean isAssignableFrom(TypeToken<?> token)
Deprecated this implementation may be inconsistent with javac for types with wildcards. Check if this type is assignable from the given type token.
| |
| isAssignableFrom | back to summary |
|---|---|
| private static boolean isAssignableFrom(Type from, GenericArrayType to) Private helper function that performs some assignability checks for the provided GenericArrayType. | |
| isAssignableFrom | back to summary |
|---|---|
| private static boolean isAssignableFrom(Type from, ParameterizedType to, Map<String, Type> typeVarMap) Private recursive helper function to actually do the type-safe checking of assignability. | |
| isCapturingTypeVariablesForbidden | back to summary |
|---|---|
| private static boolean isCapturingTypeVariablesForbidden() | |
| matches | back to summary |
|---|---|
| private static boolean matches(Type from, Type to, Map<String, Type> typeMap) Checks if two types are the same or are equivalent under a variable mapping given in the type map that was provided. | |
| toString | back to summary |
|---|---|
| public final String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| typeEquals | back to summary |
|---|---|
| private static boolean typeEquals(ParameterizedType from, ParameterizedType to, Map<String, Type> typeVarMap) Checks if two parameterized types are exactly equal, under the variable replacement described in the typeVarMap. | |
| verifyNoTypeVariable | back to summary |
|---|---|
| private static void verifyNoTypeVariable(Type type) | |