T
- the generic type parameter.public class GenericType<T>
extends java.lang.Object
T
.
Supports in-line instantiation of objects that represent generic types with
actual type parameters. An object that represents any parameterized type may
be obtained by sub-classing GenericType
. Alternatively, an object
representing a concrete parameterized type can be created using a
GenericType(java.lang.reflect.Type)
and manually specifying
the actual (parameterized) type
.
For example:
GenericType<List<String>> stringListType = new GenericType<List<String>>() {};
Or:
public class MyGenericType extends GenericType<List<String>> { ... } ... MyGenericType stringListType = new MyGenericType();
Note that due to the Java type erasure limitations the parameterized type information
must be specified on a subclass, not just during the instance creation. For example,
the following case would throw an IllegalArgumentException
:
public class MyGenericType<T> extends GenericType<T> { ... } ... // The type is only specified on instance, not in a sub-class MyGenericType<List<String>> stringListType = new MyGenericType<List<String>>();
Modifier and Type | Field and Description |
---|---|
private java.lang.Class<?> |
rawType
The actual raw parameter type.
|
private java.lang.reflect.Type |
type
Type represented by the generic type instance.
|
Modifier | Constructor and Description |
---|---|
protected |
GenericType()
Constructs a new generic type, deriving the generic type and class from
type parameter.
|
|
GenericType(java.lang.reflect.Type genericType)
Constructs a new generic type, supplying the generic type
information and deriving the class.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj) |
private static java.lang.Class |
getArrayClass(java.lang.Class c)
Get Array class of component class.
|
private static java.lang.Class |
getClass(java.lang.reflect.Type type)
Returns the object representing the class or interface that declared
the supplied
type . |
java.lang.Class<?> |
getRawType()
Returns the object representing the class or interface that declared
the type represented by this generic type instance.
|
java.lang.reflect.Type |
getType()
Retrieve the type represented by the generic type instance.
|
(package private) static java.lang.reflect.Type |
getTypeArgument(java.lang.Class<?> clazz,
java.lang.Class<?> baseClass)
Return the value of the type parameter of
GenericType<T> . |
int |
hashCode() |
java.lang.String |
toString() |
private final java.lang.reflect.Type type
private final java.lang.Class<?> rawType
protected GenericType()
java.lang.IllegalArgumentException
- in case the generic type parameter value is not
provided by any of the subclasses.public GenericType(java.lang.reflect.Type genericType)
genericType
- the generic type.java.lang.IllegalArgumentException
- if genericType is null
or not an instance of
Class
or ParameterizedType
whose raw
type is an instance of Class
.public final java.lang.reflect.Type getType()
public final java.lang.Class<?> getRawType()
private static java.lang.Class getClass(java.lang.reflect.Type type)
type
.type
- Type
to inspect.type
.private static java.lang.Class getArrayClass(java.lang.Class c)
c
- the component class of the arraystatic java.lang.reflect.Type getTypeArgument(java.lang.Class<?> clazz, java.lang.Class<?> baseClass)
GenericType<T>
.clazz
- subClass of baseClass
to analyze.baseClass
- base class having the type parameter the value of which we need to retrieveGenericType<T>
(aka T)public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object