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

fix CCE on Query#forEach call of CtConsumer#apply #1126

Merged
merged 1 commit into from
Jan 18, 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
7 changes: 6 additions & 1 deletion src/main/java/spoon/reflect/visitor/chain/CtQueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,12 @@ private String getName() {
}

private boolean isFailOnCCE() {
return getStep().isFailOnCCE();
AbstractStep step = getStep();
if (step == null) {
//it is final consumer. Never throw CCE on final forEach consumer
return false;
}
return step.isFailOnCCE();
}

private AbstractStep getStep() {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,4 +813,25 @@ public void testBoundQuery() throws Exception {
assertEquals(1, list.size());
assertEquals("x", list.get(0));
}

@Test
public void testClassCastExceptionOnForEach() throws Exception {
// contract: bound query, without any mapping

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();

class Context {
int count = 0;
}

Context context = new Context();
//contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
launcher.getFactory().Package().getRootPackage().filterChildren(f->{return true;}).forEach((CtType t)->{
context.count++;
});
assertTrue(context.count>0);
}
}