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));
}
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);
}
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;
}
Aggregations