- 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 java.lang.ObjectA generic object that holds a reference to theTypeof its generic argumentTypeRef. 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 ordinaryClassobjects.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypeRef()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanequals(@Nullable java.lang.Object obj)Returnstrueif the given object is aTypeRefand both instances represent the same type.java.lang.Class<T>exactRawType()Returns the underlying type as aClass<T>for when it is known thatTypeRefis a raw type.static <U> TypeRef<U>from(java.lang.Class<U> rawType)Deprecated.in favor ofof(Class)static TypeRef<?>from(java.lang.reflect.Type type)Deprecated.in favor ofof(Type).inthashCode()booleanisGenericArray()booleanisParameterizedType()booleanisRawType()ReturnstrueifTypeRefis a raw type (i.e.booleanisTypeVariable()booleanisWildcard()static <U> TypeRef<U>of(java.lang.Class<U> rawType)Returns a newTypeRefwho'stype()is the given class.static TypeRef<?>of(java.lang.reflect.Type type)Returns a newTypeRefwho'stype()is the given type.static <T> TypeRef<? extends T>ofRuntimeType(T instance)Returns a newTypeRefwho'stype()is the runtime type of the given instance.java.lang.Class<? super T>rawType()Returns theClass<? super T>that represents the resolved raw type ofTypeRef.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.StringtoString()Returns a string representation for the type.java.lang.reflect.Typetype()Returns the underlyingType.java.util.Optional<TypeRef<?>>typeArgumentAt(int i)Returns the type argument ofTypeRefat the given index, provided thatTis aparameterized typethat has at least as many arguments as the given index.java.util.List<TypeRef<?>>typeArguments()Returns a list ofTypeRef<?>corresponding toTypeRef's type arguments, provided it is aparameterized type, otherwise an empty list is returned.TuncheckedCast(java.lang.Object value)
-
-
-
Constructor Detail
-
TypeRef
protected TypeRef()
Creates a newTypeRef<T>capturing theTypeofTypeRef. 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 underlyingType.
-
rawType
public final java.lang.Class<? super T> rawType()
Returns theClass<? super T>that represents the resolved raw type ofTypeRef. The returned class isClass<? super T>becauseTcan possibly be a generic type, and it is not semantically correct for aClassto be parameterized with such.- See Also:
exactRawType()
-
isRawType
public final boolean isRawType()
-
isParameterizedType
public final boolean isParameterizedType()
-
isGenericArray
public final boolean isGenericArray()
-
isTypeVariable
public final boolean isTypeVariable()
-
isWildcard
public final boolean isWildcard()
-
typeArgumentAt
public final java.util.Optional<TypeRef<?>> typeArgumentAt(int i)
Returns the type argument ofTypeRefat the given index, provided thatTis aparameterized typethat has at least as many arguments as the given index.For instance:
new TypeRef<List<String>>() {}.typeArgumentAt(0) => Stringnew TypeRef<List>() {}.typeArgumentAt(0) => No resultTypeRef.of(StringList.class).resolveSupertype(List.class).typeArgumentAt(0) => StringwhereStringList implements List<String>
-
exactRawType
public final java.lang.Class<T> exactRawType()
Returns the underlying type as aClass<T>for when it is known thatTypeRefis 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 intoTypeRef, at least ensuring that the given value's raw type is assignable to theraw typeofT. This method must be used with care when it comes to generics, only when one is sure thatvalueis of the generic type represented by thisTypeRef. For raw types, prefertypeRef.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>whereStringList implements List<String>new TypeRef<ListOfArrayOf<String>>() {}.resolveSupertype(List.class) => List<String[]>whereListOfArrayOf<T> implements List<T[]>
- Throws:
java.lang.IllegalArgumentException- if the given type is not a supertype of the type represented by thisTypeRef
-
typeArguments
public final java.util.List<TypeRef<?>> typeArguments()
Returns a list ofTypeRef<?>corresponding toTypeRef's type arguments, provided it is aparameterized type, otherwise an empty list is returned.
-
equals
public final boolean equals(@Nullable java.lang.Object obj)
Returnstrueif the given object is aTypeRefand both instances represent the same type.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public final java.lang.String toString()
Returns a string representation for the type.- Overrides:
toStringin classjava.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 ofof(Type).Returns a newTypeRefwho'stype()is the given type.- Throws:
java.lang.IllegalArgumentException- if the given type is not a standard specialization ofType
-
of
public static TypeRef<?> of(java.lang.reflect.Type type)
Returns a newTypeRefwho'stype()is the given type.- Throws:
java.lang.IllegalArgumentException- if the given type instance is not a standard specialization ofType
-
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 ofof(Class)Returns a newTypeRefwho'stype()is the given class.
-
of
public static <U> TypeRef<U> of(java.lang.Class<U> rawType)
Returns a newTypeRefwho'stype()is the given class.
-
-