Class TypeToken<T>

java.lang.Object
com.google.common.reflect.TypeToken<T>
All Implemented Interfaces:
Serializable

@Beta @Deprecated(since="2022-12-01") public abstract class TypeToken<T> extends Object implements Serializable
Deprecated.
The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023
A Type with generics.

Operations that are otherwise only available in Class are implemented to support Type, for example isAssignableFrom(com.google.common.reflect.TypeToken<?>), isArray() and getComponentType(). It also provides additional utilities such as getTypes() and resolveType(java.lang.reflect.Type) etc.

There are three ways to get a TypeToken instance:

  • Wrap a Type obtained 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 the listType() 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
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Deprecated.
    The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023
  • Method Summary

    Modifier and Type
    Method
    Description
    final Invokable<T,T>
    constructor(Constructor<?> constructor)
    Deprecated.
    Returns the Invokable for constructor, which must be a member of T.
    boolean
    Deprecated.
    Returns true if o is another TypeToken that represents the same Type.
    final TypeToken<?>
    Deprecated.
    Returns the array component type if this type represents an array (int[], T[], <? extends Map<String, Integer>[]> etc.), or else null is returned.
    final Class<? super T>
    Deprecated.
    Returns the raw type of T.
    final TypeToken<? extends T>
    getSubtype(Class<?> subclass)
    Deprecated.
    Returns subtype of this with subclass as the raw class.
    final TypeToken<? super T>
    getSupertype(Class<? super T> superclass)
    Deprecated.
    Returns the generic form of superclass.
    final Type
    Deprecated.
    Returns the represented type.
    Deprecated.
    Returns the set of interfaces and classes that this type is or is a subtype of.
    int
    Deprecated.
     
    final boolean
    Deprecated.
    Returns true if this type is known to be an array type, such as int[], T[], <? extends Map<String, Integer>[]> etc.
    final boolean
    Deprecated.
    Returns true if this type is assignable from the given type.
    final boolean
    Deprecated.
    Check if this type is assignable from the given type.
    final boolean
    Deprecated.
    Returns true if this type is one of the nine primitive types (including void).
    method(Method method)
    Deprecated.
    Returns the Invokable for method, which must be a member of T.
    static <T> TypeToken<T>
    of(Class<T> type)
    Deprecated.
    Returns an instance of type token that wraps type.
    static TypeToken<?>
    of(Type type)
    Deprecated.
    Returns an instance of type token that wraps type.
    final TypeToken<?>
    Deprecated.
    Resolves the given type against the type context represented by this type.
    Deprecated.
     
    final TypeToken<T>
    Deprecated.
    Returns the corresponding primitive type if this is a wrapper type; otherwise returns this itself.
    final <X> TypeToken<T>
    where(TypeParameter<X> typeParam, TypeToken<X> typeArg)
    Deprecated.
    Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg.
    final <X> TypeToken<T>
    where(TypeParameter<X> typeParam, Class<X> typeArg)
    Deprecated.
    Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg.
    final TypeToken<T>
    Deprecated.
    Returns the corresponding wrapper type if this is a primitive type; otherwise returns this itself.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static <T> TypeToken<T> of(Class<T> type)
      Deprecated.
      Returns an instance of type token that wraps type.
    • of

      public static TypeToken<?> of(Type type)
      Deprecated.
      Returns an instance of type token that wraps type.
    • getRawType

      public final Class<? super T> getRawType()
      Deprecated.
      Returns the raw type of T. Formally speaking, if T is returned by Method.getGenericReturnType(), the raw type is what's returned by Method.getReturnType() of the same method object. Specifically:
      • If T is a Class itself, T itself is returned.
      • If T is a ParameterizedType, the raw type of the parameterized type is returned.
      • If T is a GenericArrayType, the returned type is the corresponding array class. For example: List<Integer>[] => List[].
      • If T is a type variable or a wildcard type, the raw type of the first upper bound is returned. For example: <X extends Foo> => Foo.
    • getType

      public final Type getType()
      Deprecated.
      Returns the represented type.
    • where

      public final <X> TypeToken<T> where(TypeParameter<X> typeParam, TypeToken<X> typeArg)
      Deprecated.

      Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg. For example, it can be used to construct Map<K, V> for any K and V type:

         
         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 variable
      typeArg - the actual type to substitute
    • where

      public final <X> TypeToken<T> where(TypeParameter<X> typeParam, Class<X> typeArg)
      Deprecated.

      Returns a new TypeToken where type variables represented by typeParam are substituted by typeArg. For example, it can be used to construct Map<K, V> for any K and V type:

         
         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 variable
      typeArg - the actual type to substitute
    • resolveType

      public final TypeToken<?> resolveType(Type type)
      Deprecated.

      Resolves the given type against 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<T>.TypeSet getTypes()
      Deprecated.
      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(Class<? super T> superclass)
      Deprecated.
      Returns the generic form of superclass. For example, if this is ArrayList<String>, Iterable<String> is returned given the input Iterable.class.
    • getSubtype

      public final TypeToken<? extends T> getSubtype(Class<?> subclass)
      Deprecated.
      Returns subtype of this with subclass as the raw class. For example, if this is Iterable<String> and subclass is List, List<String> is returned.
    • isAssignableFrom

      public final boolean isAssignableFrom(TypeToken<?> type)
      Deprecated.
      Returns true if this type is assignable from the given type.
    • isAssignableFrom

      public final boolean isAssignableFrom(Type type)
      Deprecated.
      Check if this type is assignable from the given type.
    • isArray

      public final boolean isArray()
      Deprecated.
      Returns true if this type is known to be an array type, such as int[], T[], <? extends Map<String, Integer>[]> etc.
    • isPrimitive

      public final boolean isPrimitive()
      Deprecated.
      Returns true if this type is one of the nine primitive types (including void).
      Since:
      15.0
    • wrap

      public final TypeToken<T> wrap()
      Deprecated.
      Returns the corresponding wrapper type if this is a primitive type; otherwise returns this itself. Idempotent.
      Since:
      15.0
    • unwrap

      public final TypeToken<T> unwrap()
      Deprecated.
      Returns the corresponding primitive type if this is a wrapper type; otherwise returns this itself. Idempotent.
      Since:
      15.0
    • getComponentType

      @Nullable public final TypeToken<?> getComponentType()
      Deprecated.
      Returns the array component type if this type represents an array (int[], T[], <? extends Map<String, Integer>[]> etc.), or else null is returned.
    • method

      public final Invokable<T,Object> method(Method method)
      Deprecated.
      Returns the Invokable for method, which must be a member of T.
      Since:
      14.0
    • constructor

      public final Invokable<T,T> constructor(Constructor<?> constructor)
      Deprecated.
      Returns the Invokable for constructor, which must be a member of T.
      Since:
      14.0
    • equals

      public boolean equals(@Nullable Object o)
      Deprecated.
      Returns true if o is another TypeToken that represents the same Type.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Deprecated.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Deprecated.
      Overrides:
      toString in class Object