Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature CtQuery#filterChildren(null) #1140

Merged
merged 2 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/spoon/reflect/visitor/chain/CtQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public interface CtQuery extends CtQueryable {
* The elements which throw {@link ClassCastException} during {@link Filter#matches(CtElement)}
* are considered as **not matching**, ie. are excluded.
*
* @param filter used to filter scanned children elements of the AST tree
* @param filter used to filter scanned children elements of the AST tree.
* If null then all children elements pass to next step.
* @return this to support fluent API
*/
@Override
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/spoon/reflect/visitor/chain/CtQueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,14 @@ private void processFilter(CtElement element) {
if (element == null) {
return;
}
boolean matches = false;
try {
matches = filter.matches(element);
} catch (ClassCastException e) {
onClassCastException(next, e, element);
return;
boolean matches = true;
if (filter != null) {
try {
matches = filter.matches(element);
} catch (ClassCastException e) {
onClassCastException(next, e, element);
return;
}
}
if (matches) {
//send input to output, because Fitler.matches returned true
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

Expand Down Expand Up @@ -548,6 +549,27 @@ public void testFilterQueryStep() throws Exception {
assertTrue(expectedList.size()>0);
}

@Test
public void testFilterChildrenWithoutFilterQueryStep() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput","--level","info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();

//Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
Iterator<CtElement> iter = list.iterator();
launcher.getFactory().Package().getRootPackage().filterChildren(e->{return true;}).forEach(real->{
//the elements produced by each query are same
CtElement expected = iter.next();
if(real!=expected) {
assertEquals(expected, real);
}
});
assertTrue(list.size()>0);
assertTrue(iter.hasNext()==false);
}

@Test
public void testFunctionQueryStep() throws Exception {
final Launcher launcher = new Launcher();
Expand Down