Search in sources :

Example 1 with OverriddenMethodQuery

use of spoon.reflect.visitor.filter.OverriddenMethodQuery in project spoon by INRIA.

the class FilterTest method testQueryBuilderWithFilterChain.

@Test
public void testQueryBuilderWithFilterChain() throws Exception {
    // contract: query methods can be lazy evaluated in a foreach
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        CtMethod<?> method;

        int count = 0;
    }
    Context context = new Context();
    // chaining queries
    CtQuery q = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<CtMethod<?>>(CtMethod.class)).map((CtMethod<?> method) -> {
        context.method = method;
        return method;
    }).map(new OverriddenMethodQuery());
    // actual evaluation
    q.forEach((CtMethod<?> method) -> {
        assertTrue(context.method.getReference().isOverriding(method.getReference()));
        assertTrue(context.method.isOverriding(method));
        context.count++;
    });
    // sanity check
    assertTrue(context.count > 0);
}
Also used : OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 2 with OverriddenMethodQuery

use of spoon.reflect.visitor.filter.OverriddenMethodQuery in project spoon by INRIA.

the class FilterTest method testOverriddenMethodFromAbstractClass.

@Test
public void testOverriddenMethodFromAbstractClass() throws Exception {
    // contract: When we declare an abstract method on an abstract class, we must return an empty list
    // when we ask all overriden methods from this declaration.
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    final CtClass<AbstractTostada> aClass = launcher.getFactory().Class().get(AbstractTostada.class);
    assertEquals(0, Query.getElements(launcher.getFactory(), new OverriddenMethodFilter(aClass.getMethodsByName("prepare").get(0))).size());
    assertEquals(0, aClass.getMethodsByName("prepare").get(0).map(new OverriddenMethodQuery()).list().size());
}
Also used : OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 3 with OverriddenMethodQuery

use of spoon.reflect.visitor.filter.OverriddenMethodQuery in project spoon by INRIA.

the class FilterTest method testOverriddenMethodFromInterface.

@Test
public void testOverriddenMethodFromInterface() throws Exception {
    // contract: When we declare a method in an interface, we must return an empty list
    // when we ask all overridden methods from this declaration.
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    final CtInterface<ITostada> aITostada = launcher.getFactory().Interface().get(ITostada.class);
    OverriddenMethodFilter filter = new OverriddenMethodFilter(aITostada.getMethodsByName("make").get(0));
    List<CtMethod<?>> overridingMethods = Query.getElements(launcher.getFactory(), filter);
    assertEquals(0, overridingMethods.size());
    List<CtMethod> overridingMethods2 = aITostada.getMethodsByName("make").get(0).map(new OverriddenMethodQuery()).list(CtMethod.class);
    assertEquals(0, overridingMethods2.size());
}
Also used : OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Launcher(spoon.Launcher) ITostada(spoon.test.filters.testclasses.ITostada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 Launcher (spoon.Launcher)3 OverriddenMethodQuery (spoon.reflect.visitor.filter.OverriddenMethodQuery)3 CtMethod (spoon.reflect.declaration.CtMethod)2 OverriddenMethodFilter (spoon.reflect.visitor.filter.OverriddenMethodFilter)2 CtQuery (spoon.reflect.visitor.chain.CtQuery)1 AbstractTostada (spoon.test.filters.testclasses.AbstractTostada)1 ITostada (spoon.test.filters.testclasses.ITostada)1