Class TypeToken<V>

java.lang.Object
space.arim.dazzleconf.reflect.TypeToken<V>
Type Parameters:
V - the type

public class TypeToken<V> extends Object
Token for handling configuration-related types at runtime.

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 Details

    • TypeToken

      public TypeToken(@NonNull ReifiedType.Annotated reifiedType)
      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

      public final @PolyNull V cast(@PolyNull Object obj)
      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 obj as 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

      public final @NonNull Class<V> getRawType()
      Gets the raw type as a class object
      Returns:
      the raw type
    • getReifiedType

      public final @NonNull ReifiedType.Annotated getReifiedType()
      Gets the annotated reified type represented by this type token
      Returns:
      the annotated reified type
    • equals

      public final boolean equals(Object o)
      A type token is equal to another when it points to the same reified type (at runtime)
      Overrides:
      equals in class Object
      Parameters:
      o - the other object
      Returns:
      true if equal
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public final String toString()
      Overrides:
      toString in class Object