use of spoon.reflect.code.CtCodeSnippetStatement in project spoon by INRIA.
the class CodeFactory method createCodeSnippetStatement.
/**
* Creates a Code Snippet statement.
*
* @param statement
* The String containing the statement.
* @return a new CtCodeSnippetStatement
*/
public CtCodeSnippetStatement createCodeSnippetStatement(String statement) {
CtCodeSnippetStatement e = factory.Core().createCodeSnippetStatement();
e.setValue(statement);
return e;
}
use of spoon.reflect.code.CtCodeSnippetStatement in project spoon by INRIA.
the class InsertMethodsTest method testInsertBeforeWithBrace.
@Test
public void testInsertBeforeWithBrace() throws Exception {
CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
// replace the return
CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
// Inserts a s before the then statement
ifWithBraces.getThenStatement().insertBefore(s);
assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(0));
assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
use of spoon.reflect.code.CtCodeSnippetStatement in project spoon by INRIA.
the class InsertMethodsTest method testInsertAfterWithBrace.
@Test
public void testInsertAfterWithBrace() throws Exception {
CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
// replace the return
CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
// Inserts a s before the then statement
ifWithBraces.getThenStatement().insertAfter(s);
assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(1));
assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
use of spoon.reflect.code.CtCodeSnippetStatement in project spoon by INRIA.
the class InsertMethodsTest method testInsertAfter.
@Test
public void testInsertAfter() throws Exception {
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.insertAfter(stmt);
assertEquals(4, body.getStatements().size());
assertSame(stmt, body.getStatements().get(3));
assertEquals(body, stmt.getParent());
}
use of spoon.reflect.code.CtCodeSnippetStatement in project spoon by INRIA.
the class InsertMethodsTest method testInsertAfterWithoutBrace.
@Test
public void testInsertAfterWithoutBrace() throws Exception {
CtMethod<?> ifWithoutBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithoutBraces")).get(0);
// replace the return
CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
CtIf ifWithoutBraces = ifWithoutBraces_m.getElements(new TypeFilter<>(CtIf.class)).get(0);
// Inserts a s before the then statement
ifWithoutBraces.getThenStatement().insertAfter(s);
assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(1));
assertEquals(ifWithoutBraces.getThenStatement(), s.getParent());
}
Aggregations