Search in sources :

Example 86 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitAnnotation.

@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
    // as @MyAnno(value="myArg") which has a single element argument list with an assignment.
    if (javaParserNode instanceof MarkerAnnotationExpr) {
        processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
    } else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == 1;
        ExpressionTree value = javacTree.getArguments().get(0);
        assert value instanceof AssignmentTree;
        AssignmentTree assignment = (AssignmentTree) value;
        assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
        assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
        assignment.getExpression().accept(this, node.getMemberValue());
    } else if (javaParserNode instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == node.getPairs().size();
        Iterator<MemberValuePair> argIter = node.getPairs().iterator();
        for (ExpressionTree arg : javacTree.getArguments()) {
            assert arg instanceof AssignmentTree;
            AssignmentTree assignment = (AssignmentTree) arg;
            IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
            MemberValuePair javaParserArg = argIter.next();
            assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
            assignment.getExpression().accept(this, javaParserArg.getValue());
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 87 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitMethodInvocation.

@Override
public Void visitMethodInvocation(MethodInvocationTree javacTree, Node javaParserNode) {
    if (javaParserNode instanceof MethodCallExpr) {
        MethodCallExpr node = (MethodCallExpr) javaParserNode;
        processMethodInvocation(javacTree, node);
        // JavaParser the type arguments will have the none Optional value.
        assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
        if (!javacTree.getTypeArguments().isEmpty()) {
            visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
        }
        // In JavaParser, the method name itself and receiver are stored as fields of the
        // invocation itself, but in javac they might be combined into one MemberSelectTree.
        // That member select may also be a single IdentifierTree if no receiver was written.
        // This requires one layer of unnesting.
        ExpressionTree methodSelect = javacTree.getMethodSelect();
        if (methodSelect.getKind() == Tree.Kind.IDENTIFIER) {
            methodSelect.accept(this, node.getName());
        } else if (methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) {
            MemberSelectTree selection = (MemberSelectTree) methodSelect;
            assert node.getScope().isPresent();
            selection.getExpression().accept(this, node.getScope().get());
        } else {
            throw new BugInCF("Unexpected method selection type: %s", methodSelect);
        }
        visitLists(javacTree.getArguments(), node.getArguments());
    } else if (javaParserNode instanceof ExplicitConstructorInvocationStmt) {
        ExplicitConstructorInvocationStmt node = (ExplicitConstructorInvocationStmt) javaParserNode;
        processMethodInvocation(javacTree, node);
        assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
        if (!javacTree.getTypeArguments().isEmpty()) {
            visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
        }
        visitLists(javacTree.getArguments(), node.getArguments());
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) BugInCF(org.checkerframework.javacutil.BugInCF) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 88 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.

the class ExpectedTreesVisitor method visitClass.

@Override
public Void visitClass(ClassTree tree, Void p) {
    defaultAction(tree);
    scan(tree.getModifiers(), p);
    scan(tree.getTypeParameters(), p);
    scan(tree.getExtendsClause(), p);
    scan(tree.getImplementsClause(), p);
    if (tree.getKind() == Tree.Kind.ENUM) {
        // instance of an enum.
        for (Tree member : tree.getMembers()) {
            member.accept(this, p);
            if (member.getKind() != Tree.Kind.VARIABLE) {
                continue;
            }
            VariableTree variable = (VariableTree) member;
            ExpressionTree initializer = variable.getInitializer();
            if (initializer == null || initializer.getKind() != Tree.Kind.NEW_CLASS) {
                continue;
            }
            NewClassTree constructor = (NewClassTree) initializer;
            if (constructor.getIdentifier().getKind() != Tree.Kind.IDENTIFIER) {
                continue;
            }
            IdentifierTree name = (IdentifierTree) constructor.getIdentifier();
            if (name.getName().contentEquals(tree.getSimpleName())) {
                trees.remove(variable.getType());
                trees.remove(constructor);
                trees.remove(constructor.getIdentifier());
            }
        }
    // RECORD was added in Java 14, so use string comparison to be JDK 8,11 compatible:
    } else if (tree.getKind().name().equals("RECORD")) {
        // A record like:
        // record MyRec(String myField) {}
        // will be expanded by javac to:
        // class MyRec {
        // MyRec(String myField) {
        // super();
        // }
        // private final String myField;
        // }
        // So the constructor and the field declarations have no matching trees in the JavaParser
        // node, and we must remove those trees (and their subtrees) from the `trees` field.
        TreeScannerWithDefaults removeAllVisitor = new TreeScannerWithDefaults() {

            @Override
            public void defaultAction(Tree node) {
                trees.remove(node);
            }
        };
        for (Tree member : tree.getMembers()) {
            scan(member, p);
            if (TreeUtils.isAutoGeneratedRecordMember(member)) {
                member.accept(removeAllVisitor, null);
            } else {
                // the parameters.  These trees also don't have a match:
                if (member.getKind() == Tree.Kind.METHOD) {
                    MethodTree methodTree = (MethodTree) member;
                    if (TreeUtils.isCompactCanonicalRecordConstructor(methodTree)) {
                        for (VariableTree canonicalParameter : methodTree.getParameters()) {
                            canonicalParameter.accept(removeAllVisitor, null);
                        }
                    }
                }
            }
        }
    } else {
        scan(tree.getMembers(), p);
    }
    return null;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ForLoopTree(com.sun.source.tree.ForLoopTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SwitchTree(com.sun.source.tree.SwitchTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewClassTree(com.sun.source.tree.NewClassTree)

Example 89 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitWhileLoop.

@Override
public Void visitWhileLoop(WhileLoopTree javacTree, Node javaParserNode) {
    WhileStmt node = castNode(WhileStmt.class, javaParserNode, javacTree);
    processWhileLoop(javacTree, node);
    // While loop conditions are always parenthesized in javac but never in JavaParser.
    assert javacTree.getCondition().getKind() == Tree.Kind.PARENTHESIZED;
    ExpressionTree condition = ((ParenthesizedTree) javacTree.getCondition()).getExpression();
    condition.accept(this, node.getCondition());
    javacTree.getStatement().accept(this, node.getBody());
    return null;
}
Also used : WhileStmt(com.github.javaparser.ast.stmt.WhileStmt) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 90 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitDoWhileLoop.

@Override
public Void visitDoWhileLoop(DoWhileLoopTree javacTree, Node javaParserNode) {
    DoStmt node = castNode(DoStmt.class, javaParserNode, javacTree);
    processDoWhileLoop(javacTree, node);
    // In javac the condition is parenthesized but not in JavaParser.
    ExpressionTree condition = ((ParenthesizedTree) javacTree.getCondition()).getExpression();
    condition.accept(this, node.getCondition());
    javacTree.getStatement().accept(this, node.getBody());
    return null;
}
Also used : ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) DoStmt(com.github.javaparser.ast.stmt.DoStmt)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)492 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)170 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)134 Tree (com.sun.source.tree.Tree)128 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)110 IdentifierTree (com.sun.source.tree.IdentifierTree)101 MemberSelectTree (com.sun.source.tree.MemberSelectTree)96 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)87 VariableTree (com.sun.source.tree.VariableTree)86 MethodTree (com.sun.source.tree.MethodTree)80 Type (com.sun.tools.javac.code.Type)77 Symbol (com.sun.tools.javac.code.Symbol)68 TreePath (com.sun.source.util.TreePath)59 ClassTree (com.sun.source.tree.ClassTree)57 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)57 NewClassTree (com.sun.source.tree.NewClassTree)56 StatementTree (com.sun.source.tree.StatementTree)53 BinaryTree (com.sun.source.tree.BinaryTree)51 AssignmentTree (com.sun.source.tree.AssignmentTree)50 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)49