private void basicForStatement(PForInit init, Node cond, PForUpdate update, PStatement _do) { context.pushStatementScope(); try { JForLoop loop = parent._for(); if (init != null) { ForInitAdapter fia = new ForInitAdapter(loop, context); init.apply(fia); } if (update != null) { ForUpdateAdapter fua = new ForUpdateAdapter(loop, context); update.apply(fua); } if (cond != null) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); cond.apply(ea); loop.test(ea.expr); } JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); _do.apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); if (stmt instanceof JBlock) { JBlock stmtBlock = (JBlock) stmt; stmtBlock.setNewlineRequired(false); } loop.setBody(stmt); } finally { context.popScope(); } }
/** * Defines the print visitor's print method, which prints an object * with the appropriate indentation. * @param model * @param printVisitor * @param delim * @param depth * @return * * @author Tristan Bepler */ private static JMethod definePrintMethod(JCodeModel model, JDefinedClass printVisitor, JVar delim, JVar depth) { JMethod print = printVisitor.method(JMod.PRIVATE, void.class, "print"); JVar obj = print.param(Object.class, "obj"); JForLoop loop = print.body()._for(); JVar i = loop.init(model.INT, "i", JExpr.lit(0)); loop.test(i.lt(depth)); loop.update(i.incr()); loop.body().invoke(model.ref(System.class).staticRef("out"), "print").arg(delim); print.body().invoke(model.ref(System.class).staticRef("out"), "println").arg(obj); return print; }
@Override public JForLoop _for() { return block._for(); }
ForUpdateAdapter(JForLoop loop, ResolutionContext context) { this.context = context; this.loop = loop; }
ForInitAdapter(JForLoop loop, ResolutionContext context) { this.context = context; this.loop = loop; }
public JForLoop _for();