T - the generic type parameterpublic class GenericType<T> extends Object implements Type
GenericType. Alternatively, an object
representing a concrete parameterized type can be created using a
create(Type) and manually specifying
the type() 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 | Constructor and Description |
|---|---|
protected |
GenericType()
Constructs a new generic type, deriving the generic type and class from
type parameter.
|
| Modifier and Type | Method and Description |
|---|---|
T |
cast(Object object)
Casts the parameter to the type of this generic type.
|
static <N> GenericType<N> |
create(Type genericType)
Constructs a new generic type, using the provided generic type information and
deriving the class.
|
boolean |
equals(Object obj) |
String |
getTypeName() |
int |
hashCode() |
boolean |
isClass()
Whether this generic type represents a simple class with no generic information.
|
Class<?> |
rawType()
Returns the object representing the class or interface that declared
the type represented by this generic type instance.
|
Type |
type()
The type represented by this generic type instance.
|
protected GenericType()
throws IllegalArgumentException
IllegalArgumentException - in case the generic type parameter value is not
provided by any of the subclasses.public static <N> GenericType<N> create(Type genericType) throws IllegalArgumentException
N - generic type of the returned GenericTypegenericType - the generic typeIllegalArgumentException - if genericType is null or not an instance of
Class or ParameterizedType whose raw
type is an instance of Class.public Type type()
For new GenericType<List<String>>(){}, this would return a ParameterizedType
for java.util.List<java.lang.String>.
public Class<?> rawType()
For new GenericType<List<String>>(){}, this would return an
interface java.util.List.
public boolean isClass()
List, false for List<String>public T cast(Object object) throws ClassCastException
object - instance to castClassCastException - in case the object is not of the expected typepublic String getTypeName()
getTypeName in interface TypeCopyright © 2018–2019 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms.