use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class DeleteTest method testDeleteConditionInACondition.
@Test
public void testDeleteConditionInACondition() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m4", factory.Type().INTEGER_PRIMITIVE, factory.Type().FLOAT_PRIMITIVE, factory.Type().STRING);
final CtIf anIf = method.getElements(new TypeFilter<>(CtIf.class)).get(0);
assertNotNull(anIf.getCondition());
anIf.getCondition().delete();
assertNull(anIf.getCondition());
}
use of spoon.reflect.visitor.filter.TypeFilter 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.visitor.filter.TypeFilter in project spoon by INRIA.
the class DeleteTest method testDeleteACaseOfASwitch.
@Test
public void testDeleteACaseOfASwitch() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m3");
final CtSwitch aSwitch = method.getElements(new TypeFilter<>(CtSwitch.class)).get(0);
final CtCase aCase = (CtCase) aSwitch.getCases().get(1);
assertEquals(2, aSwitch.getCases().size());
aCase.delete();
assertEquals(1, aSwitch.getCases().size());
assertFalse(aSwitch.getCases().contains(aCase));
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class CompilationTest method testNewInstance.
@Test
public void testNewInstance() throws Exception {
// contract: a ctclass can be instantiated, and each modification results in a new valid object
Factory factory = new Launcher().getFactory();
CtClass<Ifoo> c = factory.Code().createCodeSnippetStatement("class X implements spoon.test.compilation.Ifoo { public int foo() {int i=0; return i;} }").compile();
// required otherwise java.lang.IllegalAccessException at runtime when instantiating
c.addModifier(ModifierKind.PUBLIC);
CtBlock body = c.getElements(new TypeFilter<>(CtBlock.class)).get(1);
Ifoo o = c.newInstance();
assertEquals(0, o.foo());
for (int i = 1; i <= 10; i++) {
body.getStatement(0).replace(factory.Code().createCodeSnippetStatement("int i = " + i + ";"));
o = c.newInstance();
// each time this is a new class
// each time the behavior has changed!
assertEquals(i, o.foo());
}
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocationWithAutoimports.
@Test
public void testCloneAnonymousClassInvocationWithAutoimports() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(true);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
// newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
Aggregations