net.ranides.assira.generic
Class TypeToken<T>

java.lang.Object
  extended by net.ranides.assira.generic.TypeToken<T>
Type Parameters:
T - typ, który chcemy przekazać dalej
All Implemented Interfaces:
Comparable<TypeToken<T>>, GenericClass

public abstract class TypeToken<T>
extends Object
implements Comparable<TypeToken<T>>, GenericClass

Klasa przekazująca dalej informację o klasie w genericu, jeśli nie mamy obiektu Class<T> możemy opakować T właśnie w tę klasę. Prosty przykład przypadku, który będzie stwarzał problem:

 List<T> makeList(Class<T> clazz, int size) {
     return new ArrayList<T>(size);
 }

 List<T> copyList(List<T> source) {
     List<T> target = makeList( ? , source.size());
     for(T item : source) target.add(item);
     return target;
 }
 
makeList pobiera instancję Class<T> bo jakoś musi dostać to nieszczęsne T. Problem w tym, że copyList już takiej instancji nie dostaje - a nie da się utworzyć new Class<T>() żeby jej przekazać. Oczywiście new TypeToken<T>() jest legalne. I tylko z tego właśnie powodu klasa została napisana. Rozszerzony i poprawiony kod:
 List<T> makeList(Class<T> clazz, int size) {
     return new ArrayList<T>(size);
 }

 List<T> makeList(TypeToken<T> token, int size) {
     return new ArrayList<T>(size);
 }

 List<T> copyList(List<T> source) {
     List<T> target = makeList( new TypeToken<T>(){} , source.size());
     for(T item : source) target.add(item);
     return target;
 }
 

Uwaga. Klasa rozszerzona, potrafi porządnie wydedukować instancję Class<T>. Rozszerzenie podpatrzone z biblioteki Jackson JSON Processor

Author:
ranides

Method Summary
 int compareTo(TypeToken<T> object)
          The only reason we define this method (and require implementation of Comparable) is to prevent constructing a reference without type information.
 boolean equals(Object obj)
           
static Class<?>[] getTypeArguments(Class<?> clazz)
          To przejdzie tylko wtedy, gdy wywołamy na rzecz klasy, która jest subklasą genericsa
 int hashCode()
           
 boolean isParametrised()
           
 boolean isResolved()
           
 String name()
           
 GenericClass[] params()
           
 Class<?> rawType()
           
 String toString()
           
 Type type()
          Zwraca wydedukowany typ parametru.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

type

public Type type()
Zwraca wydedukowany typ parametru. Bez dodatkowego wsparcia narzędzi wyodrębniających i przetwarzających otrzymaną informację niezbyt użyteczny.

Specified by:
type in interface GenericClass
Returns:

compareTo

public int compareTo(TypeToken<T> object)
The only reason we define this method (and require implementation of Comparable) is to prevent constructing a reference without type information.

Specified by:
compareTo in interface Comparable<TypeToken<T>>

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

name

public String name()
Specified by:
name in interface GenericClass

rawType

public Class<?> rawType()
Specified by:
rawType in interface GenericClass

params

public GenericClass[] params()
Specified by:
params in interface GenericClass

isParametrised

public boolean isParametrised()
Specified by:
isParametrised in interface GenericClass

isResolved

public boolean isResolved()
Specified by:
isResolved in interface GenericClass

toString

public String toString()
Specified by:
toString in interface GenericClass
Overrides:
toString in class Object

getTypeArguments

public static Class<?>[] getTypeArguments(Class<?> clazz)
To przejdzie tylko wtedy, gdy wywołamy na rzecz klasy, która jest subklasą genericsa

Parameters:
clazz -
Returns:


Copyright © 2013. All Rights Reserved.