Package 

Class TypeToken


  • 
    public abstract class TypeToken<T extends Object>
    
                        

    A container for a generic type.

    Java Usage:

    Type type = new TypeToken<List<Foo>>() {}.get();
    // type == List<Foo>
    AnnotatedType type = new TypeToken<List<@TypeAnnotation Foo>>() {}.getAnnotated();
    // type == List<@TypeAnnotation Foo>

    Kotlin usage:

    val type = object : TypeToken<List<Foo>>() {}.get()
    val type = typeToken<List<Foo>>() // or the helper function

    NOTE!! Due to a bug in javac before JDK 10, in most cases annotated type tokens will not work. https://github.com/raphw/byte-buddy/issues/583

    NOTE!! As of Kotlin 1.4, Java type annotations are finally supported, though in a limited fashion and gated behind a compiler flag. (more info here)

    • Constructor Summary

      Constructors 
      Constructor Description
      TypeToken()
    • Method Summary

      Modifier and Type Method Description
      final Type get() Gets the generic type represented by this TypeToken
      final AnnotatedType getAnnotated() Gets the annotated type represented by this TypeToken
      final KTypeProjection getKotlin() Gets the kotlin type represented by this TypeToken
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TypeToken

        TypeToken()