T
- the type that this generic type literal representspublic abstract class Generic<T> extends Object implements Serializable
This class captures the actual type argument used when subclassed. This allows it to be referenced as a type parameter at compile time and also makes it available at run time. It is intended to be used as follows:
Generic<List<String>> listStringType = new Generic<List<String>>()
{};
This allows generic type literals to be used in a simple manner as standard class literals. For example, consider the following generic method signature:
<T> void add(T element, Class<T> type)
A problem arises when <T>
is a generic type, such as List<String>
, since List<String>.class
produces a compile time error. Use of this class can mitigate this problem:
<T> void add(T element, Generic<T> type)
Which can then be invoked as follows:
add(new ArrayList<String>(), new Generic<List<String>>()
{});
Generics
,
Neal Gafter's blog: Super Type Tokens,
Serialized FormModifier | Constructor and Description |
---|---|
protected |
Generic() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object object) |
static <T> Generic<T> |
get(Class<T> klass) |
static <T> Generic<? extends T> |
get(Class<T> rawType,
Type... actualTypeArguments) |
static Generic<?> |
get(Type type) |
Class<? super T> |
getRawType() |
Type |
getType() |
int |
hashCode() |
String |
toString() |
String |
toUnqualifiedString() |
static Generic<?> |
valueOf(String typeName) |
Copyright © 2008–2019. All rights reserved.