use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ReplaceTest method testReplaceStmtByListStatementsAndNull.
@Test
public void testReplaceStmtByListStatementsAndNull() {
// contract: null elements in list are ignored
CtClass<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
// replace retry content by statements
CtStatement stmt = sample.getMethod("retry").getBody().getStatement(0);
List<CtStatement> lst = sample.getMethod("statements").getBody().getStatements();
List<CtStatement> lstWithNulls = new ArrayList<>();
lstWithNulls.add(null);
lstWithNulls.add(lst.get(0));
lstWithNulls.add(null);
lstWithNulls.add(lst.get(1));
lstWithNulls.add(null);
// replace a single statement by a statement list
stmt.replace(lstWithNulls);
// we should have only 2 statements after (from the stmt list)
assertEquals(2, sample.getMethod("retry").getBody().getStatements().size());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ReplaceTest method testReplaceStmtByListStatements.
@Test
public void testReplaceStmtByListStatements() {
CtClass<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
// replace retry content by statements
CtStatement stmt = sample.getMethod("retry").getBody().getStatement(0);
List<CtStatement> lst = sample.getMethod("statements").getBody().getStatements();
// replace a single statement by a statement list
stmt.replace(lst);
// we should have only 2 statements after (from the stmt list)
assertEquals(2, sample.getMethod("retry").getBody().getStatements().size());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtFor.
@Override
public void visitCtFor(CtFor forLoop) {
enterCtStatement(forLoop);
printer.writeKeyword("for").writeSpace().writeSeparator("(");
List<CtStatement> st = forLoop.getForInit();
if (st.size() > 0) {
scan(st.get(0));
}
if (st.size() > 1) {
try (Writable _context = context.modify().noTypeDecl(true)) {
for (int i = 1; i < st.size(); i++) {
printer.writeSeparator(",").writeSpace();
scan(st.get(i));
}
}
}
printer.writeSeparator(";").writeSpace();
scan(forLoop.getExpression());
printer.writeSeparator(";");
if (!forLoop.getForUpdate().isEmpty()) {
printer.writeSpace();
}
try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, null, false, true, ",", true, false, null)) {
for (CtStatement s : forLoop.getForUpdate()) {
lp.printSeparatorIfAppropriate();
scan(s);
}
}
printer.writeSeparator(")");
elementPrinterHelper.writeIfOrLoopBlock(forLoop.getBody());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtBlock.
@Override
public <R> void visitCtBlock(CtBlock<R> block) {
enterCtStatement(block);
if (!block.isImplicit()) {
printer.writeSeparator("{");
}
printer.incTab();
for (CtStatement statement : block.getStatements()) {
if (!statement.isImplicit()) {
printer.writeln();
elementPrinterHelper.writeStatement(statement);
}
}
printer.decTab();
getPrinterHelper().adjustEndPosition(block);
if (env.isPreserveLineNumbers()) {
if (!block.isImplicit()) {
printer.writeSeparator("}");
}
} else {
printer.writeln();
if (!block.isImplicit()) {
printer.writeSeparator("}");
}
}
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class VisitorPartialEvaluator method visitCtFor.
public void visitCtFor(CtFor forLoop) {
// Evaluate forInit
List<CtStatement> lst = forLoop.getForInit();
for (CtStatement s : lst) {
CtStatement evaluateStatement = evaluate(s);
if (evaluateStatement != null) {
forLoop.addForInit(evaluateStatement);
}
}
// Evaluate Expression
forLoop.setExpression(evaluate(forLoop.getExpression()));
// Evaluate forUpdate
lst = forLoop.getForUpdate();
for (CtStatement s : lst) {
CtStatement evaluateStatement = evaluate(s);
if (evaluateStatement != null) {
forLoop.addForUpdate(evaluateStatement);
}
}
setResult(forLoop.clone());
}
Aggregations