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 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:
    • isRawType

      public final boolean isRawType()
      Returns true if T is a raw type (i.e. Class<T>).
    • isParameterizedType

      public final boolean isParameterizedType()
      Returns true if T is a parameterized type.
    • isGenericArray

      public final boolean isGenericArray()
      Returns true if T is a generic array.
    • isTypeVariable

      public final boolean isTypeVariable()
      Returns true if T is a type variable.
    • isWildcard

      public final boolean isWildcard()
      Returns true if T is a wildcard.
    • typeArgumentAt

      public final Optional<TypeRef<?>> typeArgumentAt(int i)
      Returns the type argument of T at the given index, provided that T is a parameterized type that has at least as many arguments as the given index.

      For instance:

      • new TypeRef<List<String>>() {}.typeArgumentAt(0) => String
      • new TypeRef<List>() {}.typeArgumentAt(0) => No result
      • TypeRef.of(StringList.class).resolveSupertype(List.class).typeArgumentAt(0) => String where StringList implements List<String>
    • exactRawType

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

      public final T uncheckedCast(Object value)
      Performs an unchecked cast of the given object into T, at least ensuring that the given value's raw type is assignable to the raw type of T. This method must be used with care when it comes to generics, only when one is sure that value is of the generic type represented by this TypeRef. For raw types, prefer typeRef.exactRawType().cast(value).
    • resolveSupertype

      public final TypeRef<? super T> resolveSupertype(Class<?> supertype)
      Resolves the given supertype into a type with concrete type arguments (if any) that are derived from this type (i.e. T).

      For instance:

      • new TypeRef<ArrayList<String>>() {}.resolveSupertype(List.class) => List<String>
      • TypeRef.of(StringList.class).resolveSupertype(List.class) => List<String> where StringList implements List<String>
      • new TypeRef<ListOfArrayOf<String>>() {}.resolveSupertype(List.class) => List<String[]> where ListOfArrayOf<T> implements List<T[]>
      Throws:
      IllegalArgumentException - if the given type is not a supertype of the type represented by this TypeRef
    • typeArguments

      public final List<TypeRef<?>> typeArguments()
      Returns a list of TypeRef<?> corresponding to T's type arguments, provided it is a parameterized type, otherwise an empty list is returned.
    • equals

      public final 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 final int hashCode()
      Overrides:
      hashCode in class Object
    • toString

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

      @Deprecated @InlineMe(replacement="TypeRef.of(type)", imports="com.github.mizosoft.methanol.TypeRef") public static TypeRef<?> from(Type type)
      Deprecated.
      in favor of of(Type).
      Returns a new TypeRef who's type() is the given type.
      Throws:
      IllegalArgumentException - if the given type is not a standard specialization of Type
    • of

      public static TypeRef<?> of(Type type)
      Returns a new TypeRef who's type() is the given type.
      Throws:
      IllegalArgumentException - if the given type instance is not a standard specialization of Type
    • from

      @Deprecated @InlineMe(replacement="TypeRef.of(rawType)", imports="com.github.mizosoft.methanol.TypeRef") public static <U> TypeRef<U> from(Class<U> rawType)
      Deprecated.
      in favor of of(Class)
      Returns a new TypeRef who's type() is the given class.
    • of

      public static <U> TypeRef<U> of(Class<U> rawType)
      Returns a new TypeRef who's type() is the given class.
    • ofRuntimeType

      public static <T> TypeRef<? extends T> ofRuntimeType(T instance)
      Returns a new TypeRef who's type() is the runtime type of the given instance.