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