Search in sources :

Example 1 with Tacos

use of spoon.test.filters.testclasses.Tacos in project spoon by INRIA.

the class FilterTest method testInvocationFilterWithExecutableInLibrary.

@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
    // contract: When we have an invocation of an executable declared in a library,
    // we can filter it and get the executable of the invocation.
    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<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            if (element.getExecutable() == null) {
                return false;
            }
            return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
    assertEquals(1, invocations.size());
    final CtInvocation<?> expectedInv = invocations.get(0);
    assertNotNull(expectedInv);
    final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
    assertNotNull(expectedExecutable);
    assertEquals("size", expectedExecutable.getSimpleName());
    assertNull(expectedExecutable.getDeclaration());
    CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
    assertEquals("size", exec.getSimpleName());
    assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
    final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
    assertNotNull(declaration);
    assertEquals("size", declaration.getSimpleName());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 2 with Tacos

use of spoon.test.filters.testclasses.Tacos in project spoon by INRIA.

the class FilterTest method testReflectionBasedTypeFilter.

@Test
public void testReflectionBasedTypeFilter() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // First collect all classes using tested TypeFilter
    List<CtClass<?>> allClasses = launcher.getFactory().Package().getRootPackage().getElements(new TypeFilter<CtClass<?>>(CtClass.class));
    assertTrue(allClasses.size() > 0);
    allClasses.forEach(result -> {
        assertTrue(result instanceof CtClass);
    });
    // then do it using Filter whose type is computed by reflection
    List<CtClass<?>> allClasses2 = launcher.getFactory().Package().getRootPackage().getElements(new Filter<CtClass<?>>() {

        @Override
        public boolean matches(CtClass<?> element) {
            return true;
        }
    });
    assertArrayEquals(allClasses.toArray(), allClasses2.toArray());
    // then do it using Filter implemented by lambda expression
    List<CtClass<?>> allClasses3 = launcher.getFactory().Package().getRootPackage().getElements((CtClass<?> element) -> true);
    assertArrayEquals(allClasses.toArray(), allClasses3.toArray());
    // last try AbstractFilter constructor without class parameter
    final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtInvocation<?> invSize = aTacos.getElements(new AbstractFilter<CtInvocation<?>>() {

        /*no class is needed here*/
        @Override
        public boolean matches(CtInvocation<?> element) {
            if (element.getExecutable() == null) {
                return false;
            }
            return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertNotNull(invSize);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CtInvocation (spoon.reflect.code.CtInvocation)2 FieldAccessFilterTacos (spoon.test.filters.testclasses.FieldAccessFilterTacos)2 Tacos (spoon.test.filters.testclasses.Tacos)2 CtClass (spoon.reflect.declaration.CtClass)1 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)1 InvocationFilter (spoon.reflect.visitor.filter.InvocationFilter)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1