Search in sources :

Example 1 with CtWhile

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

the class DefaultCoreFactory method createWhile.

public CtWhile createWhile() {
    CtWhile e = new CtWhileImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtWhileImpl(spoon.support.reflect.code.CtWhileImpl) CtWhile(spoon.reflect.code.CtWhile)

Example 2 with CtWhile

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

the class CtBodyHolderTest method testWhileWithBlock.

@Test
public void testWhileWithBlock() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(2, cwbClass.getMethods().size());
    CtMethod<?> method = cwbClass.getMethod("method2");
    CtBlock<?> methodBody = method.getBody();
    assertTrue(methodBody.getStatement(3) instanceof CtWhile);
    CtWhile whileStmnt = (CtWhile) methodBody.getStatement(3);
    checkCtBody(whileStmnt, "while_block", 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) CtWhile(spoon.reflect.code.CtWhile) Test(org.junit.Test)

Example 3 with CtWhile

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

the class VisitorPartialEvaluator method visitCtWhile.

public void visitCtWhile(CtWhile whileLoop) {
    CtWhile w = whileLoop.clone();
    w.setLoopingExpression(evaluate(whileLoop.getLoopingExpression()));
    // If lopping Expression always false
    if ((whileLoop.getLoopingExpression() instanceof CtLiteral) && !((CtLiteral<Boolean>) whileLoop.getLoopingExpression()).getValue()) {
        setResult(null);
        return;
    }
    w.setBody(evaluate(whileLoop.getBody()));
    setResult(w);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtWhile(spoon.reflect.code.CtWhile)

Example 4 with CtWhile

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

the class InsertMethodsTest method insertBeforeAndUpdateParent.

@Test
public void insertBeforeAndUpdateParent() throws Exception {
    /**
     * if (condition)
     *     while (loop_condition)
     *
     * In this case the 'while' is inside an implicit block, but
     * when we insert a new statement
     *
     * if (condition) {
     *     newStatement
     *     while (loop_condition)
     *     ...
     * }
     *
     * Now the while is inside an explicit block.
     */
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/spoon/test/intercession/insertBefore/InsertBeforeExample2.java")).build();
    // Get the 'while'
    List<CtWhile> elements = Query.getElements(factory, new TypeFilter<CtWhile>(CtWhile.class));
    assertTrue(1 == elements.size());
    CtWhile theWhile = elements.get(0);
    // We make sure the parent of the while is the CtIf and not the block
    CtElement parent = theWhile.getParent();
    assertTrue(parent instanceof CtBlock);
    assertTrue(parent.isImplicit());
    CtIf ifParent = (CtIf) parent.getParent();
    // Create a new statement to be inserted before the while
    CtStatement insert = factory.Code().createCodeSnippetStatement("System.out.println()");
    // Insertion of the new statement
    theWhile.insertBefore(insert);
    // We make sure the parent of the while is updated
    CtElement newParent = theWhile.getParent();
    assertTrue(newParent != ifParent);
    assertTrue(newParent instanceof CtBlock);
    assertFalse(newParent.isImplicit());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtWhile(spoon.reflect.code.CtWhile) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 5 with CtWhile

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

the class TestLabels method testLabelsAreDetected.

@Test
public void testLabelsAreDetected() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/labels/testclasses/ManyLabels.java");
    launcher.buildModel();
    CtMethod mainMethod = launcher.getFactory().getModel().getElements(new NamedElementFilter<>(CtMethod.class, "main")).get(0);
    CtBlock body = mainMethod.getBody();
    assertEquals(2, body.getStatements().size());
    CtBlock block = (CtBlock) body.getStatement(0);
    CtSwitch ctSwitch = (CtSwitch) body.getStatement(1);
    assertEquals("labelBlock", block.getLabel());
    assertEquals("sw", ctSwitch.getLabel());
    assertTrue(block.getStatement(1) instanceof CtIf);
    CtIf firstIf = (CtIf) block.getStatement(1);
    CtBlock then = firstIf.getThenStatement();
    CtBreak firstBreak = (CtBreak) then.getStatement(1);
    assertEquals("labelBlock", firstBreak.getTargetLabel());
    assertSame(block, firstBreak.getLabelledStatement());
    CtIf secondIf = (CtIf) block.getStatement(2);
    assertEquals("labelIf", secondIf.getLabel());
    CtBlock thenBlock = secondIf.getThenStatement();
    CtIf innerIf = (CtIf) thenBlock.getStatement(0);
    CtBlock innerThenBlock = innerIf.getThenStatement();
    CtBreak breakInnerIf = (CtBreak) innerThenBlock.getStatement(0);
    assertSame(secondIf, breakInnerIf.getLabelledStatement());
    CtCase firstCase = (CtCase) ctSwitch.getCases().get(0);
    List<CtStatement> statementList = firstCase.getStatements();
    assertEquals(2, statementList.size());
    CtDo ctDo = (CtDo) statementList.get(0);
    assertEquals("label", ctDo.getLabel());
    CtBreak finalBreak = (CtBreak) statementList.get(1);
    assertNull(finalBreak.getTargetLabel());
    assertNull(finalBreak.getLabelledStatement());
    CtBlock doBlock = (CtBlock) ctDo.getBody();
    CtWhile ctWhile = (CtWhile) doBlock.getStatement(1);
    assertEquals("lWhile", ctWhile.getLabel());
    CtBlock whileBlock = (CtBlock) ctWhile.getBody();
    CtFor forLoop = (CtFor) whileBlock.getStatement(0);
    CtBreak breakSwitch = (CtBreak) whileBlock.getStatement(1);
    assertEquals("sw", breakSwitch.getTargetLabel());
    assertSame(ctSwitch, breakSwitch.getLabelledStatement());
    assertEquals("forloop", forLoop.getLabel());
    CtBlock forBlock = (CtBlock) forLoop.getBody();
    assertEquals(7, forBlock.getStatements().size());
    CtIf firstForIf = (CtIf) forBlock.getStatement(1);
    CtIf secondForIf = (CtIf) forBlock.getStatement(2);
    CtIf thirdForIf = (CtIf) forBlock.getStatement(3);
    CtIf fourthForIf = (CtIf) forBlock.getStatement(4);
    CtBreak breakItself = (CtBreak) forBlock.getStatement(6);
    CtContinue continueFor = (CtContinue) ((CtBlock) firstForIf.getThenStatement()).getStatement(0);
    assertSame(forLoop, continueFor.getLabelledStatement());
    CtContinue continueWhile = (CtContinue) ((CtBlock) secondForIf.getThenStatement()).getStatement(0);
    assertSame(ctWhile, continueWhile.getLabelledStatement());
    CtContinue continueDo = (CtContinue) ((CtBlock) thirdForIf.getThenStatement()).getStatement(0);
    assertSame(ctDo, continueDo.getLabelledStatement());
    CtBreak breakDo = (CtBreak) ((CtBlock) fourthForIf.getThenStatement()).getStatement(0);
    assertSame(ctDo, breakDo.getLabelledStatement());
    assertEquals("labelbreak", breakItself.getLabel());
    assertEquals("labelbreak", breakItself.getTargetLabel());
    assertSame(breakItself, breakItself.getLabelledStatement());
}
Also used : CtBreak(spoon.reflect.code.CtBreak) CtSwitch(spoon.reflect.code.CtSwitch) CtIf(spoon.reflect.code.CtIf) CtWhile(spoon.reflect.code.CtWhile) CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtDo(spoon.reflect.code.CtDo) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod) CtContinue(spoon.reflect.code.CtContinue) Test(org.junit.Test)

Aggregations

CtWhile (spoon.reflect.code.CtWhile)6 Test (org.junit.Test)4 CtStatement (spoon.reflect.code.CtStatement)3 Launcher (spoon.Launcher)2 CtBlock (spoon.reflect.code.CtBlock)2 CtIf (spoon.reflect.code.CtIf)2 CtClass (spoon.reflect.declaration.CtClass)2 Factory (spoon.reflect.factory.Factory)2 CtAssignment (spoon.reflect.code.CtAssignment)1 CtBreak (spoon.reflect.code.CtBreak)1 CtCase (spoon.reflect.code.CtCase)1 CtContinue (spoon.reflect.code.CtContinue)1 CtDo (spoon.reflect.code.CtDo)1 CtExpression (spoon.reflect.code.CtExpression)1 CtFor (spoon.reflect.code.CtFor)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtSwitch (spoon.reflect.code.CtSwitch)1 CtElement (spoon.reflect.declaration.CtElement)1