public abstract class Type extends java.lang.Object implements java.lang.Comparable<Type>
Type: ReferenceType PrimitiveType
The subclasses of this Type
class should be used to represent types in Randoop test
generation rather than the reflection types. Using reflection, each Java type has a Class<?>
object, including primitive types. But, things get a little complicated for generic and
parameterized types, where the Class
object represents the raw type of the generic class,
but also carries the type parameters of the generic class. More information about types is
available through the subinterfaces of java.lang.reflect.Type
, but working with generic and parameterized types is still awkward.
This is in part because the correspondence to the JLS is unclear, but also because the provided
methods do not implement all of the algorithms needed to work with types and type hierarchies as
needed in Randoop. Effectively, the concrete subclasses of this class are facades for these
reflective types, but they are identified with the definitions in the JLS, and provide the
methods needed to test for assignability and test for subtypes.
Type
objects are constructed using the methods forType(java.lang.reflect.Type)
, forClass(Class)
, or forName(String)
. These
methods translate the reflection types into objects of subclasses of this type.
Constructor and Description |
---|
Type() |
Modifier and Type | Method and Description |
---|---|
Type |
applyCaptureConversion()
Applies a capture conversion to this type.
|
int |
compareTo(Type type)
Compare this
Type to another. |
static Type |
forClass(java.lang.Class<?> classType)
Translates a
Class into a Type object. |
static java.lang.Class<?> |
forFullyQualifiedName(@FqBinaryName java.lang.String fullyQualifiedName)
Returns the Class for a fully-qualified name (that may or may not be a multi-dimensional
array).
|
private static java.lang.Class<?> |
forFullyQualifiedNameNonArray(@ClassGetName java.lang.String fullyQualifiedName)
Returns the Class for a class name.
|
static Type |
forName(@ClassGetName java.lang.String typeName)
Returns a
Type object for the given type name in Class.getName format. |
static Type |
forType(java.lang.reflect.Type type)
Returns a type constructed from the object referenced by a
java.lang.reflect.Type
reference. |
static Type |
forValue(java.lang.Object value)
Returns the type for the given Object reference.
|
abstract java.lang.String |
getBinaryName()
Returns the binary name of this type, including type arguments if this is a parameterized type
(so, it isn't really a binary name).
|
@Nullable java.lang.String |
getCanonicalName()
Returns the name of this type as the "canonical name" of the underlying runtime class.
|
abstract java.lang.String |
getFqName()
Returns the fully-qualified name of this type, including type arguments if this is a
parameterized type.
|
Type |
getRawtype()
Returns the raw type for this type, which is this type except for generic types.
|
abstract java.lang.Class<?> |
getRuntimeClass()
Returns the runtime
Class object for this type. |
abstract java.lang.String |
getSimpleName()
Returns the name of this type without type arguments or package qualifiers.
|
static Type |
getTypeforFullyQualifiedName(@FqBinaryName java.lang.String fullyQualifiedName)
Returns the Type for a fully-qualified name (that may or may not be a multi-dimensional array).
|
java.lang.String |
getUnqualifiedBinaryName()
Returns the name of this type without package name, but with type arguments if parameterized,
and enclosing class if a member class.
|
boolean |
hasCaptureVariable()
Indicates whether this
ReferenceType has a capture variable. |
boolean |
hasWildcard()
Indicates whether this type has a wildcard anywhere within it.
|
boolean |
isArray()
Indicates whether this object represents an array type.
|
boolean |
isAssignableFrom(Type sourceType)
Indicates whether there is an assignment conversion from a source
Type to this type. |
<T> boolean |
isAssignableFromTypeOf(T value)
Indicates whether there is an assignment conversion from the type of
value to this
type. |
boolean |
isBoxedPrimitive()
Indicates whether this is a boxed primitive type.
|
boolean |
isClass()
Indicates whether this type is the Class type.
|
boolean |
isClassOrInterfaceType()
Indicate whether this type is a class or interface type.
|
boolean |
isEnum()
Indicates whether this is an enum type.
|
boolean |
isGeneric()
Indicate whether this type is generic.
|
boolean |
isGeneric(boolean ignoreWildcards)
Indicate whether this type is generic.
|
boolean |
isInterface()
Indicates whether this object is an interface type.
|
boolean |
isNonreceiverType()
Indicates whether this is the type of a non-receiver term: primitive, boxed primitive,
String , or Class . |
boolean |
isObject()
Indicate whether this is the
Object type. |
boolean |
isParameterized()
Indicate whether this type is a parameterized type.
|
boolean |
isPrimitive()
Indicates whether this is a primitive type.
|
boolean |
isRawtype()
Indicate whether this type is a rawtype of a generic class.
|
boolean |
isReferenceType()
Indicates whether this is a reference type.
|
boolean |
isString()
Indicates whether this type is the String type.
|
boolean |
isSubtypeOf(Type otherType)
Test whether this type is a subtype of the given type according to transitive closure of
definition of the direct supertype relation in section 4.10 of
JLS for Java SE 8.
|
boolean |
isVariable()
Indicates whether this type is a type variable.
|
boolean |
isVoid()
Indicate whether this type is void.
|
private static int |
numDimensions(@FqBinaryName java.lang.String typeName)
Return the number of dimensions in the given type, which might or might not be an array.
|
boolean |
runtimeClassIs(java.lang.Class<?> c)
Indicates whether the given
Class<?> object is the runtime class of this type. |
Type |
substitute(Substitution substitution)
Returns the type created by instantiating the type parameters of this type with
ReferenceType objects. |
java.lang.String |
toString() |
public static Type forClass(java.lang.Class<?> classType)
Class
into a Type
object. For primitive types, creates a PrimitiveType
object. For reference types, delegates to ReferenceType.forClass(Class)
.classType
- the Class
object for the typeType
object for the given reflection typepublic static Type forName(@ClassGetName java.lang.String typeName) throws java.lang.ClassNotFoundException
Type
object for the given type name in Class.getName
format. Uses reflection to find the corresponding type.
Note that no method in Type returns the type name in this format. To get the name in this
format from a Type
object t
, use t.getRuntimeClass().getName()
.
typeName
- the name of a typejava.lang.ClassNotFoundException
- if name is not a recognized typepublic static Type getTypeforFullyQualifiedName(@FqBinaryName java.lang.String fullyQualifiedName) throws java.lang.ClassNotFoundException
fullyQualifiedName
- the fully-qualified binary name of a type, which uses "$" rather than
"." to indicate a nested class. Array names such as int[]
or java.lang.String[][]
are also fully-qualified binary names. However, array definitions
with sizes, such as int[3][]
, are not fully-qualified binary names.java.lang.ClassNotFoundException
- if name is not a recognized typepublic static java.lang.Class<?> forFullyQualifiedName(@FqBinaryName java.lang.String fullyQualifiedName) throws java.lang.ClassNotFoundException
fullyQualifiedName
- the fully-qualified binary name of a type, which uses uses "$" rather
than "." to indicate a nested classjava.lang.ClassNotFoundException
- if name is not a recognized typeprivate static int numDimensions(@FqBinaryName java.lang.String typeName)
typeName
- a type name, possibly with some number of trailing "[]"private static java.lang.Class<?> forFullyQualifiedNameNonArray(@ClassGetName java.lang.String fullyQualifiedName) throws java.lang.ClassNotFoundException
fullyQualifiedName
- the fully-qualified binary name of a non-array type, which uses uses
"$" rather than "." to indicate a nested classjava.lang.ClassNotFoundException
- if name is not a recognized typepublic static Type forValue(java.lang.Object value)
value
- the Object valueType
for the given valuepublic static Type forType(java.lang.reflect.Type type)
java.lang.reflect.Type
reference.
Note that when the type corresponds to a generic class type, this method returns the type
variables from the getActualTypeArguments()
method to maintain the guarantees needed for ClassOrInterfaceType.isSubtypeOf(Type)
.
type
- the type to interpretType
object corresponding to the given typejava.lang.IllegalArgumentException
- if the type is a java.lang.reflect.WildcardType
public abstract java.lang.Class<?> getRuntimeClass()
Class
object for this type. For use when reflection is needed.
Note that type variables and the null reference type do not have a runtime class, and this method will return null in those cases.
This method should not be confused with the inherited Object.getClass()
method,
which returns the Class<?>
for the Type
object, and not of the represented
type. For instance, if a Type
object t
represented the Java type int
,
then t.getRuntimeClass()
would return int.class
while t.getClass()
would return Type.class
.
Class
that is the runtime representation of the type, or null if this type
is a type variable or null reference typepublic abstract java.lang.String getFqName()
java.util.List<T>
return "java.util.List<T>"
.public abstract java.lang.String getBinaryName()
java.util.List<T>
return "java.util.List<T>"
.public abstract java.lang.String getSimpleName()
java.util.List<T>
, returns "List"
.public @Nullable java.lang.String getCanonicalName()
getFqName()
except for types with type arguments. For java.util.List<T>
returns "java.util.List"
. Returns null
when Class<?>.getCanonicalName()
does for the underlying Class<?>
object (e.g., the type is
a local or anonymous class, or array type where the component type that has no canonical name).public java.lang.String getUnqualifiedBinaryName()
java.util.List<T>
would return
"List<T>"
.public java.lang.String toString()
toString
in class java.lang.Object
public Type getRawtype()
public boolean runtimeClassIs(java.lang.Class<?> c)
Class<?>
object is the runtime class of this type.c
- the Class<?>
to checkc
is the runtime Class<?>
of this type, false otherwisepublic boolean isArray()
public boolean isBoxedPrimitive()
public boolean isClass()
public boolean isEnum()
public final boolean isGeneric()
public boolean isGeneric(boolean ignoreWildcards)
ignoreWildcards
- if true, ignore wildcards; that is, treat wildcards as not making the
operation genericpublic boolean isInterface()
public boolean isObject()
Object
type.Object
type, false otherwisepublic boolean isString()
public boolean isVoid()
public boolean isParameterized()
C<T1,...,Tk>
that instantiates a generic class C<F1,...,Fk>
.
If inputType.isParameterized returns true, there are two possibilities: inputType
instanceof InstantiatedType
, or inputType is a member class and the enclosing type is a
parameterized type
public boolean hasWildcard()
public boolean hasCaptureVariable()
ReferenceType
has a capture variable.public boolean isPrimitive()
public boolean isNonreceiverType()
String
, or Class
.String
, or Class
public boolean isRawtype()
public boolean isReferenceType()
public boolean isVariable()
public Type substitute(Substitution substitution)
ReferenceType
objects. Simply returns this type if it has no type parameters. In otherwords,
this type is not a ParameterizedType
, which includes GenericClassType
.
There are contexts in which it is necessary to apply a substitution to a Type
and it
is not clear whether the type is parameterized. In particular, this method is defined here
because ArrayType
can hold arbitrary types, including type variables and parameterized
types.
substitution
- the type substitutionType
constructed by substituting for type parameters in this type, or this
type if this is not a generic class typepublic Type applyCaptureConversion()
public boolean isAssignableFrom(Type sourceType)
Type
to this type.
(In other words, a value of the source type can be assigned to an l-value of this type.)
Returns true if this is a legal assignment conversion:
Variablethis = Expressionsourcetype.
Based on the definition of assignment context in section 5.2 of the Java Language Specification, a value of one type is assignable to a variable of another type if the first type can be converted to the second by
sourceType
- the type to test for assignabilitypublic <T> boolean isAssignableFromTypeOf(T value)
value
to this
type. (Note this is equivalent to determining whether value
can be assigned to an
l-value of this type.) If the reference is null, then returns true only if this type is not
primitive.T
- the type of the valuevalue
- the element to checkvalue
is assignable to this type, false otherwisepublic boolean isSubtypeOf(Type otherType)
otherType
- the possible supertypepublic boolean isClassOrInterfaceType()
public int compareTo(Type type)
Type
to another. Uses the names of the underlying Class<?>
objects. Uses canonical names if both have them, otherwise uses Class<?>.getName()
.compareTo
in interface java.lang.Comparable<Type>
type
- the type to compare againsttype
, 1 if this type succeeds type
, and 0 if
they are equal