Type with generics.
Operations that are otherwise only available in Class are implemented to support
Type, for example isSubtypeOf, isArray and getComponentType.
It also provides additional utilities such as getTypes, resolveType, etc.
There are three ways to get a TypeToken instance:
Type obtained via reflection. For example: TypeToken.of(method.getGenericReturnType()).
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();
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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private class | |
| private class | |
| private static class | |
| private abstract static class | TypeToken.TypeCollector<
The type "kind". Either a TypeToken, or Class. K>Collects parent types from a subtype. |
| private static enum | |
| public class | TypeToken.TypeSet
The set of interfaces and classes that |
| Modifier and Type | Field and Description |
|---|---|
| private transient TypeResolver | covariantTypeResolver
Resolver for resolving covariant types with |
| private transient TypeResolver | invariantTypeResolver
Resolver for resolving parameter and field types with |
| private final Type | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static TypeToken. | |
| private TypeToken | |
| private ImmutableList | |
| private static Type | |
| private static ParameterizedType | |
| private static Type | |
| private static WildcardType | |
| public final Invokable | constructor(Constructor<?> constructor)
Returns the |
| public boolean | equals(Object
the reference object with which to compare. o)Overrides java. o is another TypeToken that represents the same Type.
|
| private static TypeToken. | |
| private TypeToken | |
| private TypeToken | |
| public final TypeToken | |
| private TypeResolver | |
| pack-priv final ImmutableList | |
| pack-priv final TypeToken | getGenericSuperclass()
Returns the generic superclass of this type or |
| private TypeResolver | |
| private Type | getOwnerTypeIfPresent()
Returns the owner type of a |
| public final Class | |
| private ImmutableSet | |
| public final TypeToken | |
| private TypeToken | |
| public final TypeToken | |
| private TypeToken | |
| public final Type | |
| public final TypeToken | |
| public int | |
| private boolean | is(Type
is formalType, TypeVariable<?> Foo<formalType> a supertype of Foo<T>?The type variable in the context of a parameterized type. Used to infer type
bound when declaration)formalType is a wildcard with implicit upper bound.
|
| public final boolean | |
| private boolean | |
| public final boolean | |
| public final boolean | |
| public final boolean | |
| private boolean | |
| private boolean | |
| public final boolean | |
| public final boolean | |
| private boolean | |
| private boolean | |
| public final Invokable | |
| private static Type | newArrayClassOrGenericArrayType(Type componentType)
Creates an array class if |
| public static <T> TypeToken | |
| public static TypeToken | |
| pack-priv final TypeToken | rejectTypeVariables()
Ensures that this type token doesn't contain type variables, which can cause unchecked type
errors for callers like |
| private TypeToken | |
| public final TypeToken | |
| private Type | |
| private boolean | |
| pack-priv static <T> TypeToken | |
| public String | |
| public final TypeToken | unwrap()
Returns the corresponding primitive type if this is a wrapper type; otherwise returns |
| public final < The parameter type X> TypeToken | where(TypeParameter<X>
the parameter type variable typeParam, TypeToken<X> the actual type to substitute typeArg)Returns a new |
| public final < The parameter type X> TypeToken | where(TypeParameter<X>
the parameter type variable typeParam, Class<X> the actual type to substitute typeArg)Returns a new |
| public final TypeToken | wrap()
Returns the corresponding wrapper type if this is a primitive type; otherwise returns |
| protected Object |