Search in sources :

Example 1 with CtCodeSnippetStatement

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;
}
Also used : CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement)

Example 2 with CtCodeSnippetStatement

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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 3 with CtCodeSnippetStatement

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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 4 with CtCodeSnippetStatement

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());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 5 with CtCodeSnippetStatement

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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) 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