Module methanol

Class TypeRef<T>

  • Type Parameters:
    T - represents the type this object holds a reference to

    public abstract class TypeRef<T>
    extends java.lang.Object
    A generic object that holds a reference to the Type of its generic argument TypeRef. 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 TypeRef()
      Creates a new TypeRef<T> capturing the Type of TypeRef.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean equals​(@Nullable java.lang.Object obj)
      Returns true if the given object is a TypeRef and both instances represent the same type.
      java.lang.Class<T> exactRawType()
      Returns the underlying type as a Class<T> for when it is known that TypeRef is a raw type.
      static <U> TypeRef<U> from​(java.lang.Class<U> rawType)
      Deprecated.
      in favor of of(Class)
      static TypeRef<?> from​(java.lang.reflect.Type type)
      Deprecated.
      in favor of of(Type).
      int hashCode()  
      boolean isGenericArray()
      Returns true if TypeRef is a generic array.
      boolean isParameterizedType()
      Returns true if TypeRef is a parameterized type.
      boolean isRawType()
      Returns true if TypeRef is a raw type (i.e.
      boolean isTypeVariable()
      Returns true if TypeRef is a type variable.
      boolean isWildcard()
      Returns true if TypeRef is a wildcard.
      static <U> TypeRef<U> of​(java.lang.Class<U> rawType)
      Returns a new TypeRef who's type() is the given class.
      static TypeRef<?> of​(java.lang.reflect.Type type)
      Returns a new TypeRef who's type() is the given type.
      static <T> TypeRef<? extends T> ofRuntimeType​(T instance)
      Returns a new TypeRef who's type() is the runtime type of the given instance.
      java.lang.Class<? super T> rawType()
      Returns the Class<? super T> that represents the resolved raw type of TypeRef.
      TypeRef<? super T> resolveSupertype​(java.lang.Class<?> supertype)
      Resolves the given supertype into a type with concrete type arguments (if any) that are derived from this type (i.e.
      java.lang.String toString()
      Returns a string representation for the type.
      java.lang.reflect.Type type()
      Returns the underlying Type.
      java.util.Optional<TypeRef<?>> typeArgumentAt​(int i)
      Returns the type argument of TypeRef at the given index, provided that T is a parameterized type that has at least as many arguments as the given index.
      java.util.List<TypeRef<?>> typeArguments()
      Returns a list of TypeRef<?> corresponding to TypeRef's type arguments, provided it is a parameterized type, otherwise an empty list is returned.
      T uncheckedCast​(java.lang.Object value)
      Performs an unchecked cast of the given object into TypeRef, at least ensuring that the given value's raw type is assignable to the raw type of T.
      • Methods inherited from class java.lang.Object

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

      • TypeRef

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

      • type

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

        public final java.lang.Class<? super T> rawType()
        Returns the Class<? super T> that represents the resolved raw type of TypeRef. 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()
      • isRawType

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

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

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

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

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

        public final java.util.Optional<TypeRef<?>> typeArgumentAt​(int i)
        Returns the type argument of TypeRef 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 java.lang.Class<T> exactRawType()
        Returns the underlying type as a Class<T> for when it is known that TypeRef is a raw type. Equivalent to (Class<T>) type.type().
        Throws:
        java.lang.UnsupportedOperationException - if the underlying type is not a raw type
      • uncheckedCast

        public final T uncheckedCast​(java.lang.Object value)
        Performs an unchecked cast of the given object into TypeRef, 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​(java.lang.Class<?> supertype)
        Resolves the given supertype into a type with concrete type arguments (if any) that are derived from this type (i.e. TypeRef).

        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:
        java.lang.IllegalArgumentException - if the given type is not a supertype of the type represented by this TypeRef
      • typeArguments

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

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

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

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

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

        public static TypeRef<?> of​(java.lang.reflect.Type type)
        Returns a new TypeRef who's type() is the given type.
        Throws:
        java.lang.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​(java.lang.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​(java.lang.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.