Search in sources :

Example 6 with CtBlock

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));
    }
}
Also used : CtBlock(spoon.reflect.code.CtBlock) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 7 with CtBlock

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);
}
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 8 with CtBlock

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) CtElement(spoon.reflect.declaration.CtElement) TemplateMatcher(spoon.template.TemplateMatcher) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 9 with CtBlock

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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteRootTemplate(spoon.test.template.testclasses.SubstituteRootTemplate) Test(org.junit.Test)

Example 10 with CtBlock

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());
}
Also used : TemplateParameter(spoon.template.TemplateParameter) CtBlock(spoon.reflect.code.CtBlock) ArrayAccessTemplate(spoon.test.template.testclasses.ArrayAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

CtBlock (spoon.reflect.code.CtBlock)37 Test (org.junit.Test)24 CtStatement (spoon.reflect.code.CtStatement)17 CtIf (spoon.reflect.code.CtIf)15 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)13 CtMethod (spoon.reflect.declaration.CtMethod)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtElement (spoon.reflect.declaration.CtElement)7 CtInvocation (spoon.reflect.code.CtInvocation)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)5 ArrayList (java.util.ArrayList)4 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)4 CtClass (spoon.reflect.declaration.CtClass)4 File (java.io.File)3 List (java.util.List)3 CtAssignment (spoon.reflect.code.CtAssignment)3 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)3 SourcePosition (spoon.reflect.cu.SourcePosition)3