Class Type
- All Implemented Interfaces:
Comparable<Type>
- Direct Known Subclasses:
PrimitiveType
,ReferenceType
,VoidType
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionApplies a capture conversion to this type.int
Compare thisType
to another.static Type
Translates aClass
into aType
object.static Class<?>
forFullyQualifiedName
(@FqBinaryName String fullyQualifiedName) Returns the Class for a fully-qualified name (that may or may not be a multi-dimensional array).private static Class<?>
forFullyQualifiedNameNonArray
(@ClassGetName String fullyQualifiedName) Returns the Class for a class name.static Type
Returns aType
object for the given type name inClass.getName
format.static Type
Returns a type constructed from the object referenced by ajava.lang.reflect.Type
reference.static Type
Returns the type for the given Object reference.abstract String
Returns the binary name of this type, including type arguments if this is a parameterized type (so, it isn't really a binary name).Returns the name of this type as the "canonical name" of the underlying runtime class.abstract String
Returns the fully-qualified name of this type, including type arguments if this is a parameterized type.Returns the raw type for this type, which is this type except for generic types.abstract Class<?>
Returns the runtimeClass
object for this type.abstract String
Returns the name of this type without type arguments or package qualifiers.static Type
getTypeforFullyQualifiedName
(@FqBinaryName String fullyQualifiedName) Returns the Type for a fully-qualified name (that may or may not be a multi-dimensional array).Returns the name of this type without package name, but with type arguments if parameterized, and enclosing class if a member class.boolean
Indicates whether thisReferenceType
has a capture variable.boolean
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 sourceType
to this type.<T> boolean
isAssignableFromTypeOf
(T value) Indicates whether there is an assignment conversion from the type ofvalue
to this type.boolean
Indicates whether this is a boxed primitive type.boolean
isClass()
Indicates whether this type is the Class type.boolean
Indicate whether this type is a class or interface type.boolean
isEnum()
Indicates whether this is an enum type.final boolean
Indicate whether this type is generic.boolean
isGeneric
(boolean ignoreWildcards) Indicate whether this type is generic.boolean
Indicates whether this object is an interface type.boolean
Indicates whether this is the type of a non-receiver term: primitive, boxed primitive,String
, orClass
.boolean
isObject()
Indicate whether this is theObject
type.boolean
Indicate whether this type is a parameterized type.boolean
Indicates whether this is a primitive type.boolean
Indicate whether this type is a rawtype of a generic class.boolean
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
Indicates whether this type is a type variable.boolean
isVoid()
Indicate whether this type is void.private static int
numDimensions
(@FqBinaryName String typeName) Return the number of dimensions in the given type, which might or might not be an array.boolean
runtimeClassIs
(Class<?> c) Indicates whether the givenClass<?>
object is the runtime class of this type.substitute
(Substitution substitution) Returns the type created by instantiating the type parameters of this type withReferenceType
objects.toString()
-
Constructor Details
-
Type
public Type()
-
-
Method Details
-
forClass
Translates aClass
into aType
object. For primitive types, creates aPrimitiveType
object. For reference types, delegates toReferenceType.forClass(Class)
.- Parameters:
classType
- theClass
object for the type- Returns:
- the
Type
object for the given reflection type
-
forName
Returns aType
object for the given type name inClass.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
objectt
, uset.getRuntimeClass().getName()
.- Parameters:
typeName
- the name of a type- Returns:
- the type object for the type with the name
- Throws:
ClassNotFoundException
- if name is not a recognized type
-
getTypeforFullyQualifiedName
public static Type getTypeforFullyQualifiedName(@FqBinaryName String fullyQualifiedName) throws ClassNotFoundException Returns the Type for a fully-qualified name (that may or may not be a multi-dimensional array).- Parameters:
fullyQualifiedName
- the fully-qualified binary name of a type, which uses "$" rather than "." to indicate a nested class. Array names such asint[]
orjava.lang.String[][]
are also fully-qualified binary names. However, array definitions with sizes, such asint[3][]
, are not fully-qualified binary names.- Returns:
- the type object for the type with the name
- Throws:
ClassNotFoundException
- if name is not a recognized type
-
forFullyQualifiedName
public static Class<?> forFullyQualifiedName(@FqBinaryName String fullyQualifiedName) throws ClassNotFoundException Returns the Class for a fully-qualified name (that may or may not be a multi-dimensional array).- Parameters:
fullyQualifiedName
- the fully-qualified binary name of a type, which uses uses "$" rather than "." to indicate a nested class- Returns:
- the type object for the type with the name
- Throws:
ClassNotFoundException
- if name is not a recognized type
-
numDimensions
Return the number of dimensions in the given type, which might or might not be an array.- Parameters:
typeName
- a type name, possibly with some number of trailing "[]"- Returns:
- the number of trailing "[]"
-
forFullyQualifiedNameNonArray
private static Class<?> forFullyQualifiedNameNonArray(@ClassGetName String fullyQualifiedName) throws ClassNotFoundException Returns the Class for a class name. Does not support arrays.- Parameters:
fullyQualifiedName
- the fully-qualified binary name of a non-array type, which uses uses "$" rather than "." to indicate a nested class- Returns:
- the type object for the type with the name
- Throws:
ClassNotFoundException
- if name is not a recognized type
-
forValue
Returns the type for the given Object reference.- Parameters:
value
- the Object value- Returns:
- the
Type
for the given value
-
forType
Returns a type constructed from the object referenced by ajava.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 forClassOrInterfaceType.isSubtypeOf(Type)
.- Parameters:
type
- the type to interpret- Returns:
- a
Type
object corresponding to the given type - Throws:
IllegalArgumentException
- if the type is ajava.lang.reflect.WildcardType
-
getRuntimeClass
Returns the runtimeClass
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 theClass<?>
for theType
object, and not of the represented type. For instance, if aType
objectt
represented the Java typeint
, thent.getRuntimeClass()
would returnint.class
whilet.getClass()
would returnType.class
.- Returns:
- the
Class
that is the runtime representation of the type, or null if this type is a type variable or null reference type
-
getFqName
Returns the fully-qualified name of this type, including type arguments if this is a parameterized type. Forjava.util.List<T>
return"java.util.List<T>"
.- Returns:
- the fully-qualified type name for this type
-
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). Forjava.util.List<T>
return"java.util.List<T>"
.- Returns:
- the binary name for this type
-
getSimpleName
Returns the name of this type without type arguments or package qualifiers. Forjava.util.List<T>
, returns"List"
.- Returns:
- the name of this type without type arguments
-
getCanonicalName
Returns the name of this type as the "canonical name" of the underlying runtime class. Identical togetFqName()
except for types with type arguments. Forjava.util.List<T>
returns"java.util.List"
. Returnsnull
whenClass<?>.getCanonicalName()
does for the underlyingClass<?>
object (e.g., the type is a local or anonymous class, or array type where the component type that has no canonical name).- Returns:
- the fully-qualified canonical name of this type
-
getUnqualifiedBinaryName
Returns the name of this type without package name, but with type arguments if parameterized, and enclosing class if a member class. For instance, forjava.util.List<T>
would return"List<T>"
.- Returns:
- the unqualified name of this type
-
toString
-
getRawtype
Returns the raw type for this type, which is this type except for generic types.- Returns:
- the raw type corresponding to this type
-
runtimeClassIs
Indicates whether the givenClass<?>
object is the runtime class of this type.- Parameters:
c
- theClass<?>
to check- Returns:
- true if
c
is the runtimeClass<?>
of this type, false otherwise
-
isArray
public boolean isArray()Indicates whether this object represents an array type.- Returns:
- true if this object represents an array type, false otherwise
-
isBoxedPrimitive
public boolean isBoxedPrimitive()Indicates whether this is a boxed primitive type.- Returns:
- true if this type is a boxed primitive, false otherwise
-
isClass
public boolean isClass()Indicates whether this type is the Class type.- Returns:
- true if this type is the Class type, and false otherwise
-
isEnum
public boolean isEnum()Indicates whether this is an enum type.- Returns:
- true if this is an enum type, false otherwise
-
isGeneric
public final boolean isGeneric()Indicate whether this type is generic. A type is generic if it has one or more type variables.- Returns:
- true if this type is generic, false otherwise
-
isGeneric
public boolean isGeneric(boolean ignoreWildcards) Indicate whether this type is generic. A type is generic if it has one or more type variables.- Parameters:
ignoreWildcards
- if true, ignore wildcards; that is, treat wildcards as not making the operation generic- Returns:
- true if this type is generic, false otherwise
-
isInterface
public boolean isInterface()Indicates whether this object is an interface type.- Returns:
- true if this object is an interface type, false otherwise
-
isObject
public boolean isObject()Indicate whether this is theObject
type.- Returns:
- true if this is the
Object
type, false otherwise
-
isString
public boolean isString()Indicates whether this type is the String type.- Returns:
- true if this type is the String type, and false otherwise
-
isVoid
public boolean isVoid()Indicate whether this type is void.- Returns:
- true if this type is void, false otherwise
-
isParameterized
public boolean isParameterized()Indicate whether this type is a parameterized type. A parameterized type is a typeC<T1,...,Tk>
that instantiates a generic classC<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- Returns:
- true if this type is a parameterized type, false otherwise
-
hasWildcard
public boolean hasWildcard()Indicates whether this type has a wildcard anywhere within it.- Returns:
- true if this type has a wildcard, false otherwise
-
hasCaptureVariable
public boolean hasCaptureVariable()Indicates whether thisReferenceType
has a capture variable.- Returns:
- true iff this type has a capture variable
-
isPrimitive
public boolean isPrimitive()Indicates whether this is a primitive type.- Returns:
- true if this type is primitive, false otherwise
-
isNonreceiverType
public boolean isNonreceiverType()Indicates whether this is the type of a non-receiver term: primitive, boxed primitive,String
, orClass
.- Returns:
- true iff this type is primitive, boxed primitive,
String
, orClass
-
isRawtype
public boolean isRawtype()Indicate whether this type is a rawtype of a generic class. The rawtype is the runtime type of the class that has type parameters erased.- Returns:
- true if this type is a rawtype of a generic class, false otherwise
-
isReferenceType
public boolean isReferenceType()Indicates whether this is a reference type. Note: implementing classes should ensure that this is equivalent to !(this.isPrimitive())- Returns:
- true if this type is a reference type, and false otherwise
-
isVariable
public boolean isVariable()Indicates whether this type is a type variable.- Returns:
- true if this type is a type variable, false otherwise
-
substitute
Returns the type created by instantiating the type parameters of this type withReferenceType
objects. Simply returns this type if it has no type parameters. In otherwords, this type is not aParameterizedType
, which includesGenericClassType
.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 becauseArrayType
can hold arbitrary types, including type variables and parameterized types.- Parameters:
substitution
- the type substitution- Returns:
- the
Type
constructed by substituting for type parameters in this type, or this type if this is not a generic class type
-
applyCaptureConversion
Applies a capture conversion to this type.- Returns:
- a copy of this type with wildcards replaced by type conversion
-
isAssignableFrom
Indicates whether there is an assignment conversion from a sourceType
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 JDK 8 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
- an identity conversion (section 5.1.1),
- a widening primitive conversion (section 5.1.2),
- a widening reference conversion (section 5.1.5),
- a boxing conversion (5.1.7), or
- an unboxing conversion (section 5.1.8) possibly followed by a widening conversion.
- Parameters:
sourceType
- the type to test for assignability- Returns:
- true if this type can be assigned from the source type, and false otherwise
-
isAssignableFromTypeOf
public <T> boolean isAssignableFromTypeOf(T value) Indicates whether there is an assignment conversion from the type ofvalue
to this type. (Note this is equivalent to determining whethervalue
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.- Type Parameters:
T
- the type of the value- Parameters:
value
- the element to check- Returns:
- true if the type of
value
is assignable to this type, false otherwise
-
isSubtypeOf
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.- Parameters:
otherType
- the possible supertype- Returns:
- true if this type is a subtype of the given type, false otherwise
-
isClassOrInterfaceType
public boolean isClassOrInterfaceType()Indicate whether this type is a class or interface type.- Returns:
- true if this type is a class or interface type; false, otherwise
-
compareTo
Compare thisType
to another. Uses the names of the underlyingClass<?>
objects. Uses canonical names if both have them, otherwise usesClass<?>.getName()
.- Specified by:
compareTo
in interfaceComparable<Type>
- Parameters:
type
- the type to compare against- Returns:
- -1 if this type precedes
type
, 1 if this type succeedstype
, and 0 if they are equal.
-