use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class FilterTest method testCtScannerListener.
@Test
public void testCtScannerListener() throws Exception {
// contract: CtScannerFunction can be subclassed and configured by a CtScannerListener
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
long nrOfEnter = 0;
long nrOfEnterRetTrue = 0;
long nrOfExit = 0;
long nrOfResults = 0;
}
Context context1 = new Context();
// scan only packages until top level classes. Do not scan class internals
List<CtElement> result1 = launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
@Override
public ScanningMode enter(CtElement element) {
context1.nrOfEnter++;
if (element instanceof CtType) {
return ScanningMode.SKIP_CHILDREN;
}
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context1.nrOfExit++;
}
})).list();
// check that test is visiting some nodes
assertTrue(context1.nrOfEnter > 0);
assertTrue(result1.size() > 0);
// contract: if enter is called and returns SKIP_CHILDREN or NORMAL, then exit must be called too. Exceptions are ignored for now
assertEquals(context1.nrOfEnter, context1.nrOfExit);
Context context2 = new Context();
Iterator iter = result1.iterator();
// scan only from packages till top level classes. Do not scan class internals
launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
int inClass = 0;
@Override
public ScanningMode enter(CtElement element) {
context2.nrOfEnter++;
if (inClass > 0) {
// we are in class. skip this node and all children
return ScanningMode.SKIP_ALL;
}
if (element instanceof CtType) {
inClass++;
}
context2.nrOfEnterRetTrue++;
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context2.nrOfExit++;
if (element instanceof CtType) {
inClass--;
}
assertTrue(inClass == 0 || inClass == 1);
}
})).forEach(ele -> {
context2.nrOfResults++;
assertTrue("ele instanceof " + ele.getClass(), ele instanceof CtPackage || ele instanceof CtType || ele instanceof CtModule);
// check that first and second query returned same results
assertSame(ele, iter.next());
});
// check that test is visiting some nodes
assertTrue(context2.nrOfEnter > 0);
assertTrue(context2.nrOfEnter > context2.nrOfEnterRetTrue);
assertEquals(result1.size(), context2.nrOfResults);
// contract: if enter is called and does not returns SKIP_ALL, then exit must be called too. Exceptions are ignored for now
assertEquals(context2.nrOfEnterRetTrue, context2.nrOfExit);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class DeleteTest method testDeleteAClassTopLevel.
@Test
public void testDeleteAClassTopLevel() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtPackage aPackage = adobada.getParent(CtPackage.class);
assertEquals(1, aPackage.getTypes().size());
adobada.delete();
assertEquals(0, aPackage.getTypes().size());
assertFalse(aPackage.getTypes().contains(adobada));
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class ClassFactoryTest method testTopLevelClass.
@Test
public void testTopLevelClass() throws Exception {
final Factory factory = createFactory();
final CtPackage aPackage = factory.Core().createPackage();
aPackage.setSimpleName("spoon");
final CtClass<Object> topLevel = factory.Class().create(aPackage, "TopLevel");
assertEquals("TopLevel", topLevel.getSimpleName());
assertEquals(aPackage, topLevel.getPackage());
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class FactoryTest method testIncrementalModel.
@Test
public void testIncrementalModel() throws Exception {
// Feed some inputResources to a spoon compiler
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
// Build model
spoon.buildModel();
assertEquals(1, spoon.getModel().getAllTypes().size());
// Do something with that model..
CtModel model = spoon.getModel();
model.processWith(new AbstractProcessor<CtMethod>() {
@Override
public void process(CtMethod element) {
element.setDefaultMethod(false);
}
});
// Feed some new inputResources
SpoonAPI spoon2 = new Launcher();
spoon2.addInputResource("src/test/java/spoon/test/factory/testclasses2");
// Build models of newly added classes/packages
spoon2.buildModel();
assertEquals(1, spoon2.getModel().getAllTypes().size());
// attach them to the existing model.
model.getRootPackage().addPackage(spoon2.getModel().getRootPackage());
// checking the results
assertEquals(6, model.getAllPackages().size());
assertEquals(2, model.getAllTypes().size());
assertEquals(1, model.getElements(new AbstractFilter<CtPackage>() {
@Override
public boolean matches(CtPackage element) {
return "spoon.test.factory.testclasses2".equals(element.getQualifiedName());
}
}).size());
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class FactoryTest method testCtModel.
@Test
public void testCtModel() throws Exception {
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
spoon.buildModel();
CtModel model = spoon.getModel();
// contains Foo and Foo.@Bar
assertEquals(1, model.getAllTypes().size());
// [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
assertEquals(5, model.getAllPackages().size());
// add to itself is fine
model.getRootPackage().addPackage(model.getRootPackage());
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().getPackage("spoon").addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
CtPackage p = model.getElements(new NamedElementFilter<>(CtPackage.class, "spoon")).get(0).clone();
// if we change the implem, merge is impossible
CtField f = spoon.getFactory().Core().createField();
f.setSimpleName("foo");
f.setType(spoon.getFactory().Type().BYTE);
p.getElements(new NamedElementFilter<>(CtPackage.class, "testclasses")).get(0).getType("Foo").addField(f);
try {
model.getRootPackage().addPackage(p);
fail("no exception thrown");
} catch (IllegalStateException success) {
}
model.processWith(new AbstractProcessor<CtType>() {
@Override
public void process(CtType element) {
element.delete();
}
});
assertEquals(0, model.getAllTypes().size());
}
Aggregations