@Beta public abstract class TypeToken<T> extends Object implements Serializable
Type
with generics.
Operations that are otherwise only available in Class
are implemented to support
Type
, for example isSubtypeOf(com.google.common.reflect.TypeToken<?>)
, isArray()
and getComponentType()
.
It also provides additional utilities such as getTypes()
, resolveType(java.lang.reflect.Type)
, etc.
There are three ways to get a TypeToken
instance:
Type
obtained via reflection. For example: TypeToken.of(method.getGenericReturnType())
.
new TypeToken<List<String>>() {}
Note that it's critical that the actual type argument is carried by a subclass. The
following code is wrong because it only captures the <T>
type variable of the
listType()
method signature; while <String>
is lost in erasure:
class Util {
static <T> TypeToken<List<T>> listType() {
return new TypeToken<List<T>>() {};
}
}
TypeToken<List<String>> stringListType = Util.<String>listType();
abstract class IKnowMyType<T> {
TypeToken<T> type = new TypeToken<T>(getClass()) {};
}
new IKnowMyType<String>() {}.type => String
TypeToken
is serializable when no type variable is contained in the type.
Note to Guice users: TypeToken is similar to Guice's TypeLiteral
class except
that it is serializable and offers numerous additional utility methods.
Modifier and Type | Class and Description |
---|---|
class |
TypeToken.TypeSet
The set of interfaces and classes that
T is or is a subtype of. |
Modifier | Constructor and Description |
---|---|
protected |
TypeToken()
Constructs a new type token of
T . |
protected |
TypeToken(Class<?> declaringClass)
Constructs a new type token of
T while resolving free type variables in the context of
declaringClass . |
Modifier and Type | Method and Description |
---|---|
Invokable<T,T> |
constructor(Constructor<?> constructor)
|
boolean |
equals(@Nullable Object o)
|
@Nullable TypeToken<?> |
getComponentType()
Returns the array component type if this type represents an array (
int[] , T[] ,
<? extends Map<String, Integer>[]> etc.), or else null is returned. |
Class<? super T> |
getRawType()
Returns the raw type of
T . |
TypeToken<? extends T> |
getSubtype(Class<?> subclass)
Returns subtype of
this with subclass as the raw class. |
TypeToken<? super T> |
getSupertype(Class<? super T> superclass)
Returns the generic form of
superclass . |
Type |
getType()
Returns the represented type.
|
TypeToken.TypeSet |
getTypes()
Returns the set of interfaces and classes that this type is or is a subtype of.
|
int |
hashCode()
Returns a hash code value for the object.
|
boolean |
isArray()
Returns true if this type is known to be an array type, such as
int[] , T[] ,
<? extends Map<String, Integer>[]> etc. |
boolean |
isPrimitive()
Returns true if this type is one of the nine primitive types (including
void ). |
boolean |
isSubtypeOf(Type supertype)
Returns true if this type is a subtype of the given
type . |
boolean |
isSubtypeOf(TypeToken<?> type)
Returns true if this type is a subtype of the given
type . |
boolean |
isSupertypeOf(Type type)
Returns true if this type is a supertype of the given
type . |
boolean |
isSupertypeOf(TypeToken<?> type)
Returns true if this type is a supertype of the given
type . |
Invokable<T,Object> |
method(Method method)
|
static <T> TypeToken<T> |
of(Class<T> type)
Returns an instance of type token that wraps
type . |
static TypeToken<?> |
of(Type type)
Returns an instance of type token that wraps
type . |
TypeToken<?> |
resolveType(Type type)
Resolves the given
type against the type context represented by this type. |
String |
toString()
Returns a string representation of the object.
|
TypeToken<T> |
unwrap()
Returns the corresponding primitive type if this is a wrapper type; otherwise returns
this itself. |
<X> TypeToken<T> |
where(TypeParameter<X> typeParam,
Class<X> typeArg)
Returns a new
TypeToken where type variables represented by typeParam are
substituted by typeArg . |
<X> TypeToken<T> |
where(TypeParameter<X> typeParam,
TypeToken<X> typeArg)
Returns a new
TypeToken where type variables represented by typeParam are
substituted by typeArg . |
TypeToken<T> |
wrap()
Returns the corresponding wrapper type if this is a primitive type; otherwise returns
this itself. |
protected Object |
writeReplace()
Implemented to support serialization of subclasses.
|
protected TypeToken()
T
.
Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
For example:
TypeToken<List<String>> t = new TypeToken<List<String>>() {};
protected TypeToken(Class<?> declaringClass)
T
while resolving free type variables in the context of
declaringClass
.
Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
For example:
abstract class IKnowMyType<T> {
TypeToken<T> getMyType() {
return new TypeToken<T>(getClass()) {};
}
}
new IKnowMyType<String>() {}.getMyType() => String
public static <T> TypeToken<T> of(Class<T> type)
type
.public final Class<? super T> getRawType()
T
. Formally speaking, if T
is returned by Method.getGenericReturnType()
, the raw type is what's returned by Method.getReturnType()
of the same method object. Specifically:
T
is a Class
itself, T
itself is returned.
T
is a ParameterizedType
, the raw type of the parameterized type is
returned.
T
is a GenericArrayType
, the returned type is the corresponding array
class. For example: List<Integer>[] => List[]
.
T
is a type variable or a wildcard type, the raw type of the first upper bound
is returned. For example: <X extends Foo> => Foo
.
public final <X> TypeToken<T> where(TypeParameter<X> typeParam, TypeToken<X> typeArg)
TypeToken
where type variables represented by typeParam
are
substituted by typeArg
. For example, it can be used to construct Map<K, V>
for
any K
and V
type:
static <K, V> TypeToken<Map<K, V>> mapOf(
TypeToken<K> keyType, TypeToken<V> valueType) {
return new TypeToken<Map<K, V>>() {}
.where(new TypeParameter<K>() {}, keyType)
.where(new TypeParameter<V>() {}, valueType);
}
X
- The parameter typetypeParam
- the parameter type variabletypeArg
- the actual type to substitutepublic final <X> TypeToken<T> where(TypeParameter<X> typeParam, Class<X> typeArg)
TypeToken
where type variables represented by typeParam
are
substituted by typeArg
. For example, it can be used to construct Map<K, V>
for
any K
and V
type:
static <K, V> TypeToken<Map<K, V>> mapOf(
Class<K> keyType, Class<V> valueType) {
return new TypeToken<Map<K, V>>() {}
.where(new TypeParameter<K>() {}, keyType)
.where(new TypeParameter<V>() {}, valueType);
}
X
- The parameter typetypeParam
- the parameter type variabletypeArg
- the actual type to substitutepublic final TypeToken<?> resolveType(Type type)
type
against the type context represented by this type. For example:
new TypeToken<List<String>>() {}.resolveType(
List.class.getMethod("get", int.class).getGenericReturnType())
=> String.class
public final TypeToken.TypeSet getTypes()
Subtypes are always listed before supertypes. But the reverse is not true. A type isn't necessarily a subtype of all the types following. Order between types without subtype relationship is arbitrary and not guaranteed.
If this type is a type variable or wildcard, upper bounds that are themselves type variables aren't included (their super interfaces and superclasses are).
public final TypeToken<? super T> getSupertype(Class<? super T> superclass)
superclass
. For example, if this is ArrayList<String>
, Iterable<String>
is returned given the input Iterable.class
.public final TypeToken<? extends T> getSubtype(Class<?> subclass)
this
with subclass
as the raw class. For example, if this is
Iterable<String>
and subclass
is List
, List<String>
is
returned.public final boolean isSupertypeOf(TypeToken<?> type)
type
. "Supertype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isSupertypeOf(Type type)
type
. "Supertype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isSubtypeOf(TypeToken<?> type)
type
. "Subtype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isSubtypeOf(Type supertype)
type
. "Subtype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isArray()
int[]
, T[]
,
<? extends Map<String, Integer>[]>
etc.public final boolean isPrimitive()
void
).public final TypeToken<T> wrap()
this
itself. Idempotent.public final TypeToken<T> unwrap()
this
itself. Idempotent.public final @Nullable TypeToken<?> getComponentType()
int[]
, T[]
,
<? extends Map<String, Integer>[]>
etc.), or else null
is returned.public final Invokable<T,T> constructor(Constructor<?> constructor)
public boolean equals(@Nullable Object o)
equals
in class Object
o
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
java.lang.Object
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
Java™ programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
java.lang.Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
protected Object writeReplace()
Copyright © 2010–2018. All rights reserved.