Search in sources :

Example 1 with CtCase

use of spoon.reflect.code.CtCase 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 2 with CtCase

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

the class DeleteTest method testDeleteACaseOfASwitch.

@Test
public void testDeleteACaseOfASwitch() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m3");
    final CtSwitch aSwitch = method.getElements(new TypeFilter<>(CtSwitch.class)).get(0);
    final CtCase aCase = (CtCase) aSwitch.getCases().get(1);
    assertEquals(2, aSwitch.getCases().size());
    aCase.delete();
    assertEquals(1, aSwitch.getCases().size());
    assertFalse(aSwitch.getCases().contains(aCase));
}
Also used : CtCase(spoon.reflect.code.CtCase) CtSwitch(spoon.reflect.code.CtSwitch) 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 3 with CtCase

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

the class SwitchCaseTest method insertAfterStatementInSwitchCaseWithoutException.

@Test
public void insertAfterStatementInSwitchCaseWithoutException() throws Exception {
    String packageName = "spoon.test.ctCase";
    String className = "ClassWithSwitchExample";
    Factory factory = factoryFor(packageName, className);
    List<CtCase> elements = elementsOfType(CtCase.class, factory);
    assertEquals(3, elements.size());
    CtCase firstCase = elements.get(0);
    List<CtStatement> statements = firstCase.getStatements();
    assertEquals(2, statements.size());
    CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
    statements.get(0).insertAfter(newStatement);
    statements = firstCase.getStatements();
    assertEquals(3, statements.size());
    assertTrue(statements.get(1) == newStatement);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 4 with CtCase

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

the class TestCtBlock method testAddStatementInCase.

@Test
public void testAddStatementInCase() {
    // contract: we can add a statement at a specific index
    // the statements with a higher index are pushed after
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
    spoon.buildModel();
    CtClass<?> toto = spoon.getFactory().Class().get(Toto.class);
    CtMethod bar = toto.getMethodsByName("bar").get(0);
    CtSwitch<?> ctSwitch = (CtSwitch) bar.getBody().getStatement(0);
    CtCase firstCase = ctSwitch.getCases().get(0);
    CtCase originalFirstCase = firstCase.clone();
    CtCase secondCase = ctSwitch.getCases().get(1);
    CtCase originalSecondCase = secondCase.clone();
    assertEquals(3, firstCase.getStatements().size());
    firstCase.addStatement(3, spoon.getFactory().createBreak());
    assertEquals(4, firstCase.getStatements().size());
    for (int i = 0; i < 3; i++) {
        assertEquals(originalFirstCase.getStatements().get(i), firstCase.getStatements().get(i));
    }
    assertEquals(2, secondCase.getStatements().size());
    secondCase.addStatement(1, (CtStatement) spoon.getFactory().createInvocation().setExecutable(bar.getReference()));
    assertEquals(3, secondCase.getStatements().size());
    assertEquals(originalSecondCase.getStatements().get(0), secondCase.getStatements().get(0));
    assertEquals(originalSecondCase.getStatements().get(1), secondCase.getStatements().get(2));
}
Also used : CtCase(spoon.reflect.code.CtCase) CtSwitch(spoon.reflect.code.CtSwitch) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 5 with CtCase

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

the class SwitchCaseTest method insertBeforeStatementInSwitchCaseWithoutException.

@Test
public void insertBeforeStatementInSwitchCaseWithoutException() throws Exception {
    String packageName = "spoon.test.ctCase";
    String className = "ClassWithSwitchExample";
    Factory factory = factoryFor(packageName, className);
    List<CtCase> elements = elementsOfType(CtCase.class, factory);
    assertEquals(3, elements.size());
    CtCase firstCase = elements.get(0);
    List<CtStatement> statements = firstCase.getStatements();
    assertEquals(2, statements.size());
    CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
    statements.get(0).insertBefore(newStatement);
    statements = firstCase.getStatements();
    assertEquals(3, statements.size());
    assertTrue(statements.get(0) == newStatement);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtCase (spoon.reflect.code.CtCase)8 Test (org.junit.Test)7 CtStatement (spoon.reflect.code.CtStatement)6 CtMethod (spoon.reflect.declaration.CtMethod)6 Factory (spoon.reflect.factory.Factory)5 CtSwitch (spoon.reflect.code.CtSwitch)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 ArrayList (java.util.ArrayList)2 Launcher (spoon.Launcher)2 CtBlock (spoon.reflect.code.CtBlock)2 CtIf (spoon.reflect.code.CtIf)2 Adobada (spoon.test.delete.testclasses.Adobada)2 File (java.io.File)1 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)1 CtBreak (spoon.reflect.code.CtBreak)1 CtCatch (spoon.reflect.code.CtCatch)1 CtConditional (spoon.reflect.code.CtConditional)1 CtContinue (spoon.reflect.code.CtContinue)1 CtDo (spoon.reflect.code.CtDo)1 CtFor (spoon.reflect.code.CtFor)1