Package randoop.reflection
Class TypeInstantiator
- java.lang.Object
-
- randoop.reflection.TypeInstantiator
-
public class TypeInstantiator extends Object
Instantiates type parameters from a set of input types.
-
-
Field Summary
Fields Modifier and Type Field Description private static boolean
debug
Whether to log information about instantiation.private Set<Type>
inputTypes
The set of input types for this model.
-
Constructor Summary
Constructors Constructor Description TypeInstantiator(Set<Type> inputTypes)
Creates aTypeInstantiator
object using the given types to construct instantiating substitutions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private List<Substitution>
allExtendingSubstitutions(List<TypeVariable> typeParameters, Substitution substitution)
Returns all substitutions that extend a substitution for the given type parameters.private List<Substitution>
allSubstitutions(List<TypeVariable> parameters, Substitution initialSubstitution, BoundsCheck boundsCheck)
Adds instantiating substitutions for the given parameters to the list if satisfies the given type check predicate.private List<List<ReferenceType>>
candidateTypes(List<TypeVariable> parameters)
Constructs the list of lists of candidate types for the given type parameters.private List<ReferenceType>
candidateTypes(TypeVariable argument)
Returns all input types that potentially satisfy the bounds on the argument.private ParameterBound
getLowerBound(TypeVariable argument)
Gets the lower bound of the given argument to be tested incandidateTypes(TypeVariable)
.private ParameterBound
getUpperBound(TypeVariable argument)
Gets the upper bound of the given argument to test incandidateTypes(TypeVariable)
.TypedClassOperation
instantiate(TypedClassOperation operation)
Instantiate the given operation by choosing type arguments for its type parameters.private Substitution
instantiateClass(ClassOrInterfaceType type)
Chooses an instantiating substitution for the given class.private TypedClassOperation
instantiateOperationTypes(TypedClassOperation operation)
Selects an instantiation of the generic types of an operation, and returns a new operation with the types instantiated.private Substitution
instantiateSortedSetType(TypedClassOperation operation)
Returns a substitution that instantiates theSortedSet
type of the given constructor.private Substitution
selectSubstitution(List<TypeVariable> typeParameters)
Selects an instantiating substitution for the given list of type variables.private Substitution
selectSubstitution(List<TypeVariable> typeParameters, Substitution substitution)
Extends the given substitution by instantiations for the given list of type variables.private Substitution
selectSubstitution(ClassOrInterfaceType type)
Selects an existing type that instantiates the given generic declaring type and returns the instantiating substitution.private Substitution
selectSubstitution(ClassOrInterfaceType type, ClassOrInterfaceType patternType)
Returns a substitution that instantiates bothtype
andpatternType
to an existing type.private Substitution
selectSubstitutionForSortedSet(GenericClassType searchType, TypeVariable typeParameter)
Select a substitution for the type parameter of SortedSet.private Substitution
selectSubstitutionIndependently(List<TypeVariable> parameters, Substitution substitution)
Selects types independently for a list of type parameters, and extends the given substitution by the substitution of the selected types for the parameters.
-
-
-
Field Detail
-
debug
private static final boolean debug
Whether to log information about instantiation.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TypeInstantiator
public TypeInstantiator(Set<Type> inputTypes)
Creates aTypeInstantiator
object using the given types to construct instantiating substitutions.- Parameters:
inputTypes
- the ground types for instantiations
-
-
Method Detail
-
instantiate
public TypedClassOperation instantiate(TypedClassOperation operation)
Instantiate the given operation by choosing type arguments for its type parameters.- Parameters:
operation
- the generic operation to instantiate- Returns:
- an instantiated version of the operation
-
instantiateSortedSetType
private Substitution instantiateSortedSetType(TypedClassOperation operation)
Returns a substitution that instantiates theSortedSet
type of the given constructor. ASortedSet
may be built so that the element typeE
is ordered either explicitly with aComparator<E>
, or the element type satisfiesE implements Comparable<E>
.- Parameters:
operation
- a constructor to be instantiated, for a class that implementsSortedSet
- Returns:
- the substitution to instantiate the element type of the
SortedSet
type
-
selectSubstitutionForSortedSet
private Substitution selectSubstitutionForSortedSet(GenericClassType searchType, TypeVariable typeParameter)
Select a substitution for the type parameter of SortedSet.- Parameters:
searchType
- one of Comparator, Comparable, or SortedSettypeParameter
- the type parameter to create an instantiation for- Returns:
- a substitution for the type parameter of SortedSet
-
instantiateClass
private Substitution instantiateClass(ClassOrInterfaceType type)
Chooses an instantiating substitution for the given class. Performs a coin flip to determine whether to use a previous instantiation, or to create a new one. Verifies that all of the type parameters of the type are instantiated, and logs failure if not.- Parameters:
type
- the type to be instantiated- Returns:
- a substitution instantiating the given type; null if none is found
-
selectSubstitution
private Substitution selectSubstitution(ClassOrInterfaceType type, ClassOrInterfaceType patternType)
Returns a substitution that instantiates bothtype
andpatternType
to an existing type. The pattern type makes it possible to select matches for partial instantiations of a generic type.- Parameters:
type
- the generic type for which an instantiation is to be foundpatternType
- an instantiation oftype
; only further instantiations of it are considered- Returns:
- a substitution instantiating
type
's type parameters to existing types; null if no such substitution exists
-
selectSubstitution
private Substitution selectSubstitution(ClassOrInterfaceType type)
Selects an existing type that instantiates the given generic declaring type and returns the instantiating substitution.- Parameters:
type
- the generic type for which an instantiation is to be found- Returns:
- a substitution instantiating the given type as an existing type; null if no such type
-
instantiateOperationTypes
private TypedClassOperation instantiateOperationTypes(TypedClassOperation operation)
Selects an instantiation of the generic types of an operation, and returns a new operation with the types instantiated.- Parameters:
operation
- the operation- Returns:
- the operation with generic types instantiated
-
selectSubstitution
private Substitution selectSubstitution(List<TypeVariable> typeParameters)
Selects an instantiating substitution for the given list of type variables.- Parameters:
typeParameters
- the type variables to be instantiated- Returns:
- a substitution instantiating the type variables; null if some type variable has no instantiating types
- See Also:
selectSubstitution(List, Substitution)
-
selectSubstitution
private Substitution selectSubstitution(List<TypeVariable> typeParameters, Substitution substitution)
Extends the given substitution by instantiations for the given list of type variables.- Parameters:
typeParameters
- the type variables to be instantiatedsubstitution
- the substitution to extend- Returns:
- the substitution extended by instantiating type variables; null if any of the variables has no instantiating types
-
allExtendingSubstitutions
private List<Substitution> allExtendingSubstitutions(List<TypeVariable> typeParameters, Substitution substitution)
Returns all substitutions that extend a substitution for the given type parameters.- Parameters:
typeParameters
- the type parameters to be instantiatedsubstitution
- the substitution to be extended- Returns:
- the list of possible substitutions, empty if none is found
-
selectSubstitutionIndependently
private Substitution selectSubstitutionIndependently(List<TypeVariable> parameters, Substitution substitution)
Selects types independently for a list of type parameters, and extends the given substitution by the substitution of the selected types for the parameters.IMPORTANT: Should only be used for parameters that have non-generic bounds.
- Parameters:
parameters
- a list of independent type parameterssubstitution
- the substitution to extend- Returns:
- the substitution extended by mapping given parameters to selected types; null, if there are no candidate types for some parameter
-
allSubstitutions
private List<Substitution> allSubstitutions(List<TypeVariable> parameters, Substitution initialSubstitution, BoundsCheck boundsCheck)
Adds instantiating substitutions for the given parameters to the list if satisfies the given type check predicate. Each constructed substitution extends the given initial substitution. Assumes that the parameters are or are refinements of the set of parameters check by the type check predicate.- Parameters:
parameters
- the list of parameters to instantiateinitialSubstitution
- the substitution to be extended by new substitutionsboundsCheck
- the predicate to type check a substitution- Returns:
- the list of instantiating substitutions; may be empty. The list is fresh and clients may side-effect it.
-
candidateTypes
private List<List<ReferenceType>> candidateTypes(List<TypeVariable> parameters)
Constructs the list of lists of candidate types for the given type parameters. Each sub list is the list of candidates for the corresponding parameter inparameters
, as determined bycandidateTypes(TypeVariable)
.- Parameters:
parameters
- the list of type parameters- Returns:
- the list of candidate lists for the parameters; returns the empty list if any parameter has no candidates
-
candidateTypes
private List<ReferenceType> candidateTypes(TypeVariable argument)
Returns all input types that potentially satisfy the bounds on the argument. If a bound has another type parameter, then the default bound is tested. The callerallSubstitutions(java.util.List<randoop.types.TypeVariable>, randoop.types.Substitution, randoop.types.BoundsCheck)
will ensure that the final substitution is consistent.- Parameters:
argument
- the type argument- Returns:
- the list of candidate instantiations of the type argument
-
getUpperBound
private ParameterBound getUpperBound(TypeVariable argument)
Gets the upper bound of the given argument to test incandidateTypes(TypeVariable)
. If the bound contains a type parameter other than the given argument, then returnsObject
as the bound.- Parameters:
argument
- the type argument- Returns:
- the upperbound of the argument if no other type parameter is needed, the
Object
bound otherwise
-
getLowerBound
private ParameterBound getLowerBound(TypeVariable argument)
Gets the lower bound of the given argument to be tested incandidateTypes(TypeVariable)
. If the bound has a type parameter other than the given argument, then returnsJavaTypes.NULL_TYPE
as the bound.- Parameters:
argument
- the type argument- Returns:
- the lower bound of the argument if no other type parameter is needed, the
JavaTypes.NULL_TYPE
otherwise
-
-