|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.ranides.assira.generic.TypeToken<T>
T - typ, który chcemy przekazać dalejpublic abstract class TypeToken<T>
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
| 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 |
|---|
public Type type()
type in interface GenericClasspublic int compareTo(TypeToken<T> object)
Comparable) is to prevent constructing a
reference without type information.
compareTo in interface Comparable<TypeToken<T>>public int hashCode()
hashCode in class Objectpublic boolean equals(Object obj)
equals in class Objectpublic String name()
name in interface GenericClasspublic Class<?> rawType()
rawType in interface GenericClasspublic GenericClass[] params()
params in interface GenericClasspublic boolean isParametrised()
isParametrised in interface GenericClasspublic boolean isResolved()
isResolved in interface GenericClasspublic String toString()
toString in interface GenericClasstoString in class Objectpublic static Class<?>[] getTypeArguments(Class<?> clazz)
clazz -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||