Class TypeInstantiator


  • public class TypeInstantiator
    extends Object
    Instantiates type parameters from a set of input types.
    • Field Detail

      • debug

        private static final boolean debug
        Whether to log information about instantiation.
        See Also:
        Constant Field Values
      • inputTypes

        private final Set<Type> inputTypes
        The set of input types for this model. The input types need to be closed on supertypes: if a type is in the input types, then so are all of its supertypes.
    • Constructor Detail

      • TypeInstantiator

        public TypeInstantiator​(Set<Type> inputTypes)
        Creates a TypeInstantiator 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 the SortedSet type of the given constructor. A SortedSet may be built so that the element type E is ordered either explicitly with a Comparator<E>, or the element type satisfies E implements Comparable<E>.
        Parameters:
        operation - a constructor to be instantiated, for a class that implements SortedSet
        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 SortedSet
        typeParameter - 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 both type and patternType 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 found
        patternType - an instantiation of type; 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 instantiated
        substitution - 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 instantiated
        substitution - 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 parameters
        substitution - 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 instantiate
        initialSubstitution - the substitution to be extended by new substitutions
        boundsCheck - 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 in parameters, as determined by candidateTypes(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
      • getUpperBound

        private ParameterBound getUpperBound​(TypeVariable argument)
        Gets the upper bound of the given argument to test in candidateTypes(TypeVariable). If the bound contains a type parameter other than the given argument, then returns Object 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