Search in sources :

Example 26 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 27 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Test(org.junit.Test)

Example 28 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 29 with CtStatement

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("}");
        }
    }
}
Also used : CtStatement(spoon.reflect.code.CtStatement)

Example 30 with CtStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement)

Aggregations

CtStatement (spoon.reflect.code.CtStatement)84 Test (org.junit.Test)55 Factory (spoon.reflect.factory.Factory)35 CtMethod (spoon.reflect.declaration.CtMethod)23 Launcher (spoon.Launcher)20 CtBlock (spoon.reflect.code.CtBlock)17 CtIf (spoon.reflect.code.CtIf)11 CtExpression (spoon.reflect.code.CtExpression)10 ArrayList (java.util.ArrayList)9 CtElement (spoon.reflect.declaration.CtElement)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtInvocation (spoon.reflect.code.CtInvocation)8 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 CtCase (spoon.reflect.code.CtCase)6 CtStatementList (spoon.reflect.code.CtStatementList)6 STATEMENT (spoon.reflect.path.CtRole.STATEMENT)6 FileSystemFile (spoon.support.compiler.FileSystemFile)6 Adobada (spoon.test.delete.testclasses.Adobada)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5