com.twitter.common.reflect
Class TypeToken<T>

java.lang.Object
  extended by com.twitter.common.reflect.TypeToken<T>
Type Parameters:
T - The (possibly parameterized) type to be captured.
Direct Known Subclasses:
EasyMockTest.Clazz

public abstract class TypeToken<T>
extends Object

A class meant to be sub-classed in order to capture a generic type literal value. To capture the type of a List<String> you would use: new TypeToken<List<String>>() {}.

Types can also be captured directly using the create(java.lang.Class) static factory methods.

See Neil Gafter's post about this idea.


Field Summary
protected  Type type
          The type captured by this type token.
 
Constructor Summary
protected TypeToken()
           
 
Method Summary
static
<T> TypeToken<T>
create(Class<T> cls)
          Creates a type token for the given raw class.
static TypeToken<?> create(Type type)
          Creates a type token wrapping the given type.
 boolean equals(Object o)
          Implements value-equality over the this type token's captured type.
static Type extractTypeToken(Type type)
          Extracts the actual type parameter for a singly parameterized type.
 Class<T> getRawType()
          Finds the raw class of this type token.
static Class<?> getRawType(Type type)
          Finds the raw class of a type.
 Type getType()
          Returns the type captured by this type token.
 int hashCode()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

type

protected final Type type
The type captured by this type token.

Constructor Detail

TypeToken

protected TypeToken()
Method Detail

getType

public Type getType()
Returns the type captured by this type token.

Returns:
The captured type.

getRawType

public Class<T> getRawType()
Finds the raw class of this type token.

Returns:
The raw class of this type token.

equals

public boolean equals(Object o)
Implements value-equality over the this type token's captured type.

Overrides:
equals in class Object
Parameters:
o - Any object.
Returns:
true if o is a type token that captures the exact same type as this type token.

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

create

public static <T> TypeToken<T> create(Class<T> cls)
Creates a type token for the given raw class.

Parameters:
cls - The class to extract type information from.
Returns:
A type token representing the raw class type.

create

public static TypeToken<?> create(Type type)
Creates a type token wrapping the given type.

Parameters:
type - The type to wrap in a type token.
Returns:
A type token wrapping the type.

extractTypeToken

public static Type extractTypeToken(Type type)
Extracts the actual type parameter for a singly parameterized type.

Parameters:
type - The parameterized type to extract the type argument from.
Returns:
The type of the single specified type parameter for type.
Throws:
IllegalArgumentException - if the supplied type does not have exactly one specified type parameter

getRawType

public static Class<?> getRawType(Type type)
Finds the raw class of a type.

Parameters:
type - The type to get the raw class of.
Returns:
The raw class of the type.