com.google.gson.reflect
Class TypeToken<T>

java.lang.Object
  extended by com.google.gson.reflect.TypeToken<T>

public abstract class TypeToken<T>
extends Object

Represents a generic type T. You can use this class to get the generic type for a class. For example, to get the generic type for Collection<Foo>, you can use:

Type typeOfCollectionOfFoo = new TypeToken<Collection<Foo>>(){}.getType()

Assumes Type implements equals() and hashCode() as a value (as opposed to identity) comparison. Also implements isAssignableFrom(Type) to check type-safe assignability.

Author:
Bob Lee, Sven Mawson

Constructor Summary
protected TypeToken()
          Constructs a new type token.
 
Method Summary
 boolean equals(Object o)
          Method to test equality.
static
<T> TypeToken<T>
get(Class<T> type)
          Gets type token for the given Class instance.
static TypeToken<?> get(Type type)
          Gets type token for the given Type instance.
 Class<? super T> getRawType()
          Gets the raw type.
 Type getType()
          Gets underlying Type instance.
 int hashCode()
          Hashcode for this object.
 boolean isAssignableFrom(Class<?> cls)
          Check if this type is assignable from the given class object.
 boolean isAssignableFrom(Type from)
          Check if this type is assignable from the given Type.
 boolean isAssignableFrom(TypeToken<?> token)
          Check if this type is assignable from the given type token.
 String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TypeToken

protected TypeToken()
Constructs a new type token. Derives represented class from type parameter.

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>>(){}

Method Detail

getRawType

public Class<? super T> getRawType()
Gets the raw type.


getType

public Type getType()
Gets underlying Type instance.


isAssignableFrom

public boolean isAssignableFrom(Class<?> cls)
Check if this type is assignable from the given class object.


isAssignableFrom

public boolean isAssignableFrom(Type from)
Check if this type is assignable from the given Type.


isAssignableFrom

public boolean isAssignableFrom(TypeToken<?> token)
Check if this type is assignable from the given type token.


hashCode

public int hashCode()
Hashcode for this object.

Overrides:
hashCode in class Object
Returns:
hashcode for this object.

equals

public boolean equals(Object o)
Method to test equality.

Overrides:
equals in class Object
Returns:
true if this object is logically equal to the specified object, false otherwise.

toString

public String toString()
Returns a string representation of this object.

Overrides:
toString in class Object
Returns:
a string representation of this object.

get

public static TypeToken<?> get(Type type)
Gets type token for the given Type instance.


get

public static <T> TypeToken<T> get(Class<T> type)
Gets type token for the given Class instance.



Copyright © 2008. All Rights Reserved.