public final class Type<T> extends Object implements Serializable
Basic class of the type system. A Type object representes a Java type including its type parameters (if it has any),
as specified by the TypeDef
corresponding to the type's component class.
Every possible Java type can be represented by a Type object. Some examples:
Objects of this class are never created directly. To obtain a Type object,
the diverse methods in the Types
class should be used.
Objects of this class are immutable, and thus thread-safe. Also, in order to avoid excessive memory usage, an internal synchronized cache exists which prevents the same Type from being instantiated more than once (so, if two Type objects are equal, this will mean that they are the same object).
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
Set<Type<?>> |
getAllTypesAssignableFromThis()
Returns a set of the types corresponding to all the interfaces and
superclasses that this type implements or extends.
|
int |
getArrayDimensions()
Returns the type's array dimensions.
|
Class<?> |
getComponentClass()
Returns the type's component class.
|
String |
getName()
Returns the type's name.
|
Class<? super T> |
getRawClass()
Returns the type's raw equivalent class representation.
|
Type<?> |
getRawEquivalent()
Returns a type corresponding with the one on which this method is called, but substituting all its
type parameters by "unknown".
|
String |
getSimpleName()
Returns the type's name excluding package.
|
List<TypeParameter<?>> |
getTypeParameters()
Returns the type parameters of the type.
|
int |
hashCode() |
boolean |
isAbstract()
Returns whether the type is abstract or not.
|
boolean |
isArray()
Returns whether the type is an array or not.
|
boolean |
isAssignableFrom(Type<?> type)
Returns whether type type is assignable from the type that is passed
as a parameter.
|
boolean |
isInterface()
Returns whether the type represents an interface or not.
|
boolean |
isRaw()
Returns whether this type would be considered "raw".
|
Object |
newInstance()
Creates a new instance of the type.
|
String |
toString() |
public Class<?> getComponentClass()
Returns the type's component class. For instance, it will return List.class
for "List<String>[]" (and not List[].class, which would be returned by
getRawClass()
).
public int getArrayDimensions()
Returns the type's array dimensions. For instance, it will return 1 for "List<String>[]"
public boolean isArray()
Returns whether the type is an array or not. For instance, it will return "true" for "List<String>[]".
public boolean isInterface()
Returns whether the type represents an interface or not. For instance, it will return "false" for "List<String>[]" as, although List.class is an interface, List[].class is a instantiable class. On the contrary, "List<String>" would return "true".
public boolean isAbstract()
Returns whether the type is abstract or not. For instance, it will return "false" for "AbstractList<String>[]" as, although AbstractList.class is abstract, AbstractList[] is an instantiable class. On the contrary, "AbstractList<String>" would return "true".
public List<TypeParameter<?>> getTypeParameters()
Returns the type parameters of the type.
public String getName()
Returns the type's name.
public String getSimpleName()
Returns the type's name excluding package.
public Class<? super T> getRawClass()
Returns the type's raw equivalent class representation. For instance,
if the type is "List<String>[]", this method will return
List[].class (the class representing the raw version
of the type), in contrast with getComponentClass()
which
would return List.class (the component class).
public boolean isAssignableFrom(Type<?> type)
Returns whether type type is assignable from the type that is passed as a parameter.
Assignability is computed in the following terms: a type A is considered to be assignable from another type B if a method declared as receiving a parameter of type A can be called with an object of type B as a parameter.
type
- the type on which assignability is going to be testedpublic Object newInstance() throws InstantiationException, IllegalAccessException
Creates a new instance of the type.
The returned instances are created using the default (no-arg) constructor if this Type does not represent an array. On the contrary, if this Type represents an array, an array object of zero-size dimensions is returned (like "String[0][0][0]").
InstantiationException
- if the type cannot be instantiatedIllegalAccessException
- if the type cannot be instantiatedpublic Set<Type<?>> getAllTypesAssignableFromThis()
Returns a set of the types corresponding to all the interfaces and superclasses that this type implements or extends.
For instance, for "List<String>", this method will return "Collection<String>", "Iterable<String>" and "Object".
public Type<?> getRawEquivalent()
Returns a type corresponding with the one on which this method is called, but substituting all its type parameters by "unknown". For instance: "List<String>" -> "List<?>".
public boolean isRaw()
Returns whether this type would be considered "raw". A type is raw if the result
of calling getRawEquivalent()
on it is the type itself.
Copyright © 2016 The JAVARUNTYPE team. All Rights Reserved.