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
For parameterized types, like List<String>, in GWT/J2CL environments:
In GWT/J2CL environments:
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 (
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
-
Method Summary
Modifier and Type Method Description intcompareTo(TypeToken<T> o)The only reason we define this method (and require implementation ofComparable) is to prevent constructing a reference without type information.booleanequals(java.lang.Object obj)java.lang.Class<? super T>getRawType()Return the raw type represented by thisTypeTokeninstance.TypeToken<?>[]getTypeArguments()Return the type tokens corresponding to the type arguments of the parameterized type represented by this type token.inthashCode()static <T> TypeToken<T>of(java.lang.Class<T> type)java.lang.Stringstringify()java.lang.StringtoString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Constructor Details
-
TypeToken
-
-
Method Details
-
of
-
getRawType
Return the raw type represented by thisTypeTokeninstance. E.g.:
When called onTypeToken<String>it will returnString.class
When called onTypeToken<List<String>>it will returnList.class
For arrays, this method will return null. -
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 onTypeToken<String>an empty array will be returned
When called onTypeToken<List<String>>a single-element array with a type tokenTypeToken<String>
When called onTypeToken<String[]>a single-element array with a type tokenTypeToken<String>will be returned as well -
compareTo
The only reason we define this method (and require implementation ofComparable) is to prevent constructing a reference without type information.- Specified by:
compareToin interfacejava.lang.Comparable<T>
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
stringify
public final java.lang.String stringify()
-