Class TypeToken<T>

java.lang.Object
org.dominokit.jacksonapt.registration.TypeToken<T>
All Implemented Interfaces:
java.lang.Comparable<TypeToken<T>>

public class TypeToken<T>
extends java.lang.Object
implements java.lang.Comparable<TypeToken<T>>
A "supertype" token capable of representing any Java type. While the purpose of this class is primarily to be instantiated by gwt-jackson-apt itself and then just used by the user code, it is a public API.

Based on ideas from http://gafter.blogspot.com/2006/12/super-type-tokens.html, as well as on the implementation of the same notion in various Java libraries (Guava's TypeToken, Jackson's TypeReference, Guice's TypeLiteral, Gson's TypeToken, Apache Commons TypeLiteral - too many to enumerate all of those here). Unlike all those other implementations however, this TypeToken is designed to operate in a GWT/J2CL environment, where Java reflection - and therefore, the Type class on which all other implementations are based of - is not available. How to use: For simple, non-parameterized types like String, Integer or non-parameterized Java Beans:
TypeToken.of(String.class)

For parameterized types, like List<String>, in GWT/J2CL environments:
new TypeToken<List<String>>(List.class, TypeToken.of(String)){}
A more advanced example with a multiple-nesting of a parameterized type like List<Map<Integer, String>>:
In GWT/J2CL environments:
new TypeToken<List<Map<Integer, String>>(List.class, new TypeToken<Map<Integer, String>>(Map.class, TypeToken.of(Integer.class), TypeToken.of(String.class)) {}) {} 

The syntax for GWT/J2CL is much more verbose and requires the captured type not only to be written in the type parameter of the type token (TypeToken<...>) but to be also explicitly enumerated as a pair of a raw class type reference (i.e. "List.class") plus a chain of nested type token instantiations describing all the instantiations of the type parameters of the raw type for the concrete type we are trying to capture with the type token. This verbosity is unavoidable, because GWT/J2CL is missing Java reflection, which in turn prohibits the TypeToken instance from "introspecting" itself and figuring out the type automatically.
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected TypeToken​(java.lang.Class<? super T> rawType, TypeToken<?>... typeArguments)  
  • Method Summary

    Modifier and Type Method Description
    int compareTo​(TypeToken<T> o)
    The only reason we define this method (and require implementation of Comparable) is to prevent constructing a reference without type information.
    boolean equals​(java.lang.Object obj)  
    java.lang.Class<? super T> getRawType()
    Return the raw type represented by this TypeToken instance.
    TypeToken<?>[] getTypeArguments()
    Return the type tokens corresponding to the type arguments of the parameterized type represented by this type token.
    int hashCode()  
    static <T> TypeToken<T> of​(java.lang.Class<T> type)  
    java.lang.String stringify()  
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypeToken

      protected TypeToken​(java.lang.Class<? super T> rawType, TypeToken<?>... typeArguments)
  • Method Details

    • of

      public static <T> TypeToken<T> of​(java.lang.Class<T> type)
    • getRawType

      public final java.lang.Class<? super T> getRawType()
      Return the raw type represented by this TypeToken instance. E.g.:
      When called on TypeToken<String> it will return String.class
      When called on TypeToken<List<String>> it will return List.class

      For arrays, this method will return null.
    • getTypeArguments

      public final TypeToken<?>[] getTypeArguments()
      Return the type tokens corresponding to the type arguments of the parameterized type represented by this type token. If the type is not parameterized, an empty array is returned. For example:
      When called on TypeToken<String> an empty array will be returned
      When called on TypeToken<List<String>> a single-element array with a type token TypeToken<String>
      When called on TypeToken<String[]> a single-element array with a type token TypeToken<String> will be returned as well
    • compareTo

      public final int compareTo​(TypeToken<T> o)
      The only reason we define this method (and require implementation of Comparable) is to prevent constructing a reference without type information.
      Specified by:
      compareTo in interface java.lang.Comparable<T>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.lang.Object
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • stringify

      public final java.lang.String stringify()