use of spoon.reflect.code.CtBlock in project spoon by INRIA.
the class TestCtBlock method testAddStatementInBlock.
@Test
public void testAddStatementInBlock() {
// contract: we can add a statement at a specific index
// the statements with a higher index are pushed after
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();
CtClass<?> toto = spoon.getFactory().Class().get(Toto.class);
CtMethod foo = toto.getMethodsByName("foo").get(0);
CtBlock block = foo.getBody();
CtBlock originalBlock = block.clone();
assertEquals(4, block.getStatements().size());
block.addStatement(1, (CtStatement) spoon.getFactory().createInvocation().setExecutable(foo.getReference()));
assertEquals(5, block.getStatements().size());
assertEquals(originalBlock.getStatement(0), block.getStatement(0));
for (int i = 1; i < 4; i++) {
assertEquals(originalBlock.getStatement(i), block.getStatement(i + 1));
}
}
use of spoon.reflect.code.CtBlock 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.CtBlock in project spoon by INRIA.
the class TemplateTest method testTemplateMatcherWithWholePackage.
@Test
public void testTemplateMatcherWithWholePackage() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/ContextHelper.java");
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/BServiceImpl.java");
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0);
CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
assertEquals(1, matches.size());
CtElement match = matches.get(0);
assertTrue("Match is not a if", match instanceof CtIf);
CtElement matchParent = match.getParent();
assertTrue("Match parent is not a block", matchParent instanceof CtBlock);
CtElement matchParentParent = matchParent.getParent();
assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod);
CtMethod methodHello = (CtMethod) matchParentParent;
assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName());
CtElement methodParent = methodHello.getParent();
assertTrue("Parent of the method is not a class", methodParent instanceof CtClass);
CtClass bservice = (CtClass) methodParent;
assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
}
use of spoon.reflect.code.CtBlock in project spoon by INRIA.
the class TemplateTest method testStatementTemplateRootSubstitution.
@Test
public void testStatementTemplateRootSubstitution() throws Exception {
// contract: the template engine supports substitution of root element
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteRootTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> templateClass = factory.Class().get(SubstituteRootTemplate.class);
CtBlock<Void> templateParam = (CtBlock) templateClass.getMethod("sampleBlock").getBody();
CtClass<?> resultKlass = factory.Class().create("Result");
CtStatement result = new SubstituteRootTemplate(templateParam).apply(resultKlass);
assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) result).getStatement(0).toString());
}
use of spoon.reflect.code.CtBlock in project spoon by INRIA.
the class TemplateTest method testTemplateArrayAccess.
@Test
public void testTemplateArrayAccess() throws Exception {
// contract: the template engine supports substitution of arrays of parameters.
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ArrayAccessTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> resultKlass = factory.Class().create("Result");
CtClass<?> templateClass = factory.Class().get(ArrayAccessTemplate.class);
// create array of template parameters, which contains CtBlocks
TemplateParameter[] params = templateClass.getMethod("sampleBlocks").getBody().getStatements().toArray(new TemplateParameter[0]);
new ArrayAccessTemplate(params).apply(resultKlass);
CtMethod<?> m = resultKlass.getMethod("method");
// check that both TemplateParameter usages were replaced by appropriate parameter value and that substitution which miss the value is silently removed
assertEquals(2, m.getBody().getStatements().size());
assertTrue(m.getBody().getStatements().get(0) instanceof CtBlock);
assertEquals("int i = 0", ((CtBlock) m.getBody().getStatements().get(0)).getStatement(0).toString());
assertTrue(m.getBody().getStatements().get(1) instanceof CtBlock);
assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) m.getBody().getStatements().get(1)).getStatement(0).toString());
// check that both @Parameter usage was replaced by appropriate parameter value
CtMethod<?> m2 = resultKlass.getMethod("method2");
assertEquals("java.lang.System.out.println(\"second\")", m2.getBody().getStatement(0).toString());
// check that substitution by missing value correctly produces empty expression
assertEquals("java.lang.System.out.println(null)", m2.getBody().getStatement(1).toString());
}
Aggregations