Search in sources :

Example 1 with ExecutableReferenceFilter

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

the class MethodsRefactoringTest method testExecutableReferenceFilter.

@Test
public void testExecutableReferenceFilter() {
    Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
    List<CtExecutable<?>> executables = factory.getModel().filterChildren((CtExecutable<?> e) -> true).list();
    int nrExecRefsTotal = 0;
    // contract check that ExecutableReferenceFilter found CtExecutableReferences of each executable individually
    for (CtExecutable<?> executable : executables) {
        nrExecRefsTotal += checkExecutableReferenceFilter(factory, Collections.singletonList(executable));
    }
    // contract check that ExecutableReferenceFilter found CtExecutableReferences of all executables together
    int nrExecRefsTotal2 = checkExecutableReferenceFilter(factory, executables);
    assertSame(nrExecRefsTotal, nrExecRefsTotal2);
    // contract check that it found lambdas too
    CtLambda lambda = factory.getModel().filterChildren((CtLambda<?> e) -> true).first();
    assertNotNull(lambda);
    // this test case is quite wild, because there is normally lambda reference in spoon model. So make one lambda reference here:
    CtExecutableReference<?> lambdaRef = lambda.getReference();
    List<CtExecutableReference<?>> refs = lambdaRef.filterChildren(null).select(new ExecutableReferenceFilter(lambda)).list();
    assertEquals(1, refs.size());
    assertSame(lambdaRef, refs.get(0));
}
Also used : CtLambda(spoon.reflect.code.CtLambda) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) Test(org.junit.Test)

Example 2 with ExecutableReferenceFilter

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

the class CtParameterRemoveRefactoring method computeAllInvocations.

/**
 * search for all methods and lambdas which has to be refactored together with target method
 */
private void computeAllInvocations() {
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    for (CtExecutable<?> exec : getTargetExecutables()) {
        execRefFilter.addExecutable(exec);
    }
    // all the invocations, which belongs to same inheritance tree
    final List<CtInvocation<?>> invocations = new ArrayList<>();
    target.getFactory().getModel().filterChildren(execRefFilter).forEach(new CtConsumer<CtExecutableReference<?>>() {

        @Override
        public void accept(CtExecutableReference<?> t) {
            CtElement parent = t.getParent();
            if (parent instanceof CtInvocation<?>) {
                invocations.add((CtInvocation<?>) parent);
            }
        // else ignore other hits, which are not in context of invocation
        }
    });
    targetInvocations = Collections.unmodifiableList(invocations);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter)

Example 3 with ExecutableReferenceFilter

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

the class MethodsRefactoringTest method checkExecutableReferenceFilter.

private int checkExecutableReferenceFilter(Factory factory, List<CtExecutable<?>> executables) {
    assertTrue(executables.size() > 0);
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    executables.forEach((CtExecutable<?> e) -> execRefFilter.addExecutable(e));
    final List<CtExecutableReference<?>> refs = new ArrayList<>(factory.getModel().filterChildren(execRefFilter).list());
    int nrExecRefs = refs.size();
    // use different (slower, but straight forward) algorithm to search for all executable references to check if ExecutableReferenceFilter returns correct results
    factory.getModel().filterChildren((CtExecutableReference er) -> {
        return containsSame(executables, er.getDeclaration());
    }).forEach((CtExecutableReference er) -> {
        // check that each expected reference was found by ExecutableReferenceFilter and remove it from that list
        assertTrue("Executable reference: " + er + " not found.", refs.remove(er));
    });
    // check that no other reference was found by ExecutableReferenceFilter
    assertSame(0, refs.size());
    return nrExecRefs;
}
Also used : ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) CtExecutable(spoon.reflect.declaration.CtExecutable)

Aggregations

CtExecutableReference (spoon.reflect.reference.CtExecutableReference)3 ExecutableReferenceFilter (spoon.reflect.visitor.filter.ExecutableReferenceFilter)3 ArrayList (java.util.ArrayList)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 File (java.io.File)1 Test (org.junit.Test)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtLambda (spoon.reflect.code.CtLambda)1 CtElement (spoon.reflect.declaration.CtElement)1 Factory (spoon.reflect.factory.Factory)1