Search in sources :

Example 46 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class GenericsTest method testConstructorCallGenerics.

@Test
public void testConstructorCallGenerics() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    assertEquals(2, aTacos.getFormalCtTypeParameters().size());
    final CtTypeReference interfaces = aTacos.getSuperInterfaces().toArray(new CtTypeReference[0])[0];
    assertEquals(1, interfaces.getActualTypeArguments().size());
    final CtMethod<?> m = aTacos.getMethodsByName("m").get(0);
    final CtElement local1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal1 = (CtTypeReference<Object>) local1.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal1 = (CtConstructorCall<Object>) local1.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, leftSideLocal1.getActualTypeArguments().size());
    assertEquals(1, rightSideLocal1.getType().getActualTypeArguments().size());
    assertEquals("java.util.List<java.lang.String> l = new java.util.ArrayList<>()", local1.toString());
    final CtElement local2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal2 = (CtTypeReference<Object>) local2.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    assertEquals(0, leftSideLocal2.getActualTypeArguments().size());
    assertEquals("java.util.List l2", local2.toString());
    final CtElement local3 = m.getBody().getStatement(2).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal3 = (CtTypeReference<Object>) local3.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal3 = (CtConstructorCall<Object>) local3.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(2, leftSideLocal3.getActualTypeArguments().size());
    assertEquals(2, rightSideLocal3.getType().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.IBurritos<?, ?> burritos = new Burritos<>()", local3.toString());
    final CtElement local4 = m.getBody().getStatement(3).getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    final CtTypeReference<Object> leftSideLocal4 = (CtTypeReference<Object>) local4.getElements(new ReferenceTypeFilter<>(CtTypeReference.class)).get(0);
    final CtConstructorCall<Object> rightSideLocal4 = (CtConstructorCall<Object>) local4.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, leftSideLocal4.getActualTypeArguments().size());
    assertEquals(1, rightSideLocal4.getType().getActualTypeArguments().size());
    assertEquals("java.util.List<?> l3 = new java.util.ArrayList<java.lang.Object>()", local4.toString());
    final CtConstructorCall constructorCall1 = (CtConstructorCall) m.getBody().getStatement(4).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(1, constructorCall1.getActualTypeArguments().size());
    assertEquals(2, constructorCall1.getType().getActualTypeArguments().size());
    assertEquals("new <java.lang.Integer>spoon.test.generics.testclasses.Tacos<java.lang.Object, java.lang.String>()", constructorCall1.toString());
    final CtConstructorCall constructorCall2 = (CtConstructorCall) m.getBody().getStatement(5).getElements(new TypeFilter<>(CtConstructorCall.class)).get(0);
    assertEquals(0, constructorCall2.getActualTypeArguments().size());
    assertEquals(2, constructorCall2.getType().getActualTypeArguments().size());
    assertEquals("new spoon.test.generics.testclasses.Tacos<>()", constructorCall2.toString());
    canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 47 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class FilterTest method testQueryInQuery.

@Test
public void testQueryInQuery() throws Exception {
    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 {

        int count = 0;
    }
    Context context = new Context();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    // first query
    CtQuery allChildPublicClasses = launcher.getFactory().Query().createQuery().filterChildren((CtClass clazz) -> clazz.hasModifier(ModifierKind.PUBLIC));
    // second query,involving the first query
    CtQuery q = launcher.getFactory().Package().getRootPackage().map((CtElement in) -> allChildPublicClasses.setInput(in).list());
    // now the assertions
    q.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
    // reset
    context.count = 0;
    // again second query, but now with CtConsumableFunction
    CtQuery q2 = launcher.getFactory().Package().getRootPackage().map((CtElement in, CtConsumer<Object> out) -> allChildPublicClasses.setInput(in).forEach(out));
    // now the assertions
    q2.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
    // reset
    context.count = 0;
    // again second query, but with low-level circuitry thanks to cast
    CtQuery q3 = launcher.getFactory().Package().getRootPackage().map((in, out) -> ((CtQueryImpl) allChildPublicClasses).evaluate(in, out));
    // now the assertions
    q3.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) Test(org.junit.Test)

Example 48 with CtElement

use of spoon.reflect.declaration.CtElement 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 49 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class FilterTest method testElementMapFunction.

@Test
public void testElementMapFunction() throws Exception {
    // contract: a map(Function) can be followed by a forEach(...) or by a list()
    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();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    cls.map((CtClass<?> c) -> c.getParent()).forEach((CtElement e) -> {
        assertEquals(cls.getParent(), e);
    });
    assertEquals(cls.getParent(), cls.map((CtClass<?> c) -> c.getParent()).list().get(0));
}
Also used : CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 50 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class FilterTest method testParentFunction.

@Test
public void testParentFunction() throws Exception {
    // contract: a mapping function which returns all parents of CtElement
    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();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    CtLocalVariable<?> varStrings = cls.filterChildren(new NamedElementFilter<>(CtLocalVariable.class, "strings")).first();
    class Context {

        CtElement expectedParent;
    }
    Context context = new Context();
    context.expectedParent = varStrings;
    varStrings.map(new ParentFunction()).forEach((parent) -> {
        context.expectedParent = context.expectedParent.getParent();
        assertSame(context.expectedParent, parent);
    });
    // context.expectedParent is last visited element
    // Check that last visited element was root package
    assertSame(launcher.getFactory().getModel().getUnnamedModule(), context.expectedParent);
    // contract: if includingSelf(false), then parent of input element is first element
    assertSame(varStrings.getParent(), varStrings.map(new ParentFunction().includingSelf(false)).first());
    // contract: if includingSelf(true), then input element is first element
    assertSame(varStrings, varStrings.map(new ParentFunction().includingSelf(true)).first());
    // contract: do not fail on unitialized parent
    assertNull(factory.Type().createReference("p.T").map(new ParentFunction()).first());
}
Also used : CtElement(spoon.reflect.declaration.CtElement) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)86 Test (org.junit.Test)39 Launcher (spoon.Launcher)23 Factory (spoon.reflect.factory.Factory)18 ArrayList (java.util.ArrayList)17 CtMethod (spoon.reflect.declaration.CtMethod)17 CtType (spoon.reflect.declaration.CtType)15 CtStatement (spoon.reflect.code.CtStatement)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)14 List (java.util.List)12 CtClass (spoon.reflect.declaration.CtClass)12 CtField (spoon.reflect.declaration.CtField)12 CtIf (spoon.reflect.code.CtIf)11 CtInvocation (spoon.reflect.code.CtInvocation)11 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)11 CtLocalVariable (spoon.reflect.code.CtLocalVariable)10 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 CtScanner (spoon.reflect.visitor.CtScanner)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9 Collection (java.util.Collection)8