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.");
}
});
}
});
});
});
}
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;
}
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());
}
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;
}
Aggregations