Class ReferenceType
- java.lang.Object
-
- randoop.types.Type
-
- randoop.types.ReferenceType
-
- All Implemented Interfaces:
Comparable<Type>
- Direct Known Subclasses:
ArrayType
,ClassOrInterfaceType
,NullReferenceType
,ParameterType
public abstract class ReferenceType extends Type
Represents a reference type defined in JLS Section 4.3ReferenceType: ClassOrInterfaceType TypeVariable ArrayType
This abstract type is used to mirror the grammar in the JLS.
-
-
Constructor Summary
Constructors Constructor Description ReferenceType()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ReferenceType
applyCaptureConversion()
Applies a capture conversion to this type.static ReferenceType
forClass(Class<?> classType)
Returns aReferenceType
object for the givenClass
object.static ReferenceType
forType(Type type)
Creates aReferenceType
for the givenjava.lang.reflect.Type
.Substitution
getInstantiatingSubstitution(ReferenceType goalType)
Computes a substitution that can be applied to the type variables of the generic goal type to instantiate operations of this type, possibly inherited from from the goal type.static Substitution
getInstantiatingSubstitutionforTypeVariable(ReferenceType instantiatedType, ReferenceType goalType)
Static helper method that does the work of getInstantiatingSubstitution, if goalType is a type variable.List<TypeVariable>
getTypeParameters()
Returns the list of type parameters for this type.boolean
isAssignableFrom(Type sourceType)
Indicates whether there is an assignment conversion from a sourceType
to this type.boolean
isCaptureVariable()
Indicates whether this type is a capture type variable as constructed byInstantiatedType.applyCaptureConversion()
.boolean
isInstantiationOf(ReferenceType otherType)
Indicates whether this type is an instantiation of a more general type.boolean
isReferenceType()
Indicates whether this is a reference 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.abstract ReferenceType
substitute(Substitution substitution)
Applies a substitution to aReferenceType
.-
Methods inherited from class randoop.types.Type
compareTo, forFullyQualifiedName, forName, forValue, getBinaryName, getCanonicalName, getFqName, getRawtype, getRuntimeClass, getSimpleName, getTypeforFullyQualifiedName, getUnqualifiedBinaryName, hasCaptureVariable, hasWildcard, isArray, isAssignableFromTypeOf, isBoxedPrimitive, isClass, isClassOrInterfaceType, isEnum, isGeneric, isGeneric, isInterface, isNonreceiverType, isObject, isParameterized, isPrimitive, isRawtype, isString, isVariable, isVoid, runtimeClassIs, toString
-
-
-
-
Method Detail
-
forClass
public static ReferenceType forClass(Class<?> classType)
Returns aReferenceType
object for the givenClass
object. Creates arrays, classes, and interfaces. For arrays, callsArrayType.forClass(Class)
. For other (class/interface) types, callsClassOrInterfaceType.forClass(Class)
.- Parameters:
classType
- theClass
object representing the type- Returns:
- the
ReferenceType
object for the given type
-
forType
public static ReferenceType forType(Type type)
Creates aReferenceType
for the givenjava.lang.reflect.Type
. Specifically, creates- an
ArrayType
if the reference isGenericArrayType
or is aClass
object representing an array. - a
TypeVariable
) if the reference isjava.lang.reflect.TypeVariable
. - a
ClassOrInterfaceType
if the reference is none of those.
- Parameters:
type
- the type reference- Returns:
- the
ReferenceType
for the givenType
- an
-
substitute
public abstract ReferenceType substitute(Substitution substitution)
Applies a substitution to aReferenceType
. If the type is parameterized then replaces type variables that occur, otherwise returns this type.This abstract method forces typing of substitutions applied to
ReferenceType
objects without an explicit cast.- Overrides:
substitute
in classType
- Parameters:
substitution
- the type substitution- Returns:
- the type created by applying the substitution to this type
-
applyCaptureConversion
public ReferenceType applyCaptureConversion()
Description copied from class:Type
Applies a capture conversion to this type.- Overrides:
applyCaptureConversion
in classType
- Returns:
- a copy of this type with wildcards replaced by type conversion
-
getTypeParameters
public List<TypeVariable> getTypeParameters()
Returns the list of type parameters for this type. Reference types that may have type parameters includeArrayType
(such asE[]
), and subclasses ofParameterizedType
.- Returns:
- the type parameters for this type
-
isAssignableFrom
public boolean isAssignableFrom(Type sourceType)
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 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.
For assignment to
ReferenceType
, checks for widening reference conversion when the source type is also a reference type. See section JLS 5.1.5 for details.- Overrides:
isAssignableFrom
in classType
- Parameters:
sourceType
- the type to test for assignability- Returns:
- true if this type can be assigned from the source type, and false otherwise
-
isCaptureVariable
public boolean isCaptureVariable()
Indicates whether this type is a capture type variable as constructed byInstantiatedType.applyCaptureConversion()
. A capture type variable can only occur as a type argument in anInstantiatedType
constructed byInstantiatedType.applyCaptureConversion()
.- Returns:
- true if this type is a capture type variable, false otherwise
-
isInstantiationOf
public boolean isInstantiationOf(ReferenceType otherType)
Indicates whether this type is an instantiation of a more general type.For a general
ReferenceType
, this is only true if the other type is the same, or the other type is a type variable for which this type satisfies the bounds. Other cases are handled by the overriding implementationsClassOrInterfaceType.isInstantiationOf(ReferenceType)
,InstantiatedType.isInstantiationOf(ReferenceType)
, andTypeVariable.isInstantiationOf(ReferenceType)
.- Parameters:
otherType
- the general reference type- Returns:
- true if this type instantiates the other reference type, false otherwise
-
getInstantiatingSubstitution
public Substitution getInstantiatingSubstitution(ReferenceType goalType)
Computes a substitution that can be applied to the type variables of the generic goal type to instantiate operations of this type, possibly inherited from from the goal type. The substitution will unify this type or a supertype of this type with the given goal type.If there is no unifying substitution, returns
null
.- Parameters:
goalType
- the generic type for which a substitution is needed- Returns:
- a substitution unifying this type or a supertype of this type with the goal type
-
getInstantiatingSubstitutionforTypeVariable
public static Substitution getInstantiatingSubstitutionforTypeVariable(ReferenceType instantiatedType, ReferenceType goalType)
Static helper method that does the work of getInstantiatingSubstitution, if goalType is a type variable.- Parameters:
instantiatedType
- the first typegoalType
- the generic type for which a substitution is needed- Returns:
- a substitution unifying this first type or a supertype of the first type with the goal type
-
isReferenceType
public boolean isReferenceType()
Description copied from class:Type
Indicates whether this is a reference type. Note: implementing classes should ensure that this is equivalent to !(this.isPrimitive())- Overrides:
isReferenceType
in classType
- Returns:
- true if this type is a reference type, and false otherwise
-
isSubtypeOf
public 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.For
ReferenceType
, returns true ifotherType
isObject
.- Overrides:
isSubtypeOf
in classType
- Parameters:
otherType
- the possible supertype- Returns:
- true if this type is a subtype of the given type, false otherwise
-
-