public abstract class TypeReference<T> extends Object
Usage example:
// using anonymous classes (preferred)
TypeReference<Integer> intToken = new TypeReference<Integer>() {{ }};
// using named classes
class IntTypeReference extends TypeReference<Integer> {...}
TypeReference<Integer> intToken = new IntTypeReference();
Unlike the reference implementation, this bans nested TypeVariables; that is all dynamic types must equal to the static types.
See http://gafter.blogspot.com/2007/05/limitation-of-super-type-tokens.html for more details.
Modifier | Constructor and Description |
---|---|
protected |
TypeReference()
Create a new type reference for
T . |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsTypeVariable(Type type)
Check if the
type contains a TypeVariable recursively. |
static <T> TypeReference<T> |
createSpecializedTypeReference(Class<T> klass)
Create a specialized type reference from a dynamic class instance,
bypassing the standard compile-time checks.
|
static TypeReference<?> |
createSpecializedTypeReference(Type type)
Create a specialized type reference from a dynamic
Type instance,
bypassing the standard compile-time checks. |
boolean |
equals(Object o)
Compare two objects for equality.
|
TypeReference<?> |
getComponentType()
Get the component type, e.g.
|
Class<? super T> |
getRawType()
Returns the raw type of T.
|
Type |
getType()
Return the dynamic
Type corresponding to the captured type T . |
int |
hashCode()
Returns a hash code value for the object.
|
String |
toString()
Returns a string representation of the object.
|
protected TypeReference()
T
.IllegalArgumentException
- if T
's actual type contains a type variableTypeReference
public static <T> TypeReference<T> createSpecializedTypeReference(Class<T> klass)
As with a regular type reference, the klass
must not contain
any type variables.
klass
- a non-null
Class
instanceT
at runtimeIllegalArgumentException
- if T
had any type variablespublic static TypeReference<?> createSpecializedTypeReference(Type type)
Type
instance,
bypassing the standard compile-time checks.
As with a regular type reference, the type
must not contain
any type variables.
type
- a non-null
Type
instanceT
at runtimeIllegalArgumentException
- if type
had any type variablespublic final Class<? super T> getRawType()
List<Integer>[]
=> List[]
.
<X extends Foo>
=> Foo
.
T
public TypeReference<?> getComponentType()
T
from T[]
.null
if T
is not an arraypublic boolean equals(Object o)
A TypeReference is only equal to another TypeReference if their captured type T
is also equal.
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()
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
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public static boolean containsTypeVariable(Type type)
type
contains a TypeVariable
recursively.
Intuitively, a type variable is a type in a type expression that refers to a generic
type which is not known at the definition of the expression (commonly seen when
type parameters are used, e.g. class Foo<T>
).
See http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4 for a more formal definition of a type variable
.type
- a type object (null
is allowed)true
if there were nested type variables; false
otherwisepublic String toString()
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())