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