Module methanol

Class TypeRef<T>

java.lang.Object
com.github.mizosoft.methanol.TypeRef<T>
Type Parameters:
T - represents the type this object holds a reference to

public abstract class TypeRef<T> extends Object
A generic object that holds a reference to the Type of its generic argument T. This class utilizes the supertype-token idiom, which is used to capture complex types (e.g. List<String>) that are otherwise impossible to represent using ordinary Class objects.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new TypeRef<T> capturing the Type of T.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(@Nullable Object obj)
    Returns true if the given object is a TypeRef and both instances represent the same type.
    final Class<T>
    Returns the underlying type as a Class<T> for when it is known that TypeRef is already raw.
    static <U> TypeRef<U>
    from(Class<U> rawType)
    Deprecated.
    in favor of the better-named of(Class)
    static TypeRef<?>
    from(Type type)
    Deprecated.
    in favor of the better-named of(Type).
    int
     
    static <U> TypeRef<U>
    of(Class<U> rawType)
    Creates a new TypeRef from the given class.
    static TypeRef<?>
    of(Type type)
    Deprecated.
    final Class<? super T>
    Returns the Class<? super T> that represents the resolved raw type of T.
    Returns a string representation for the type.
    final Type
    Returns the underlying Type.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypeRef

      protected TypeRef()
      Creates a new TypeRef<T> capturing the Type of T. This constructor is typically invoked as an anonymous class expression (e.g. new TypeRef<List<String>>() {}).
      Throws:
      IllegalStateException - if the raw version of this class is used
  • Method Details

    • type

      public final Type type()
      Returns the underlying Type.
    • rawType

      public final Class<? super T> rawType()
      Returns the Class<? super T> that represents the resolved raw type of T. The returned class is Class<? super T> because T can possibly be a generic type, and it is not semantically correct for a Class to be parameterized with such.
      See Also:
    • exactRawType

      public final Class<T> exactRawType()
      Returns the underlying type as a Class<T> for when it is known that TypeRef is already raw. Similar to (Class<T>) typeRef.type().
      Throws:
      UnsupportedOperationException - if the underlying type is not a raw type
    • equals

      public boolean equals(@Nullable Object obj)
      Returns true if the given object is a TypeRef and both instances represent the same type.
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Returns a string representation for the type.
      Overrides:
      toString in class Object
    • from

      @Deprecated public static TypeRef<?> from(Type type)
      Deprecated.
      in favor of the better-named of(Type).
      Creates a new TypeRef for the given type.
      Throws:
      IllegalArgumentException - if the given type is not a standard specialization of Type
    • of

      @Deprecated public static TypeRef<?> of(Type type)
      Deprecated.
      Creates a new TypeRef for the given type.
      Throws:
      IllegalArgumentException - if the given type is not a standard specialization of Type
    • from

      @Deprecated public static <U> TypeRef<U> from(Class<U> rawType)
      Deprecated.
      in favor of the better-named of(Class)
      Creates a new TypeRef from the given class.
    • of

      public static <U> TypeRef<U> of(Class<U> rawType)
      Creates a new TypeRef from the given class.