Search in sources :

Example 1 with ForStmt

use of com.github.javaparser.ast.stmt.ForStmt in project javaparser by javaparser.

the class VarValidator method accept.

@Override
public void accept(VarType node, ProblemReporter reporter) {
    // All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
    Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
    if (!variableDeclarator.isPresent()) {
        // Java 11's var in lambda's
        if (varAllowedInLambdaParameters) {
            boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
            if (valid) {
                return;
            }
        }
        reportIllegalPosition(node, reporter);
        return;
    }
    variableDeclarator.ifPresent(vd -> {
        Optional<Node> variableDeclarationExpr = vd.getParentNode();
        if (!variableDeclarationExpr.isPresent()) {
            reportIllegalPosition(node, reporter);
            return;
        }
        variableDeclarationExpr.ifPresent(vdeNode -> {
            if (!(vdeNode instanceof VariableDeclarationExpr)) {
                reportIllegalPosition(node, reporter);
                return;
            }
            VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
            if (vde.getVariables().size() > 1) {
                reporter.report(vde, "\"var\" only takes a single variable.");
            }
            Optional<Node> container = vdeNode.getParentNode();
            if (!container.isPresent()) {
                reportIllegalPosition(node, reporter);
                return;
            }
            container.ifPresent(c -> {
                boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
                if (!positionIsFine) {
                    reportIllegalPosition(node, reporter);
                }
                // A local variable declaration ends up inside an ExpressionStmt.
                if (c instanceof ExpressionStmt) {
                    if (!vd.getInitializer().isPresent()) {
                        reporter.report(node, "\"var\" needs an initializer.");
                    }
                    vd.getInitializer().ifPresent(initializer -> {
                        if (initializer instanceof NullLiteralExpr) {
                            reporter.report(node, "\"var\" cannot infer type from just null.");
                        }
                        if (initializer instanceof ArrayCreationExpr) {
                            reporter.report(node, "\"var\" cannot infer array types.");
                        }
                    });
                }
            });
        });
    });
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Node(com.github.javaparser.ast.Node) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ForeachStmt(com.github.javaparser.ast.stmt.ForeachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ForStmt(com.github.javaparser.ast.stmt.ForStmt) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 2 with ForStmt

use of com.github.javaparser.ast.stmt.ForStmt in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitForLoop.

@Override
public Void visitForLoop(ForLoopTree javacTree, Node javaParserNode) {
    ForStmt node = castNode(ForStmt.class, javaParserNode, javacTree);
    processForLoop(javacTree, node);
    Iterator<? extends StatementTree> javacInitializers = javacTree.getInitializer().iterator();
    for (Expression initializer : node.getInitialization()) {
        if (initializer.isVariableDeclarationExpr()) {
            for (VariableDeclarator declarator : initializer.asVariableDeclarationExpr().getVariables()) {
                assert javacInitializers.hasNext();
                javacInitializers.next().accept(this, declarator);
            }
        } else if (initializer.isAssignExpr()) {
            ExpressionStatementTree statement = (ExpressionStatementTree) javacInitializers.next();
            statement.getExpression().accept(this, initializer);
        } else {
            assert javacInitializers.hasNext();
            javacInitializers.next().accept(this, initializer);
        }
    }
    assert !javacInitializers.hasNext();
    visitOptional(javacTree.getCondition(), node.getCompare());
    // the javac statements must be unwrapped.
    assert javacTree.getUpdate().size() == node.getUpdate().size();
    Iterator<Expression> javaParserUpdates = node.getUpdate().iterator();
    for (ExpressionStatementTree javacUpdate : javacTree.getUpdate()) {
        // Match the inner javac expression with the JavaParser expression.
        javacUpdate.getExpression().accept(this, javaParserUpdates.next());
    }
    javacTree.getStatement().accept(this, node.getBody());
    return null;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ForStmt(com.github.javaparser.ast.stmt.ForStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 3 with ForStmt

use of com.github.javaparser.ast.stmt.ForStmt in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final ForStmt node1, final Node other) {
    ForStmt node2 = (ForStmt) other;
    defaultAction(node1, node2);
    node1.getBody().accept(this, node2.getBody());
    node1.getCompare().ifPresent(l -> l.accept(this, node2.getCompare().get()));
    visitLists(node1.getInitialization(), node2.getInitialization());
    visitLists(node1.getUpdate(), node2.getUpdate());
}
Also used : ForStmt(com.github.javaparser.ast.stmt.ForStmt)

Example 4 with ForStmt

use of com.github.javaparser.ast.stmt.ForStmt in project drools by kiegroup.

the class ForStmtT method toJavaExpression.

@Override
public Node toJavaExpression() {
    ForStmt stmt = new ForStmt();
    stmt.setInitialization(NodeList.nodeList(initialization.stream().map(TypedExpression::toJavaExpression).map(Expression.class::cast).collect(Collectors.toList())));
    compare.ifPresent(c -> stmt.setCompare((Expression) c.toJavaExpression()));
    stmt.setUpdate(NodeList.nodeList(update.stream().map(TypedExpression::toJavaExpression).map(Expression.class::cast).collect(Collectors.toList())));
    stmt.setBody((Statement) body.toJavaExpression());
    return stmt;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ForStmt(com.github.javaparser.ast.stmt.ForStmt)

Aggregations

ForStmt (com.github.javaparser.ast.stmt.ForStmt)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 Expression (com.github.javaparser.ast.expr.Expression)2 Node (com.github.javaparser.ast.Node)1 Parameter (com.github.javaparser.ast.body.Parameter)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 ForeachStmt (com.github.javaparser.ast.stmt.ForeachStmt)1 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)1