Class TypeToken<T>

java.lang.Object
com.google.gson.reflect.TypeToken<T>

public class TypeToken<T> extends Object
Represents a generic type 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 a TypeToken should be avoided. 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, which gives a false sense of type-safety at compilation time and can 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.

  • Field Details

    • rawType

      private final Class<? super T> rawType
    • type

      private final Type type
    • hashCode

      private final int hashCode
  • Constructor Details

    • TypeToken

      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.

    • TypeToken

      private TypeToken(Type type)
      Unsafe. Constructs a type literal manually.
  • Method Details

    • getTypeTokenTypeArgument

      private Type getTypeTokenTypeArgument()
      Verifies that this is an instance of a direct subclass of TypeToken and returns the type argument for T in canonical form.
    • getRawType

      public final Class<? super T> getRawType()
      Returns the raw (non-generic) type for this type.
    • getType

      public final Type getType()
      Gets underlying Type instance.
    • isAssignableFrom

      @Deprecated 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

      @Deprecated 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

      @Deprecated 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

      private static boolean isAssignableFrom(Type from, GenericArrayType to)
      Private helper function that performs some assignability checks for the provided GenericArrayType.
    • isAssignableFrom

      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.
    • typeEquals

      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.
    • buildUnexpectedTypeError

      private static AssertionError buildUnexpectedTypeError(Type token, Class<?>... expected)
    • matches

      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.
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public final boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • get

      public static TypeToken<?> get(Type type)
      Gets type literal for the given Type instance.
    • get

      public static <T> TypeToken<T> get(Class<T> type)
      Gets type literal for the given Class instance.
    • getParameterized

      public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)
      Gets a type literal for the parameterized type represented by applying typeArguments to rawType. This is mainly intended for situations where the type arguments are not available at compile time. The following example shows how a type token for Map<K, V> can be created:
      
       Class<K> keyClass = ...;
       Class<V> valueClass = ...;
       TypeToken<?> mapTypeToken = TypeToken.getParameterized(Map.class, keyClass, valueClass);
       
      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.
      Throws:
      IllegalArgumentException - If rawType is not of type Class, or if the type arguments are invalid for the raw type
    • getArray

      public static TypeToken<?> getArray(Type componentType)
      Gets type literal for the array type whose elements are all instances of componentType.