Search in sources :

Example 1 with InvocationFilter

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

the class ExecutableRefTest method testOverridingMethod.

@Test
public void testOverridingMethod() throws Exception {
    final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);
    final CtExecutableReference<?> run = aPozole.getMethodsByName("run").get(0).getReference();
    final List<CtInvocation<?>> elements = Query.getElements(run.getFactory(), new InvocationFilter(run));
    assertEquals(1, elements.size());
    assertEquals(run, elements.get(0).getExecutable());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Pozole(spoon.test.executable.testclasses.Pozole) Test(org.junit.Test)

Example 2 with InvocationFilter

use of spoon.reflect.visitor.filter.InvocationFilter 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 3 with InvocationFilter

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

the class TemplateMatcher method getMethods.

/**
 * Searches for all invocations of {@link TemplateParameter#S()} in "root", a CtClass model of {@link Template}
 *
 * @param root CtClass model of {@link Template}
 */
private List<CtInvocation<?>> getMethods(CtClass<? extends Template<?>> root) {
    CtExecutableReference<?> methodRef = root.getFactory().Executable().createReference(root.getFactory().Type().createReference(TemplateParameter.class), root.getFactory().Type().createTypeParameterReference("T"), "S");
    List<CtInvocation<?>> meths = Query.getElements(root, new InvocationFilter(methodRef));
    return meths;
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter)

Aggregations

CtInvocation (spoon.reflect.code.CtInvocation)3 InvocationFilter (spoon.reflect.visitor.filter.InvocationFilter)3 Test (org.junit.Test)2 Launcher (spoon.Launcher)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1 Pozole (spoon.test.executable.testclasses.Pozole)1 FieldAccessFilterTacos (spoon.test.filters.testclasses.FieldAccessFilterTacos)1 Tacos (spoon.test.filters.testclasses.Tacos)1