Search in sources :

Example 1 with Pair

use of com.github.javaparser.utils.Pair in project javaparser by javaparser.

the class BoundSet method deriveImpliedBounds.

public BoundSet deriveImpliedBounds(TypeSolver typeSolver) {
    // As bound sets are constructed and grown during inference, it is possible that new bounds can be inferred
    // based on the assertions of the original bounds. The process of incorporation identifies these new bounds
    // and adds them to the bound set.
    // 
    // Incorporation can happen in two scenarios. One scenario is that the bound set contains complementary pairs
    // of bounds; this implies new constraint formulas, as specified in §18.3.1. The other scenario is that the
    // bound set contains a bound involving capture conversion; this implies new bounds and may imply new
    // constraint formulas, as specified in §18.3.2. In both scenarios, any new constraint formulas are reduced,
    // and any new bounds are added to the bound set. This may trigger further incorporation; ultimately, the set
    // will reach a fixed point and no further bounds can be inferred.
    // 
    // If incorporation of a bound set has reached a fixed point, and the set does not contain the bound false,
    // then the bound set has the following properties:
    // 
    // - For each combination of a proper lower bound L and a proper upper bound U of an inference variable, L <: U.
    // 
    // - If every inference variable mentioned by a bound has an instantiation, the bound is satisfied by the
    // corresponding substitution.
    // 
    // - Given a dependency α = β, every bound of α matches a bound of β, and vice versa.
    // 
    // - Given a dependency α <: β, every lower bound of α is a lower bound of β, and every upper bound of β is an
    // upper bound of α.
    ConstraintFormulaSet newConstraintsSet = ConstraintFormulaSet.empty();
    // SECTION Complementary Pairs of Bounds
    // (In this section, S and T are inference variables or types, and U is a proper type. For conciseness, a bound
    // of the form α = T may also match a bound of the form T = α.)
    // 
    // When a bound set contains a pair of bounds that match one of the following rules, a new constraint formula
    // is implied:
    // 
    // - α = S and α = T imply ‹S = T›
    newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getT(), b.getT()));
        }
        if (areSameTypeInference(a.getS(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getS(), b.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getT(), b.getS()));
        }
        if (areSameTypeInference(a.getT(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getS(), b.getS()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = S and α <: T imply ‹S <: T›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, a.getT(), b.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, a.getS(), b.getT()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = S and T <: α imply ‹T <: S›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getS()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - S <: α and α <: T imply ‹S <: T›
    newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getT()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = U and S = T imply ‹S[α:=U] = T[α:=U]›
    newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
            InferenceVariable alpha = (InferenceVariable) a.getS();
            ResolvedType U = a.getT();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
            InferenceVariable alpha = (InferenceVariable) a.getT();
            ResolvedType U = a.getS();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(b.getS()) && isProperType(b.getT())) {
            InferenceVariable alpha = (InferenceVariable) b.getS();
            ResolvedType U = b.getT();
            ResolvedType S = a.getS();
            ResolvedType T = a.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(b.getT()) && isProperType(b.getS())) {
            InferenceVariable alpha = (InferenceVariable) b.getT();
            ResolvedType U = b.getS();
            ResolvedType S = a.getS();
            ResolvedType T = a.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = U and S <: T imply ‹S[α:=U] <: T[α:=U]›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
            InferenceVariable alpha = (InferenceVariable) a.getS();
            ResolvedType U = a.getT();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
            InferenceVariable alpha = (InferenceVariable) a.getT();
            ResolvedType U = a.getS();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // When a bound set contains a pair of bounds α <: S and α <: T, and there exists a supertype of S of the
    // form G<S1, ..., Sn> and a supertype of T of the form G<T1, ..., Tn> (for some generic class or interface, G),
    // then for all i (1 ≤ i ≤ n), if Si and Ti are types (not wildcards), the constraint formula ‹Si = Ti› is
    // implied.
    newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isInferenceVariable(b.getS())) {
            if (a.getT().isReferenceType() && b.getT().isReferenceType()) {
                ResolvedReferenceType S = a.getT().asReferenceType();
                ResolvedReferenceType T = b.getT().asReferenceType();
                List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = findPairsOfCommonAncestors(S, T);
                for (Pair<ResolvedReferenceType, ResolvedReferenceType> pair : pairs) {
                    for (int i = 0; i < Math.min(pair.a.typeParametersValues().size(), pair.b.typeParametersValues().size()); i++) {
                        ResolvedType si = pair.a.typeParametersValues().get(i);
                        ResolvedType ti = pair.b.typeParametersValues().get(i);
                        if (!si.isWildcard() && !ti.isWildcard()) {
                            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(si, ti));
                        }
                    }
                }
            }
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    for (Bound b : this.bounds.stream().filter(b -> b instanceof CapturesBound).collect(Collectors.toList())) {
        CapturesBound capturesBound = (CapturesBound) b;
        throw new UnsupportedOperationException();
    // Let P1, ..., Pn represent the type parameters of G and let B1, ..., Bn represent the bounds of these type
    // parameters. Let θ represent the substitution [P1:=α1, ..., Pn:=αn]. Let R be a type that is not an inference
    // variable (but is not necessarily a proper type).
    // 
    // A set of bounds on α1, ..., αn is implied, constructed from the declared bounds of P1, ..., Pn as specified
    // in §18.1.3.
    // 
    // In addition, for all i (1 ≤ i ≤ n):
    // 
    // - If Ai is not a wildcard, then the bound αi = Ai is implied.
    // 
    // - If Ai is a wildcard of the form ?:
    // 
    // - αi = R implies the bound false
    // 
    // - αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the bound false
    // 
    // - If Ai is a wildcard of the form ? extends T:
    // 
    // - αi = R implies the bound false
    // 
    // - If Bi is Object, then αi <: R implies the constraint formula ‹T <: R›
    // 
    // - If T is Object, then αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the bound false
    // 
    // - If Ai is a wildcard of the form ? super T:
    // 
    // - αi = R implies the bound false
    // 
    // - αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the constraint formula ‹R <: T›
    }
    if (newConstraintsSet.isEmpty()) {
        return this;
    } else {
        BoundSet newBounds = newConstraintsSet.reduce(typeSolver);
        if (newBounds.isEmpty()) {
            return this;
        }
        return this.incorporate(newBounds, typeSolver);
    }
}
Also used : TypeSameAsType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType) TypeHelper(com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper) TypeSubtypeOfType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType) java.util(java.util) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) com.github.javaparser.symbolsolver.resolution.typeinference.bounds(com.github.javaparser.symbolsolver.resolution.typeinference.bounds) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) Pair(com.github.javaparser.utils.Pair) TypeSubtypeOfType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TypeSameAsType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Pair(com.github.javaparser.utils.Pair)

Example 2 with Pair

use of com.github.javaparser.utils.Pair in project javaparser by javaparser.

the class LexicalPreservingPrinter method storeInitialTextForOneNode.

private static void storeInitialTextForOneNode(Node node, List<JavaToken> nodeTokens) {
    if (nodeTokens == null) {
        nodeTokens = Collections.emptyList();
    }
    List<Pair<Range, TextElement>> elements = new LinkedList<>();
    for (Node child : node.getChildNodes()) {
        if (!PhantomNodeLogic.isPhantomNode(child)) {
            if (!child.getRange().isPresent()) {
                throw new RuntimeException("Range not present on node " + child);
            }
            elements.add(new Pair<>(child.getRange().get(), new ChildTextElement(child)));
        }
    }
    for (JavaToken token : nodeTokens) {
        elements.add(new Pair<>(token.getRange().get(), new TokenTextElement(token)));
    }
    elements.sort(comparing(e -> e.a.begin));
    node.setData(NODE_TEXT_DATA, new NodeText(elements.stream().map(p -> p.b).collect(Collectors.toList())));
}
Also used : java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) TreeVisitor(com.github.javaparser.ast.visitor.TreeVisitor) Pair(com.github.javaparser.utils.Pair) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) DataKey(com.github.javaparser.ast.DataKey) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Utils.assertNotNull(com.github.javaparser.utils.Utils.assertNotNull) Utils.decapitalize(com.github.javaparser.utils.Utils.decapitalize) PropagatingAstObserver(com.github.javaparser.ast.observer.PropagatingAstObserver) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) TokenTypes.eolTokenKind(com.github.javaparser.TokenTypes.eolTokenKind) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) InvocationTargetException(java.lang.reflect.InvocationTargetException) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) com.github.javaparser(com.github.javaparser) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables) ConcreteSyntaxModel(com.github.javaparser.printer.ConcreteSyntaxModel) Utils(com.github.javaparser.utils.Utils) Writer(java.io.Writer) Comment(com.github.javaparser.ast.comments.Comment) Comparator(java.util.Comparator) Node(com.github.javaparser.ast.Node) Pair(com.github.javaparser.utils.Pair)

Example 3 with Pair

use of com.github.javaparser.utils.Pair in project javaparser by javaparser.

the class ArrayType method unwrapArrayTypes.

/**
 * Takes a type that may be an ArrayType. Unwraps ArrayTypes until the element type is found.
 *
 * @return a pair of the element type, and the unwrapped ArrayTypes, if any.
 */
public static Pair<Type, List<ArrayBracketPair>> unwrapArrayTypes(Type type) {
    final List<ArrayBracketPair> arrayBracketPairs = new ArrayList<>();
    while (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        arrayBracketPairs.add(new ArrayBracketPair(Range.UNKNOWN, arrayType.getAnnotations()));
        type = arrayType.getComponentType();
    }
    return new Pair<>(type, arrayBracketPairs);
}
Also used : ArrayBracketPair(com.github.javaparser.ast.ArrayBracketPair) ArrayList(java.util.ArrayList) ArrayBracketPair(com.github.javaparser.ast.ArrayBracketPair) Pair(com.github.javaparser.utils.Pair)

Example 4 with Pair

use of com.github.javaparser.utils.Pair in project javaparser by javaparser.

the class BoundSet method findPairsOfCommonAncestors.

private List<Pair<ResolvedReferenceType, ResolvedReferenceType>> findPairsOfCommonAncestors(ResolvedReferenceType r1, ResolvedReferenceType r2) {
    List<ResolvedReferenceType> set1 = new LinkedList<>();
    set1.add(r1);
    set1.addAll(r1.getAllAncestors());
    List<ResolvedReferenceType> set2 = new LinkedList<>();
    set2.add(r2);
    set2.addAll(r2.getAllAncestors());
    List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = new LinkedList<>();
    for (ResolvedReferenceType rtFrom1 : set1) {
        for (ResolvedReferenceType rtFrom2 : set2) {
            if (rtFrom1.getTypeDeclaration().equals(rtFrom2.getTypeDeclaration())) {
                pairs.add(new Pair<>(rtFrom1, rtFrom2));
            }
        }
    }
    return pairs;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Pair(com.github.javaparser.utils.Pair)

Example 5 with Pair

use of com.github.javaparser.utils.Pair in project javaparser by javaparser.

the class NodeGenerator method parseNode.

protected Pair<CompilationUnit, ClassOrInterfaceDeclaration> parseNode(BaseNodeMetaModel nodeMetaModel) {
    CompilationUnit nodeCu = sourceRoot.parse(nodeMetaModel.getPackageName(), nodeMetaModel.getTypeName() + ".java");
    ClassOrInterfaceDeclaration nodeCoid = nodeCu.getClassByName(nodeMetaModel.getTypeName()).orElseThrow(() -> new AssertionError("Can't find class"));
    return new Pair<>(nodeCu, nodeCoid);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Pair(com.github.javaparser.utils.Pair)

Aggregations

Pair (com.github.javaparser.utils.Pair)5 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 com.github.javaparser (com.github.javaparser)1 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)1 TokenTypes.eolTokenKind (com.github.javaparser.TokenTypes.eolTokenKind)1 ArrayBracketPair (com.github.javaparser.ast.ArrayBracketPair)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 DataKey (com.github.javaparser.ast.DataKey)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Comment (com.github.javaparser.ast.comments.Comment)1 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)1 NodeWithVariables (com.github.javaparser.ast.nodeTypes.NodeWithVariables)1 AstObserver (com.github.javaparser.ast.observer.AstObserver)1 ObservableProperty (com.github.javaparser.ast.observer.ObservableProperty)1 PropagatingAstObserver (com.github.javaparser.ast.observer.PropagatingAstObserver)1