Class TypeRef<T>
java.lang.Object
com.github.mizosoft.methanol.TypeRef<T>
- Type Parameters:
T- represents the type this object holds a reference to
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanReturnstrueif the given object is aTypeRefand both instances represent the same type.Returns the underlying type as aClass<T>for when it is known thatTis a raw type.static <U> TypeRef<U> Deprecated.static TypeRef<?> Deprecated.in favor ofof(Type).final inthashCode()final booleanfinal booleanfinal booleanReturnstrueifTis a raw type (i.e.final booleanfinal booleanstatic <U> TypeRef<U> Returns a newTypeRefwho'stype()is the given class.static TypeRef<?> 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.rawType()Returns theClass<? super T>that represents the resolved raw type ofT.resolveSupertype(Class<?> supertype) Resolves the given supertype into a type with concrete type arguments (if any) that are derived from this type (i.e.final StringtoString()Returns a string representation for the type.final Typetype()Returns the underlyingType.typeArgumentAt(int i) Returns the type argument ofTat the given index, provided thatTis aparameterized typethat has at least as many arguments as the given index.Returns a list ofTypeRef<?>corresponding toT's type arguments, provided it is aparameterized type, otherwise an empty list is returned.final TuncheckedCast(Object value)
-
Constructor Details
-
TypeRef
protected TypeRef()Creates a newTypeRef<T>capturing theTypeofT. 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
-
rawType
-
isRawType
public final boolean isRawType() -
isParameterizedType
public final boolean isParameterizedType() -
isGenericArray
public final boolean isGenericArray() -
isTypeVariable
public final boolean isTypeVariable() -
isWildcard
-
typeArgumentAt
Returns the type argument ofTat 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
Returns the underlying type as aClass<T>for when it is known thatTis a raw type. Equivalent to(Class<T>) type.type().- Throws:
UnsupportedOperationException- if the underlying type is not a raw type
-
uncheckedCast
Performs an unchecked cast of the given object intoT, 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
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>whereStringList implements List<String>new TypeRef<ListOfArrayOf<String>>() {}.resolveSupertype(List.class) => List<String[]>whereListOfArrayOf<T> implements List<T[]>
- Throws:
IllegalArgumentException- if the given type is not a supertype of the type represented by thisTypeRef
-
typeArguments
Returns a list ofTypeRef<?>corresponding toT's type arguments, provided it is aparameterized type, otherwise an empty list is returned. -
equals
-
hashCode
-
toString
-
from
@Deprecated @InlineMe(replacement="TypeRef.of(type)", imports="com.github.mizosoft.methanol.TypeRef") public static TypeRef<?> from(Type type) Deprecated.in favor ofof(Type).Returns a newTypeRefwho'stype()is the given type.- Throws:
IllegalArgumentException- if the given type is not a standard specialization ofType
-
of
Returns a newTypeRefwho'stype()is the given type.- Throws:
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(Class<U> rawType) Deprecated.in favor ofof(Class)Returns a newTypeRefwho'stype()is the given class. -
of
-
ofRuntimeType
-
of(Class)