Search in sources :

Example 16 with CtStatement

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

the class SwitchCaseTest method insertAfterStatementInSwitchCaseWithoutException.

@Test
public void insertAfterStatementInSwitchCaseWithoutException() throws Exception {
    String packageName = "spoon.test.ctCase";
    String className = "ClassWithSwitchExample";
    Factory factory = factoryFor(packageName, className);
    List<CtCase> elements = elementsOfType(CtCase.class, factory);
    assertEquals(3, elements.size());
    CtCase firstCase = elements.get(0);
    List<CtStatement> statements = firstCase.getStatements();
    assertEquals(2, statements.size());
    CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
    statements.get(0).insertAfter(newStatement);
    statements = firstCase.getStatements();
    assertEquals(3, statements.size());
    assertTrue(statements.get(1) == newStatement);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 17 with CtStatement

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

the class TestCtBlock method testRemoveStatement.

@Test
public void testRemoveStatement() {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
    spoon.buildModel();
    List<CtMethod> methods = spoon.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "foo"));
    assertEquals(1, methods.size());
    CtMethod foo = methods.get(0);
    CtBlock block = foo.getBody();
    CtStatement lastStatement = block.getLastStatement();
    assertEquals("i++", lastStatement.toString());
    block.removeStatement(lastStatement);
    CtStatement newLastStatement = block.getLastStatement();
    assertTrue(newLastStatement != lastStatement);
    assertTrue(newLastStatement instanceof CtIf);
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 18 with CtStatement

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

the class FilterTest method testLineFilter.

@Test
public void testLineFilter() throws Exception {
    CtType<FooLine> foo = ModelUtils.buildClass(FooLine.class);
    CtMethod method = foo.getMethod("simple");
    List<CtStatement> expressions = method.getElements(new LineFilter());
    assertEquals(3, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    method = foo.getMethod("loopBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtLoop);
    method = foo.getMethod("loopNoBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtLoop);
    method = foo.getMethod("ifBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtIf);
    method = foo.getMethod("ifNoBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtIf);
    method = foo.getMethod("switchBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(3, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtSwitch);
    assertTrue(expressions.get(2).getParent(new LineFilter()) instanceof CtSwitch);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtSwitch(spoon.reflect.code.CtSwitch) CtMethod(spoon.reflect.declaration.CtMethod) CtLoop(spoon.reflect.code.CtLoop) CtIf(spoon.reflect.code.CtIf) LineFilter(spoon.reflect.visitor.filter.LineFilter) Test(org.junit.Test)

Example 19 with CtStatement

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

the class ImportTest method testImportStaticAndFieldAccessWithImport.

@Test
public void testImportStaticAndFieldAccessWithImport() throws Exception {
    // contract: Qualified field access and an import static with import should import the type first, and not use static import
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--with-imports" });
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/");
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/Tacos.java");
    launcher.buildModel();
    final CtType<Object> aTacos = launcher.getFactory().Type().get(Tacos.class);
    final CtStatement assignment = aTacos.getMethod("m").getBody().getStatement(0);
    assertTrue(assignment instanceof CtLocalVariable);
    assertEquals("Constants.CONSTANT.foo", ((CtLocalVariable) assignment).getAssignment().toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 20 with CtStatement

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

the class TemplateInvocationSubstitutionTest method testInvocationSubstitutionByStatement.

@Test
public void testInvocationSubstitutionByStatement() throws Exception {
    // contract: the template engine supports substitution of any method invocation
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationSubstitutionByStatementTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtBlock<?> templateArg = factory.Class().get(InvocationSubstitutionByStatementTemplate.class).getMethod("sample").getBody();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtStatement result = new InvocationSubstitutionByStatementTemplate(templateArg).apply(resultKlass);
    assertEquals("throw new java.lang.RuntimeException(\"Failed\")", result.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationSubstitutionByStatementTemplate(spoon.test.template.testclasses.InvocationSubstitutionByStatementTemplate) Test(org.junit.Test)

Aggregations

CtStatement (spoon.reflect.code.CtStatement)84 Test (org.junit.Test)55 Factory (spoon.reflect.factory.Factory)35 CtMethod (spoon.reflect.declaration.CtMethod)23 Launcher (spoon.Launcher)20 CtBlock (spoon.reflect.code.CtBlock)17 CtIf (spoon.reflect.code.CtIf)11 CtExpression (spoon.reflect.code.CtExpression)10 ArrayList (java.util.ArrayList)9 CtElement (spoon.reflect.declaration.CtElement)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtInvocation (spoon.reflect.code.CtInvocation)8 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 CtCase (spoon.reflect.code.CtCase)6 CtStatementList (spoon.reflect.code.CtStatementList)6 STATEMENT (spoon.reflect.path.CtRole.STATEMENT)6 FileSystemFile (spoon.support.compiler.FileSystemFile)6 Adobada (spoon.test.delete.testclasses.Adobada)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5