use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DeleteTest method testDeleteAStatementInConstructor.
@Test
public void testDeleteAStatementInConstructor() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtConstructor<Adobada> constructor = adobada.getConstructor();
assertEquals(3, constructor.getBody().getStatements().size());
final CtStatement statement = constructor.getBody().getStatement(1);
statement.delete();
assertEquals(2, constructor.getBody().getStatements().size());
assertFalse(constructor.getBody().getStatements().contains(statement));
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DeleteTest method testDeleteStatementInCase.
@Test
public void testDeleteStatementInCase() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m3");
final CtCase aCase = method.getElements(new TypeFilter<>(CtCase.class)).get(0);
assertEquals(2, aCase.getStatements().size());
final CtStatement statement = aCase.getStatements().get(1);
statement.delete();
assertEquals(1, aCase.getStatements().size());
assertFalse(aCase.getStatements().contains(statement));
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DeleteTest method testDeleteReturn.
@Test
public void testDeleteReturn() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m2");
assertEquals(1, method.getBody().getStatements().size());
final CtStatement statement = method.getBody().getStatement(0);
statement.delete();
assertEquals(0, method.getBody().getStatements().size());
assertFalse(method.getBody().getStatements().contains(statement));
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ConditionalTest method testNoBlockInConditionAndLoop.
@Test
public void testNoBlockInConditionAndLoop() throws Exception {
String newLine = System.getProperty("line.separator");
final CtType<Foo> aFoo = ModelUtils.buildClass(Foo.class);
CtMethod<Object> method = aFoo.getMethod("m3");
final List<CtIf> conditions = method.getElements(new TypeFilter<CtIf>(CtIf.class));
for (int i = 0; i < conditions.size(); i++) {
CtIf ctIf = conditions.get(i);
// replace the block to a statement
CtStatement then = ((CtBlock) ctIf.getThenStatement()).getStatement(0);
ctIf.setThenStatement(then);
if (ctIf.getElseStatement() != null) {
CtStatement elseStatement = ((CtBlock) ctIf.getElseStatement()).getStatement(0);
ctIf.setElseStatement(elseStatement);
}
}
assertEquals("if (true)" + newLine + " java.lang.System.out.println();" + newLine + "else if (true)" + newLine + " java.lang.System.out.println();" + newLine + "else" + newLine + " java.lang.System.out.println();" + newLine, method.getBody().getStatement(0).toString());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtBodyHolderTest method checkCtBody.
private void checkCtBody(CtBodyHolder p_bodyHolder, String p_constant, int off) {
CtStatement body = p_bodyHolder.getBody();
assertTrue(body instanceof CtBlock<?>);
CtBlock<?> block = (CtBlock) body;
assertEquals(1 + off, block.getStatements().size());
assertTrue(block.getStatement(off) instanceof CtAssignment);
CtAssignment assignment = block.getStatement(off);
assertEquals(p_constant, ((CtLiteral<String>) assignment.getAssignment().partiallyEvaluate()).getValue());
Factory f = body.getFactory();
CtStatement newStat = new CWBStatementTemplate("xx").apply(body.getParent(CtType.class));
try {
newStat.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
// try to set statement and get CtBlock
p_bodyHolder.setBody(newStat);
CtBlock newBlock = (CtBlock) p_bodyHolder.getBody();
assertSame(p_bodyHolder, newBlock.getParent());
assertSame(newBlock, newStat.getParent());
// try to set CtBlock and get the same CtBlock
CtStatement newStat2 = newStat.clone();
try {
newStat2.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
CtBlock newBlock2 = f.Code().createCtBlock(newStat2);
assertSame(newBlock2, newStat2.getParent());
try {
newBlock2.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
p_bodyHolder.setBody(newBlock2);
assertSame(newBlock2, p_bodyHolder.getBody());
assertSame(p_bodyHolder, newBlock2.getParent());
assertSame(newBlock2, newStat2.getParent());
}
Aggregations