Search in sources :

Example 6 with CtCodeSnippetStatement

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

the class InsertMethodsTest method testInsertBeforeWithoutBrace.

@Test
public void testInsertBeforeWithoutBrace() 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().insertBefore(s);
    assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(0));
    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)

Example 7 with CtCodeSnippetStatement

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

the class InsertMethodsTest method testInsertBeforeSwitchCase.

@Test
public void testInsertBeforeSwitchCase() throws Exception {
    CtMethod<?> sm = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "switchMethod")).get(0);
    // Adds a new snippet in a case.
    CtSwitch<?> sw = sm.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
    CtCase<?> ctCase1 = sw.getCases().get(2);
    CtCase<?> ctCase2 = sw.getCases().get(3);
    CtCodeSnippetStatement snippet = factory.Code().createCodeSnippetStatement("System.out.println(\"foo\")");
    ctCase1.getStatements().get(0).insertBefore(snippet);
    assertEquals(snippet, ctCase1.getStatements().get(0));
    assertEquals(ctCase1, snippet.getParent());
    CtCodeSnippetStatement snippet2 = snippet.clone();
    ctCase2.getStatements().get(1).insertBefore(snippet2);
    assertEquals(snippet2, ctCase2.getStatements().get(1));
    assertEquals(ctCase2, snippet2.getParent());
    // Creates a new case.
    CtCase<Object> caseElem = factory.Core().createCase();
    CtLiteral<Object> literal = factory.Core().createLiteral();
    literal.setValue(1);
    caseElem.setCaseExpression(literal);
    // here we may call either insertBefore(CtStatement) or insertBefore(CtStatementList)
    // ctCase.insertBefore(caseElem);
    // so force the correct insert
    CtCase<?> ctCase = sw.getCases().get(1);
    ctCase.insertBefore((CtStatement) caseElem);
    assertEquals(5, sw.getCases().size());
    assertEquals(caseElem, sw.getCases().get(1));
    assertEquals(ctCase, sw.getCases().get(2));
    assertEquals(sw, caseElem.getParent());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) Test(org.junit.Test)

Example 8 with CtCodeSnippetStatement

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

the class DefaultPrettyPrinterTest method testTernaryParenthesesOnLocalVariable.

@Test
public void testTernaryParenthesesOnLocalVariable() {
    // Spooning the code snippet
    Launcher launcher = new Launcher();
    CtCodeSnippetStatement snippet = launcher.getFactory().Code().createCodeSnippetStatement("final int foo = (new Object() instanceof Object ? new Object().equals(null) : new Object().equals(new Object())) ? 0 : new Object().hashCode();");
    CtStatement compile = snippet.compile();
    // Pretty-printing the Spooned code snippet and compiling the resulting code.
    snippet = launcher.getFactory().Code().createCodeSnippetStatement(compile.toString());
    assertEquals(compile, snippet.compile());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) Test(org.junit.Test)

Example 9 with CtCodeSnippetStatement

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

the class DefaultCoreFactory method createCodeSnippetStatement.

public CtCodeSnippetStatement createCodeSnippetStatement() {
    CtCodeSnippetStatement e = new CtCodeSnippetStatementImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtCodeSnippetStatementImpl(spoon.support.reflect.code.CtCodeSnippetStatementImpl) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement)

Example 10 with CtCodeSnippetStatement

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

the class InsertMethodsTest method testInsertAfterSwitchCase.

@Test
public void testInsertAfterSwitchCase() throws Exception {
    CtMethod<?> sm = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "switchMethod")).get(0);
    // Adds a new snippet in a case.
    CtSwitch<?> sw = sm.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
    CtCase<?> ctCase1 = sw.getCases().get(2);
    CtCase<?> ctCase2 = sw.getCases().get(3);
    CtCodeSnippetStatement snippet = factory.Code().createCodeSnippetStatement("System.out.println(\"foo\")");
    ctCase1.getStatements().get(0).insertAfter(snippet);
    assertEquals(snippet, ctCase1.getStatements().get(1));
    assertEquals(ctCase1, snippet.getParent());
    CtCodeSnippetStatement snippet2 = snippet.clone();
    ctCase2.getStatements().get(1).insertAfter(snippet2);
    assertEquals(snippet2, ctCase2.getStatements().get(2));
    assertEquals(ctCase2, snippet2.getParent());
    // Creates a new case.
    CtCase<Object> caseElem = factory.Core().createCase();
    CtLiteral<Object> literal = factory.Core().createLiteral();
    literal.setValue(1);
    caseElem.setCaseExpression(literal);
    // here we may call either insertAfter(CtStatement) or insertAfter(CtStatementList)
    // ctCase.insertAfter(caseElem);
    // so force the correct insert
    CtCase<?> ctCase = sw.getCases().get(1);
    ctCase.insertAfter((CtStatement) caseElem);
    assertEquals(5, sw.getCases().size());
    assertEquals(caseElem, sw.getCases().get(2));
    assertEquals(ctCase, sw.getCases().get(1));
    assertEquals(sw, caseElem.getParent());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) 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