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