Search in sources :

Example 11 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class DeleteTest method testDeleteAStatementInConstructor.

@Test
public void testDeleteAStatementInConstructor() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtConstructor<Adobada> constructor = adobada.getConstructor();
    assertEquals(3, constructor.getBody().getStatements().size());
    final CtStatement statement = constructor.getBody().getStatement(1);
    statement.delete();
    assertEquals(2, constructor.getBody().getStatements().size());
    assertFalse(constructor.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) Test(org.junit.Test)

Example 12 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class DeleteTest method testDeleteStatementInCase.

@Test
public void testDeleteStatementInCase() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m3");
    final CtCase aCase = method.getElements(new TypeFilter<>(CtCase.class)).get(0);
    assertEquals(2, aCase.getStatements().size());
    final CtStatement statement = aCase.getStatements().get(1);
    statement.delete();
    assertEquals(1, aCase.getStatements().size());
    assertFalse(aCase.getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 13 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class DeleteTest method testDeleteReturn.

@Test
public void testDeleteReturn() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m2");
    assertEquals(1, method.getBody().getStatements().size());
    final CtStatement statement = method.getBody().getStatement(0);
    statement.delete();
    assertEquals(0, method.getBody().getStatements().size());
    assertFalse(method.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 14 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class ConditionalTest method testNoBlockInConditionAndLoop.

@Test
public void testNoBlockInConditionAndLoop() throws Exception {
    String newLine = System.getProperty("line.separator");
    final CtType<Foo> aFoo = ModelUtils.buildClass(Foo.class);
    CtMethod<Object> method = aFoo.getMethod("m3");
    final List<CtIf> conditions = method.getElements(new TypeFilter<CtIf>(CtIf.class));
    for (int i = 0; i < conditions.size(); i++) {
        CtIf ctIf = conditions.get(i);
        // replace the block to a statement
        CtStatement then = ((CtBlock) ctIf.getThenStatement()).getStatement(0);
        ctIf.setThenStatement(then);
        if (ctIf.getElseStatement() != null) {
            CtStatement elseStatement = ((CtBlock) ctIf.getElseStatement()).getStatement(0);
            ctIf.setElseStatement(elseStatement);
        }
    }
    assertEquals("if (true)" + newLine + "    java.lang.System.out.println();" + newLine + "else if (true)" + newLine + "    java.lang.System.out.println();" + newLine + "else" + newLine + "    java.lang.System.out.println();" + newLine, method.getBody().getStatement(0).toString());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Foo(spoon.test.condition.testclasses.Foo) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 15 with CtStatement

use of spoon.reflect.code.CtStatement in project spoon by INRIA.

the class CtBodyHolderTest method checkCtBody.

private void checkCtBody(CtBodyHolder p_bodyHolder, String p_constant, int off) {
    CtStatement body = p_bodyHolder.getBody();
    assertTrue(body instanceof CtBlock<?>);
    CtBlock<?> block = (CtBlock) body;
    assertEquals(1 + off, block.getStatements().size());
    assertTrue(block.getStatement(off) instanceof CtAssignment);
    CtAssignment assignment = block.getStatement(off);
    assertEquals(p_constant, ((CtLiteral<String>) assignment.getAssignment().partiallyEvaluate()).getValue());
    Factory f = body.getFactory();
    CtStatement newStat = new CWBStatementTemplate("xx").apply(body.getParent(CtType.class));
    try {
        newStat.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    // try to set statement and get CtBlock
    p_bodyHolder.setBody(newStat);
    CtBlock newBlock = (CtBlock) p_bodyHolder.getBody();
    assertSame(p_bodyHolder, newBlock.getParent());
    assertSame(newBlock, newStat.getParent());
    // try to set CtBlock and get the same CtBlock
    CtStatement newStat2 = newStat.clone();
    try {
        newStat2.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    CtBlock newBlock2 = f.Code().createCtBlock(newStat2);
    assertSame(newBlock2, newStat2.getParent());
    try {
        newBlock2.getParent();
        fail();
    } catch (ParentNotInitializedException e) {
    // expected exception
    }
    p_bodyHolder.setBody(newBlock2);
    assertSame(newBlock2, p_bodyHolder.getBody());
    assertSame(p_bodyHolder, newBlock2.getParent());
    assertSame(newBlock2, newStat2.getParent());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtBlock(spoon.reflect.code.CtBlock) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) CWBStatementTemplate(spoon.test.ctBodyHolder.testclasses.CWBStatementTemplate)

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