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());
}
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));
}
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());
}
Aggregations