public class TypeToken<T> extends Object implements Comparable<TypeToken<T>>
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)
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>>:new TypeToken<List<Map<Integer, String>>(List.class, new TypeToken<Map<Integer, String>>(Map.class, TypeToken.of(Integer.class), TypeToken.of(String.class)) {}) {}
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.| Modifier | Constructor and Description |
|---|---|
protected |
TypeToken(Class<? super T> rawType,
TypeToken<?>... typeArguments) |
| Modifier and Type | Method and 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(Object obj) |
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(Class<T> type) |
String |
stringify() |
String |
toString() |
public final Class<? super T> getRawType()
TypeToken instance. E.g.:TypeToken<String> it will return String.class TypeToken<List<String>> it will return List.classpublic final TypeToken<?>[] getTypeArguments()
TypeToken<String> an empty array will be returnedTypeToken<List<String>> a single-element array with a type token TypeToken<String>TypeToken<String[]> a single-element array with a type token TypeToken<String> will be returned as wellpublic final int compareTo(TypeToken<T> o)
Comparable) is to prevent constructing a
reference without type information.compareTo in interface Comparable<TypeToken<T>>public final String stringify()
Copyright © 2020. All rights reserved.