Module io.jooby
Package io.jooby

Class Reified<T>

java.lang.Object
io.jooby.Reified<T>
Type Parameters:
T - Target type.

public class Reified<T> extends Object
Represents a generic type T. Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.

For example, to create a type literal for List<String>, you can create an empty anonymous inner class:

Reified<List<String>> list = new Reified<List<String>>() {};

This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.

Author:
Bob Lee, Sven Mawson, Jesse Wilson
  • Constructor Details

    • Reified

      public Reified()
      Constructs a new type literal. Derives represented class from type parameter.

      Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.

  • Method Details

    • getRawType

      @NonNull public final Class<? super T> getRawType()
      Returns the raw (non-generic) type for this type.
      Returns:
      Returns the raw (non-generic) type for this type.
    • getType

      @NonNull public final Type getType()
      Gets underlying Type instance.
      Returns:
      Gets underlying Type instance.
    • hashCode

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

      public final boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

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

      @NonNull public static Reified<?> get(@NonNull Type type)
      Gets type literal for the given Type instance.
      Parameters:
      type - Source type.
      Returns:
      Gets type literal for the given Type instance.
    • rawType

      @NonNull public static Class<?> rawType(@NonNull Type type)
      Get raw type (class) from given type.
      Parameters:
      type - Type.
      Returns:
      Raw type.
    • get

      @NonNull public static <T> Reified<T> get(@NonNull Class<T> type)
      Gets type literal for the given Class instance.
      Type Parameters:
      T - Generic type.
      Parameters:
      type - Java type.
      Returns:
      Gets type literal for the given Class instance.
    • list

      @NonNull public static <T> Reified<List<T>> list(@NonNull Type type)
      Creates a List type literal.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Item type.
      Returns:
      A List type literal.
    • set

      @NonNull public static <T> Reified<Set<T>> set(@NonNull Type type)
      Creates a Set type literal.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Item type.
      Returns:
      A Set type literal.
    • optional

      @NonNull public static <T> Reified<Optional<T>> optional(@NonNull Type type)
      Creates an Optional type literal.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Item type.
      Returns:
      A Optional type literal.
    • map

      @NonNull public static <K, V> Reified<Map<K,V>> map(@NonNull Type key, @NonNull Type value)
      Creates an Map type literal.
      Type Parameters:
      K - Key type.
      V - Key type.
      Parameters:
      key - Key type.
      value - Value type.
      Returns:
      A Map type literal.
    • completableFuture

      @NonNull public static <T> Reified<CompletableFuture<T>> completableFuture(@NonNull Type type)
      Creates a CompletableFuture type literal.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Item type.
      Returns:
      A CompletableFuture type literal.
    • getParameterized

      @NonNull public static Reified<?> getParameterized(@NonNull Type rawType, @NonNull Type... typeArguments)
      Gets type literal for the parameterized type represented by applying typeArguments to rawType.
      Parameters:
      rawType - Raw type.
      typeArguments - Parameter types.
      Returns:
      Gets type literal for the parameterized type represented by applying typeArguments to rawType.