Search in sources :

Example 26 with CtPackage

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);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) CtModule(spoon.reflect.declaration.CtModule) CtType(spoon.reflect.declaration.CtType) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) Iterator(java.util.Iterator) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Test(org.junit.Test)

Example 27 with CtPackage

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));
}
Also used : Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) Adobada(spoon.test.delete.testclasses.Adobada) Test(org.junit.Test)

Example 28 with CtPackage

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());
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 29 with CtPackage

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());
}
Also used : Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModel(spoon.reflect.CtModel) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 30 with CtPackage

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());
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModel(spoon.reflect.CtModel) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2