Search in sources :

Example 81 with CtStatement

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

the class AnonymousExecutableTest method testStatements.

@Test
public void testStatements() throws Exception {
    CtType<?> type = build("spoon.test.model", "AnonymousExecutableClass");
    CtAnonymousExecutable anonexec = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
    List<CtStatement> stats = anonexec.getBody().getStatements();
    assertEquals(1, stats.size());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 82 with CtStatement

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

the class BlockTest method testAddEmptyBlock.

@Test
public void testAddEmptyBlock() {
    // specifies a bug found by Benoit Cornu on August 7 2014
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " " + "}};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    // empty block (immutable EMPTY_LIST())
    CtBlock<?> body = foo.getBody();
    CtCodeSnippetStatement snippet = factory.Core().createCodeSnippetStatement();
    List<CtStatement> statements = body.getStatements();
    statements.add(snippet);
    assertEquals(snippet, body.getStatement(0));
    // plus implicit assertion: no exception
    CtCodeSnippetStatement snippet2 = factory.Core().createCodeSnippetStatement();
    body.getStatements().add(snippet2);
    assertEquals(snippet2, body.getStatement(1));
    assertEquals(2, body.getStatements().size());
    CtCodeSnippetStatement snippet3 = factory.Core().createCodeSnippetStatement();
    statements.add(snippet3);
    assertEquals(snippet3, body.getStatement(2));
    assertEquals(3, body.getStatements().size());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 83 with CtStatement

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

the class BlockTest method testIterationStatements.

@Test
public void testIterationStatements() {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;int y=0;" + "}};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    CtBlock<?> body = foo.getBody();
    assertEquals(2, body.getStatements().size());
    List<CtStatement> l = new ArrayList<CtStatement>();
    // this compiles (thanks to the new CtBlock extends CtStatementList)
    for (CtStatement s : body) {
        l.add(s);
    }
    assertTrue(body.getStatements().equals(l));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 84 with CtStatement

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

the class SwitchCaseTest method testIterationStatements.

@Test
public void testIterationStatements() {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;" + "switch(x) {" + "case 0: x=x+1;break;" + "case 1: x=0;" + "default: x=-1;" + "}" + "}};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    CtSwitch<?> sw = foo.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
    assertEquals(3, sw.getCases().size());
    CtCase<?> c = (CtCase<?>) sw.getCases().get(0);
    assertEquals(0, ((CtLiteral<?>) c.getCaseExpression()).getValue());
    assertEquals(2, c.getStatements().size());
    List<CtStatement> l = new ArrayList<CtStatement>();
    // this compiles (thanks to the new CtCase extends CtStatementList)
    for (CtStatement s : c) {
        l.add(s);
    }
    assertTrue(c.getStatements().equals(l));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) 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