Class InstantiatedType
- java.lang.Object
-
- randoop.types.Type
-
- randoop.types.ReferenceType
-
- randoop.types.ClassOrInterfaceType
-
- randoop.types.ParameterizedType
-
- randoop.types.InstantiatedType
-
- All Implemented Interfaces:
Comparable<Type>
public class InstantiatedType extends ParameterizedType
Represents a parameterized type as a generic class instantiated with type arguments.Note that
ParameterizedType
is an interface that can represent either a parameterized type in the sense meant here, or a generic class. Conversion to this type fromType
interfaces is handled byType.forType(java.lang.reflect.Type)
.
-
-
Field Summary
Fields Modifier and Type Field Description private List<TypeArgument>
argumentList
The type arguments for this class.private GenericClassType
genericType
The generic class for this type.-
Fields inherited from class randoop.types.ClassOrInterfaceType
enclosingType
-
-
Constructor Summary
Constructors Constructor Description InstantiatedType(GenericClassType genericType, List<TypeArgument> argumentList)
Create a parameterized type from the generic class type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description InstantiatedType
applyCaptureConversion()
Constructs a capture conversion for this type.boolean
equals(Object obj)
GenericClassType
getGenericClassType()
Returns theGenericClassType
for this parameterized 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.List<ClassOrInterfaceType>
getInterfaces()
Constructs the list of interfaces for this parameterized type.InstantiatedType
getMatchingSupertype(GenericClassType goalType)
Finds the parameterized type that is a supertype of this class that also matches the given generic class.NonParameterizedType
getRawtype()
Returns the non-parameterized form of this class type.(package private) List<ReferenceType>
getReferenceArguments()
Returns the list of reference type arguments of this type if there are no wildcards.Class<?>
getRuntimeClass()
Returns the runtimeClass
object for this type.ClassOrInterfaceType
getSuperclass()
Constructs the superclass type for this parameterized type.List<TypeArgument>
getTypeArguments()
Returns the type arguments for this type.List<TypeVariable>
getTypeParameters()
Returns the list of type parameters for this type.Substitution
getTypeSubstitution()
Creates the type substitution of the type arguments of this type for the type variables of the instantiated class, if the type arguments are reference types.boolean
hasCaptureVariable()
Indicates whether thisReferenceType
has a capture variable.int
hashCode()
boolean
hasWildcard()
Indicates whether this type has a wildcard anywhere within it.boolean
isAbstract()
Indicate whether this class is abstract.boolean
isAssignableFrom(Type otherType)
Indicates whether there is an assignment conversion from a sourceType
to this type.boolean
isGeneric(boolean ignoreWildcards)
Indicate whether this type is generic.boolean
isInstantiationOf(ReferenceType otherType)
Checks whether this type is an instantiation of the given instantiated type.boolean
isInterface()
Indicates whether this object is an interface type.boolean
isParameterized()
Indicate whether this type is a parameterized type.boolean
isRecursiveType()
Determines if this type is recursive in the sense that the type is the bound of its type argument.boolean
isStatic()
Indicates whether this class is static.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.2 of JLS for JavaSE 8.InstantiatedType
substitute(Substitution substitution)
Applies a substitution to aReferenceType
.-
Methods inherited from class randoop.types.ParameterizedType
forClass, forType, getBinaryName, getFqName, getUnqualifiedBinaryName
-
Methods inherited from class randoop.types.ClassOrInterfaceType
applyCaptureConversion, getAllSupertypesInclusive, getCanonicalName, getImmediateSupertypes, getPackage, getPackagePrefix, getSimpleName, getSuperTypes, isClassOrInterfaceType, isMemberClass, isNestedClass, substitute
-
Methods inherited from class randoop.types.ReferenceType
getInstantiatingSubstitutionforTypeVariable, isCaptureVariable, isReferenceType
-
Methods inherited from class randoop.types.Type
compareTo, forFullyQualifiedName, forName, forValue, getTypeforFullyQualifiedName, isArray, isAssignableFromTypeOf, isBoxedPrimitive, isClass, isEnum, isGeneric, isNonreceiverType, isObject, isPrimitive, isRawtype, isString, isVariable, isVoid, runtimeClassIs, toString
-
-
-
-
Field Detail
-
genericType
private final GenericClassType genericType
The generic class for this type. Its enclosing type is instantiated (or is not generic).
-
argumentList
private final List<TypeArgument> argumentList
The type arguments for this class.
-
-
Constructor Detail
-
InstantiatedType
InstantiatedType(GenericClassType genericType, List<TypeArgument> argumentList)
Create a parameterized type from the generic class type.- Parameters:
genericType
- the generic class typeargumentList
- the list of type arguments- Throws:
IllegalArgumentException
- if either argument is null
-
-
Method Detail
-
equals
public boolean equals(Object obj)
Test if the given object is equal to this parameterized type. Two parameterized types are equal if they have the same raw type and the same type arguments.
- Overrides:
equals
in classClassOrInterfaceType
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classClassOrInterfaceType
-
substitute
public InstantiatedType substitute(Substitution substitution)
Description copied from class:ClassOrInterfaceType
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.This abstract method allows substitutions to be applied to
ClassOrInterfaceType
objects without casting.- Specified by:
substitute
in classParameterizedType
- Parameters:
substitution
- the type substitution- Returns:
- the type created by applying the substitution to this type
-
applyCaptureConversion
public InstantiatedType applyCaptureConversion()
Constructs a capture conversion for this type. If this type has wildcard type arguments, then introducesCaptureTypeVariable
for each wildcard as described in the JLS, section 5.1.10, Capture Conversion.Based on algorithm in Mads Torgerson et al. "Adding Wildcards to the Java Programming Language", Journal of Object Technology, 3 (December 2004) 11, 97-116. Special Issue: OOPS track at SAC 2004.
If this type has no wildcards, then returns this type.
- Specified by:
applyCaptureConversion
in classClassOrInterfaceType
- Returns:
- the capture conversion type for this type
-
getInterfaces
public List<ClassOrInterfaceType> getInterfaces()
Constructs the list of interfaces for this parameterized type.See the implementation note for
getSuperclass()
.- Specified by:
getInterfaces
in classClassOrInterfaceType
- Returns:
- list of directly-implemented interfaces for this parameterized type
-
getGenericClassType
public GenericClassType getGenericClassType()
Description copied from class:ParameterizedType
Returns theGenericClassType
for this parameterized type.- Specified by:
getGenericClassType
in classParameterizedType
- Returns:
- the generic class type for this type
-
getMatchingSupertype
public InstantiatedType getMatchingSupertype(GenericClassType goalType)
Finds the parameterized type that is a supertype of this class that also matches the given generic class. For example, ifclass C<T> implements Comparator<T>
andclass A extends C<String>
, then when applied toA
, this method would returnC<String>
when givenC<T>
, andComparator<String>
when givenComparator<E>
. Returns null if there is no such type.Performs a depth-first search of the supertype relation for this type. If the goal type is an interface, then searches the interfaces of this type first.
An instantiated type may have a wildcard, and so must perform capture conversion before doing supertype search.
- Overrides:
getMatchingSupertype
in classClassOrInterfaceType
- Parameters:
goalType
- the generic class type- Returns:
- the instantiated type matching the goal type, or null
-
getReferenceArguments
List<ReferenceType> getReferenceArguments()
Returns the list of reference type arguments of this type if there are no wildcards.- Returns:
- the list of reference types that are arguments to this type
-
getRuntimeClass
public Class<?> getRuntimeClass()
Description copied from class:Type
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
.- Specified by:
getRuntimeClass
in classType
- Returns:
- the
Class
that is the runtime representation of the type, or null if this type is a type variable or null reference type
-
getSuperclass
public ClassOrInterfaceType getSuperclass()
Constructs the superclass type for this parameterized type.Implementation note: we can think of an
InstantiatedType
A<T1,...,Tk>
as being represented as a generic classA<F1,...,Fk>
with a substitution[ Fi := Ti]
for all of the type parametersFi
. So, when we compute a superclass, we first find the supertype of the generic classB<F1,...,Fk>
, and then apply the substitution[ Fi := Ti]
using the methodGenericClassType.getSuperclass(Substitution)
.- Specified by:
getSuperclass
in classClassOrInterfaceType
- Returns:
- the superclass type for this parameterized type
-
getTypeArguments
public List<TypeArgument> getTypeArguments()
Returns the type arguments for this type.- Overrides:
getTypeArguments
in classClassOrInterfaceType
- Returns:
- the list of type arguments
-
getTypeParameters
public List<TypeVariable> getTypeParameters()
Description copied from class:ReferenceType
Returns the list of type parameters for this type. Reference types that may have type parameters includeArrayType
(such asE[]
), and subclasses ofParameterizedType
.- Overrides:
getTypeParameters
in classClassOrInterfaceType
- Returns:
- the type parameters for this type
-
getTypeSubstitution
public Substitution getTypeSubstitution()
Creates the type substitution of the type arguments of this type for the type variables of the instantiated class, if the type arguments are reference types. If any type argument is a wildcard, then null is returned.- Returns:
- the type substitution of the type arguments of this class for the type variables of the instantiated type
-
hasWildcard
public boolean hasWildcard()
Description copied from class:Type
Indicates whether this type has a wildcard anywhere within it.- Overrides:
hasWildcard
in classClassOrInterfaceType
- Returns:
- true if this type has a wildcard, false otherwise
-
hasCaptureVariable
public boolean hasCaptureVariable()
Description copied from class:Type
Indicates whether thisReferenceType
has a capture variable.- Overrides:
hasCaptureVariable
in classClassOrInterfaceType
- Returns:
- true iff this type has a capture variable
-
isAbstract
public boolean isAbstract()
Description copied from class:ClassOrInterfaceType
Indicate whether this class is abstract.- Specified by:
isAbstract
in classClassOrInterfaceType
- Returns:
- true if this class is abstract, false otherwise
-
isAssignableFrom
public boolean isAssignableFrom(Type otherType)
Description copied from class:ReferenceType
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 classReferenceType
- Parameters:
otherType
- the type to test for assignability- Returns:
- true if this type can be assigned from the source type, and false otherwise
-
isGeneric
public boolean isGeneric(boolean ignoreWildcards)
Description copied from class:Type
Indicate whether this type is generic. A type is generic if it has one or more type variables.- Overrides:
isGeneric
in classClassOrInterfaceType
- 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
-
isInstantiationOf
public boolean isInstantiationOf(ReferenceType otherType)
Checks whether this type is an instantiation of the given instantiated type. This is only possible if this type isA<T1,...,Tk>
where allTi
are instantiated by ground types (e.g., does not have type variables), the other type isA<S1,...,Sk>
, and eachTi
matchesSi
fori = 1,...,k
as follows:- If
Si
is the variableX
with lower boundL
and upper boundU
, thenTi
is a supertype ofL
and a subtype ofU
Si
is identical toTi
- Overrides:
isInstantiationOf
in classClassOrInterfaceType
- Parameters:
otherType
- the otherInstantiatedType
- Returns:
- true if this type is an instantiation of the other type, false otherwise
- See Also:
ReferenceType.isInstantiationOf(ReferenceType)
- If
-
getInstantiatingSubstitution
public Substitution getInstantiatingSubstitution(ReferenceType goalType)
Description copied from class:ReferenceType
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
.- Overrides:
getInstantiatingSubstitution
in classClassOrInterfaceType
- 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
-
isInterface
public boolean isInterface()
Description copied from class:Type
Indicates whether this object is an interface type.- Overrides:
isInterface
in classType
- Returns:
- true if this object is an interface type, false otherwise
-
isParameterized
public boolean isParameterized()
Description copied from class:Type
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- Overrides:
isParameterized
in classClassOrInterfaceType
- Returns:
- true if this type is a parameterized type, false otherwise
-
isRecursiveType
public boolean isRecursiveType()
Determines if this type is recursive in the sense that the type is the bound of its type argument. So, should have a single type argument that is a subtype of this type.- Returns:
- true if the type argument is a subtype of this type, false otherwise
-
isStatic
public boolean isStatic()
Description copied from class:ClassOrInterfaceType
Indicates whether this class is static.- Specified by:
isStatic
in classClassOrInterfaceType
- Returns:
- true if this class is static, 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.2 of JLS for JavaSE 8.Handles specific cases of supertypes of a parameterized type
C<T1,...,Tn>
instantiating the generic typeC<F1,...,Fn>
by substitution θ=[F1:=T1,...,Fn:=Tn]
for which direct supertypes are:D<U1
θ,...,Uk
θ>
whereD<U1,...,Uk>
is a supertype ofC<F1,...,Fn>
.C<S1,...,Sn>
where Si contains Ti (JLS section 4.5.1).- The rawtype
C
. Object
if generic form is interface with no interfaces as supertypes.
- Overrides:
isSubtypeOf
in classClassOrInterfaceType
- Parameters:
otherType
- the possible supertype- Returns:
- true if this type is a subtype of the given type, false otherwise
- See Also:
ReferenceType.isAssignableFrom(Type)
,ClassOrInterfaceType.isSubtypeOf(Type)
-
getRawtype
public NonParameterizedType getRawtype()
Description copied from class:ClassOrInterfaceType
Returns the non-parameterized form of this class type.- Specified by:
getRawtype
in classClassOrInterfaceType
- Returns:
- the non-parameterized form of this class type
-
-