Search in sources :

Example 11 with CtCodeSnippetStatement

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

the class InsertMethodsTest method testInsertBefore.

@Test
public void testInsertBefore() {
    CtMethod<Void> foo = (CtMethod<Void>) assignmentClass.getMethods().toArray()[0];
    CtBlock<?> body = foo.getBody();
    assertEquals(3, body.getStatements().size());
    CtStatement s = body.getStatements().get(2);
    assertEquals("int z = x + y", s.toString());
    // adding a new statement;
    CtCodeSnippetStatement stmt = factory.Core().createCodeSnippetStatement();
    stmt.setValue("System.out.println(x);");
    s.insertBefore(stmt);
    assertEquals(4, body.getStatements().size());
    assertSame(stmt, body.getStatements().get(2));
    assertEquals(s.getParent(), stmt.getParent());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 12 with CtCodeSnippetStatement

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

the class IntercessionTest method testInsertAfter.

@Test
public void testInsertAfter() {
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;" + " int y=0;" + " int z=x+y;" + "}" + "};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    CtBlock<?> body = foo.getBody();
    assertEquals(3, body.getStatements().size());
    CtStatement s = body.getStatements().get(2);
    assertEquals("int z = x + y", s.toString());
    // adding a new statement;
    CtCodeSnippetStatement stmt = factory.Core().createCodeSnippetStatement();
    stmt.setValue("System.out.println(x);");
    s.insertAfter(stmt);
    assertEquals(4, body.getStatements().size());
    assertSame(stmt, body.getStatements().get(3));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 13 with CtCodeSnippetStatement

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

the class BlockTest method testAddEmptyBlock.

@Test
public void testAddEmptyBlock() {
    // specifies a bug found by Benoit Cornu on August 7 2014
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " " + "}};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    // empty block (immutable EMPTY_LIST())
    CtBlock<?> body = foo.getBody();
    CtCodeSnippetStatement snippet = factory.Core().createCodeSnippetStatement();
    List<CtStatement> statements = body.getStatements();
    statements.add(snippet);
    assertEquals(snippet, body.getStatement(0));
    // plus implicit assertion: no exception
    CtCodeSnippetStatement snippet2 = factory.Core().createCodeSnippetStatement();
    body.getStatements().add(snippet2);
    assertEquals(snippet2, body.getStatement(1));
    assertEquals(2, body.getStatements().size());
    CtCodeSnippetStatement snippet3 = factory.Core().createCodeSnippetStatement();
    statements.add(snippet3);
    assertEquals(snippet3, body.getStatement(2));
    assertEquals(3, body.getStatements().size());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)13 Test (org.junit.Test)11 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)6 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)6 CtStatement (spoon.reflect.code.CtStatement)5 CtBlock (spoon.reflect.code.CtBlock)4 CtIf (spoon.reflect.code.CtIf)4 CtMethod (spoon.reflect.declaration.CtMethod)4 Launcher (spoon.Launcher)1 Factory (spoon.reflect.factory.Factory)1 CtCodeSnippetStatementImpl (spoon.support.reflect.code.CtCodeSnippetStatementImpl)1 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)1