Class TypeToken<V>
- Type Parameters:
V- the type
A TypeToken stores the annotated, reified type data for a single written type. It represents this
information at the source level as the type parameter V, and at runtime via getReifiedType().
Creation
Construction of this type follows the common pattern seen in other libraries. The caller should instantiate an anonymous subclass, fully specifying the generic type. For example:
TypeToken<GenericConfig<String>> token = new TypeToken<GenericConfig<String>>() {};
The type data will be extracted thanks to the use of the anonymous subclass, which retains the declaration of its
superclass (and therefore the concrete generic parameter) at runtime. As one would expect, type variables are not
permitted in the type declaration, as they defeat the very purpose of using TypeToken (which is a runtime,
not source, variable) to begin with.
Keying and Usage
This class doubles as an immutable store of a single ReifiedType.Annotated. This class represents that type
and can be thought of as a higher-level wrapper for it. It contains that same information at runtime, while also
adding the source-level variable V.
Thus, in addition to extracting information upon subclass construction, this class can be used like any other POJO.
Callers can use the trusted constructor TypeToken(ReifiedType.Annotated), and they are expected to
choose an appropriate type parameter based on their usage of this class.
Equality and hash code are defined based on the ReifiedType.Annotated this token represents. Subclases
cannot override this behavior, which makes TypeToken safely consumable by various parts of the library and
its callers.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates from a subclass.TypeToken(@NonNull ReifiedType.Annotated reifiedType) Creates from a reified type. -
Method Summary
Modifier and TypeMethodDescriptionfinal @PolyNull VCasts the argument to this type.final booleanA type token is equal to another when it points to the same reified type (at runtime)Gets the raw type as a class objectfinal @NonNull ReifiedType.AnnotatedGets the annotated reified type represented by this type tokenfinal inthashCode()final StringtoString()
-
Constructor Details
-
TypeToken
Creates from a reified type.Caller ensures that the passed parameter is compatible with the source level usage of this type token.
- Parameters:
reifiedType- a reified type
-
TypeToken
protected TypeToken()Creates from a subclass.The subclass must fully specify concrete generic arguments. The most common approach is to create a simple anonymous subclass using double brackets.
Example usage
TypeToken<GenericConfig<String>> token = new TypeToken<GenericConfig<String>>() {};- Throws:
DeveloperMistakeException- if type variables were used in the type declaration
-
-
Method Details
-
cast
Casts the argument to this type.Generic types: Note that only the raw type (
getRawType()) can be checked at runtime, and not generic arguments.Primitive types: This method accepts
objas a boxed argument if this type token represents a primitive type.- Parameters:
obj- the object- Returns:
- the cast value
- Throws:
ClassCastException- if the obj is not null and not assignable to this type
-
getRawType
Gets the raw type as a class object- Returns:
- the raw type
-
getReifiedType
Gets the annotated reified type represented by this type token- Returns:
- the annotated reified type
-
equals
A type token is equal to another when it points to the same reified type (at runtime) -
hashCode
public final int hashCode() -
toString
-