Search in sources :

Example 1 with ForEachStmt

use of com.github.javaparser.ast.stmt.ForEachStmt 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 ForEachStmt

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

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final ForEachStmt node1, final Node other) {
    ForEachStmt node2 = (ForEachStmt) other;
    defaultAction(node1, node2);
    node1.getBody().accept(this, node2.getBody());
    node1.getIterable().accept(this, node2.getIterable());
    node1.getVariable().accept(this, node2.getVariable());
}
Also used : ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt)

Example 3 with ForEachStmt

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

the class PropagatorCompilerHandler method startRangeIndex.

@Override
public void startRangeIndex(AlphaRangeIndex alphaRangeIndex) {
    String rangeIndexVariableName = getRangeIndexVariableName(alphaRangeIndex, getMinIdFromRangeIndex(alphaRangeIndex));
    String matchingResultVariableName = rangeIndexVariableName + "_result";
    String matchingNodeVariableName = matchingResultVariableName + "_node";
    ExpressionStmt matchingResultVariable = localVariable(parseType("java.util.Collection<org.drools.core.reteoo.AlphaNode>"), matchingResultVariableName, new MethodCallExpr(new NameExpr(rangeIndexVariableName), "getMatchingAlphaNodes", nodeList(new MethodCallExpr(new NameExpr(FACT_HANDLE_PARAM_NAME), "getObject"))));
    final BlockStmt currentBlockStatement = getCurrentBlockStatement();
    currentBlockStatement.addStatement(matchingResultVariable);
    BlockStmt body = new BlockStmt();
    ForEachStmt forEachStmt = new ForEachStmt(new VariableDeclarationExpr(parseType("org.drools.core.reteoo.AlphaNode"), matchingNodeVariableName), new NameExpr(matchingResultVariableName), body);
    currentBlockStatement.addStatement(forEachStmt);
    SwitchStmt switchStatement = new SwitchStmt().setSelector(new MethodCallExpr(new NameExpr(matchingNodeVariableName), "getId"));
    this.currentStatement.push(switchStatement);
    body.addStatement(switchStatement);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with ForEachStmt

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

the class JointJavacJavaParserVisitor method visitEnhancedForLoop.

@Override
public Void visitEnhancedForLoop(EnhancedForLoopTree javacTree, Node javaParserNode) {
    ForEachStmt node = castNode(ForEachStmt.class, javaParserNode, javacTree);
    processEnhancedForLoop(javacTree, node);
    javacTree.getVariable().accept(this, node.getVariableDeclarator());
    javacTree.getExpression().accept(this, node.getIterable());
    javacTree.getStatement().accept(this, node.getBody());
    return null;
}
Also used : ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt)

Example 5 with ForEachStmt

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

the class ForEachDowncastStmtT method toJavaExpression.

@Override
public Node toJavaExpression() {
    ForEachStmt newForEachStmt = new ForEachStmt();
    BlockStmt body = new BlockStmt();
    NodeList<VariableDeclarator> variables = nodeList();
    for (VariableDeclarator v : variableDeclarationExpr.getVariables()) {
        VariableDeclarator newVariable = v.clone();
        String newIteratorVariable = "_" + v.getNameAsString();
        VariableDeclarationExpr castAssign = new VariableDeclarationExpr(new VariableDeclarator(v.getType(), v.getName(), new CastExpr(v.getType(), new NameExpr(newIteratorVariable))));
        body.addStatement(0, castAssign);
        newVariable.setType(Object.class);
        newVariable.setName(newIteratorVariable);
        variables.add(newVariable);
    }
    body.addStatement((BlockStmt) child.toJavaExpression());
    newForEachStmt.setBody(body);
    VariableDeclarationExpr newVariables = new VariableDeclarationExpr(variables);
    newForEachStmt.setVariable(newVariables);
    return new ForEachStmt(newVariables, new NameExpr(iterable), body);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CastExpr(com.github.javaparser.ast.expr.CastExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Aggregations

ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)4 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)3 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 Node (com.github.javaparser.ast.Node)1 Parameter (com.github.javaparser.ast.body.Parameter)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ForStmt (com.github.javaparser.ast.stmt.ForStmt)1 ForeachStmt (com.github.javaparser.ast.stmt.ForeachStmt)1 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)1