Search in sources :

Example 6 with CtStatement

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

the class CtCaseImpl method setStatements.

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> statements) {
    if (statements == null || statements.isEmpty()) {
        this.statements = CtElementImpl.emptyList();
        return (T) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, STATEMENT, this.statements, new ArrayList<>(this.statements));
    this.statements.clear();
    for (CtStatement stmt : statements) {
        addStatement(stmt);
    }
    return (T) this;
}
Also used : STATEMENT(spoon.reflect.path.CtRole.STATEMENT) CtStatement(spoon.reflect.code.CtStatement)

Example 7 with CtStatement

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

the class CtForImpl method setForInit.

@Override
public <T extends CtFor> T setForInit(List<CtStatement> statements) {
    if (statements == null || statements.isEmpty()) {
        this.forInit = CtElementImpl.emptyList();
        return (T) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, FOR_INIT, this.forInit, new ArrayList<>(this.forInit));
    this.forInit.clear();
    for (CtStatement stmt : statements) {
        addForInit(stmt);
    }
    return (T) this;
}
Also used : FOR_INIT(spoon.reflect.path.CtRole.FOR_INIT) CtStatement(spoon.reflect.code.CtStatement)

Example 8 with CtStatement

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

the class CtForImpl method setForUpdate.

@Override
public <T extends CtFor> T setForUpdate(List<CtStatement> statements) {
    if (statements == null || statements.isEmpty()) {
        this.forUpdate = CtElementImpl.emptyList();
        return (T) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, FOR_UPDATE, this.forUpdate, new ArrayList<>(this.forUpdate));
    this.forUpdate.clear();
    for (CtStatement stmt : statements) {
        addForUpdate(stmt);
    }
    return (T) this;
}
Also used : FOR_INIT(spoon.reflect.path.CtRole.FOR_INIT) CtStatement(spoon.reflect.code.CtStatement)

Example 9 with CtStatement

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

the class CommentTest method testAddCommentsToSnippet.

@Test
public void testAddCommentsToSnippet() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
    CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
    statement.addComment(comment);
    CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
    CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
    expression.addComment(commentExpression);
    assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
    assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 10 with CtStatement

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

the class DeleteTest method testDeleteAStatementInStaticAnonymousExecutable.

@Test
public void testDeleteAStatementInStaticAnonymousExecutable() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final List<CtAnonymousExecutable> anonymousExecutables = adobada.getAnonymousExecutables();
    final CtAnonymousExecutable staticExec = anonymousExecutables.get(1);
    assertEquals(2, staticExec.getBody().getStatements().size());
    final CtStatement statement = staticExec.getBody().getStatement(1);
    statement.delete();
    assertEquals(1, staticExec.getBody().getStatements().size());
    assertFalse(staticExec.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) 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