public abstract class ClassType<T> extends ReferenceType<T>
ClassOrInterfaceType
, but that would be too long. )
A ClassType may represent a parameterized types, e.g. Map<String,Integer>
.
A ClassType can be created by one of the static factory methods, for example
ClassType< Map<String,Integer> > type = ClassType.of(Map.class, String.class, Integer.class);
or as "type literal" by anonymous subclass , for example
new ClassType< List<String> > (){}
Modifier and Type | Field and Description |
---|---|
static ClassType<Object> |
OBJECT
The
java.lang.Object type. |
Modifier | Constructor and Description |
---|---|
protected |
ClassType()
For anonymous subclass to create type literal.
|
Instance Methods | |
---|---|
Class<T> |
getTheClass()
Get the class or interface of this type.
|
List<TypeArg> |
getTypeArgs()
Get the type arguments.
|
List<TypeVar<?>> |
getTypeVars()
Get the type variables of the class/interface definition.
|
boolean |
isRawType()
Whether this is a raw type.
|
boolean |
hasWildcard()
Whether any type argument is a wildcard.
|
int |
hashCode() |
boolean |
equals(Object obj)
Whether this type is equal to another type.
|
String |
toString(boolean full)
A textual description of the type.
|
Static Methods | |
<C,T extends C> ClassType<T> |
of(Class<C> clazz,
TypeArg... typeArgs)
Create a ClassType with the class and type arguments.
|
<C,T extends C> ClassType<T> |
of(Class<C> clazz,
Class<?>... typeArgs)
Create a ClassType with the class and type arguments.
|
<T> ClassType<T> |
of(Class<T> clazz)
Create a ClassType representing the class.
|
<C> ClassType<? extends C> |
withTypeVars(Class<C> clazz)
Create a ClassType, using type variables as type arguments.
|
convertFrom, toString
protected ClassType()
This protected constructor is only intended to be used to create "type literal" by anonymous subclass. For example
new ClassType< List<String> > (){}
public static <C,T extends C> ClassType<T> of(Class<C> clazz, TypeArg... typeArgs)
If `clazz` is generic, and `typeArgs` is empty, this method creates a raw type.
public static <C,T extends C> ClassType<T> of(Class<C> clazz, Class<?>... typeArgs)
This is a convenience method for
of(Class, TypeArg...)
by converting each Class
to a TypeArg
.
Example
ClassType<Map<String,Integer>> type = ClassType.of(Map.class, String.class, Integer.class);
public static <T> ClassType<T> of(Class<T> clazz)
ClassType<String> type = ClassType.of(String.class);
If `clazz` is generic, this method creates a raw type.
public static <C> ClassType<? extends C> withTypeVars(Class<C> clazz)
For example,
ClassType.withTypeVars(java.util.Map.class)
returns a
ClassType<K,V>
where K,V
are type variables defined on
java.util.Map<K,V>
.
public final Class<T> getTheClass()
For example, if this ClassType represents List<String>
,
this method will return List.class
.
Strictly speaking, the return type of this method should be
Class<|T|>
where |T|
is the erasure of T
.
However, we cannot express that constraint precisely.
Therefore the return type might be for example Class<List<String>>
which technically does not exist.
public final List<TypeArg> getTypeArgs()
For example, if this ClassType represents
Map<String,Integer>
,
this method returns [String,Integer]
.
Return an empty list if this is not a parameterized type, either because the class/interface is non-generic, or this is a raw type.
public final List<TypeVar<?>> getTypeVars()
For example, if getTheClass()
is java.util.Map.class
,
this method returns [K,V]
as they are declared on Map
.
Return an empty list if the class/interface is not generic.
public final boolean isRawType()
If the class/interface is generic, but there is no type arguments for this ClassType, this is a raw type.
public final boolean hasWildcard()
public final boolean equals(Object obj)
A `ClassType` is only equal to another `ClassType`
with the same class
and the same type arguments
.
public final String toString(boolean full)
JavaType
If `full==false`, simpler names are used for types.
For example:
ClassType< List<String> > type = ClassType.of(List.class, String.class); type.toString(true); // "java.util.List<java.lang.String>" type.toString(false); // "List<String>"