GeezerVison is showing github.com — [java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to by Eljees · Pull Request #7118 · pmd/pmd
Conversation
Eljees
commented
Sep 27, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Describe the PR
#4815 was a false positive: a throw inside a lambda passed to an executor was matched against the try around the call. #7105 stopped the walk at every lambda body, which fixed that, and left a false negative behind - pointed out by @UncleOwen in the review of #7105, with spring-framework's LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) as the example:
try {
this.created.forEach(info -> {
if (((PooledDataBuffer) info.getDataBuffer()).isAllocated()) {
throw info.getError();
}
});
break;
}
catch (AssertionError ex) { ... }
Iterable.forEach calls the lambda it is passed and relays an exception thrown by it to the caller, so that throw is caught by the catch right below it - the control flow this rule is about. Per @UncleOwen, the criterion is the contract of the method the lambda is passed to. @zbynek added that all methods of Stream that take a lambda should be treated the same.
The walk now continues through a lambda that is an argument of a call whose method calls that argument: Iterable.forEach, Iterator.forEachRemaining, Map.forEach, Optional.ifPresent/ifPresentOrElse, and every Stream method that takes a lambda argument. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline. Every other lambda stays opaque, so #4815 does not come back. +75/-3 in ExceptionAsFlowControlRule.
Tests
Four cases added to ExceptionAsFlowControl.xml:
- the spring-framework code above: one violation on the
catch. With the rule as on main the same fixture fails with Expected 1 problem(s), 0 problem(s) found;
- the same
forEach, but the exception type is not caught there: no violation, so the rule is not switched off inside forEach;
- a
Stream.map pipeline inside the try: one violation on the catch;
- the same
map, but the exception type is not caught there: no violation.
The two cases from #4815 are unchanged.
ExceptionAsFlowControlTest: 13 tests, 0 failures. With the rule as on current main (7774e57) the same fixtures fail in two cases, with Expected 1 problem(s), 0 problem(s) found.
The rule was also run through the PMD API on the whole spring-framework file (127 lines): no violation with the rule as on main, one violation at line 61 with this patch.
The branch is rebased on current main; docs/pages/release_notes.md lists the issue under Fixed Issues and the new reporting behaviour under Changed Rules.
Related issues
Ready?
- Added unit tests for fixed bug/feature
- Passing all unit tests
- Complete build
./mvnw clean verify passes (checked automatically by github actions)
- Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Documentation Preview
Compared to main:
This changeset changes 0 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.
Regression Tester Report
(comment created at 2026-09-30 17:50:47+00:00 for dfab14b)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Is this really about "in the same thread"? I'd say it's more about the fact that the lambda gets called by the method its passed to (e.g. forEach).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Probably all methods of Stream that accept a lambda should be treated the same.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…hod it is passed to
pmd#7105 stopped the walk at every lambda body. That fixed the false positive of
pmd#4815, but it also stopped reporting a throw inside a lambda that the method it
is passed to calls itself. spring-framework's
LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside
Iterable.forEach and catches the exception right below.
The walk now continues through a lambda that is an argument of a call whose
method calls that argument: Iterable.forEach, Iterator.forEachRemaining,
Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that
take a lambda argument. A terminal operation calls the action while the call is
evaluated; an intermediate operation is evaluated by the terminal operation of
the same pipeline. Every other lambda stays opaque, so pmd#4815 does not come back.
Signed-off-by: Eljees <[email protected]>
Eljees
changed the title
[java] ExceptionAsFlowControl: a throw in a lambda that runs in the calling thread
[java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to
Sep 28, 2026
|
Both review points are addressed and pushed:
- the criterion is now the contract of the method the lambda is passed to (@UncleOwen): the title, the description, the rule's comment and the test case names say "invoked by the method it is passed to" instead of "in the same thread";
- all methods of
Stream that take a lambda argument are treated the same (@zbynek): forEach/forEachOrdered, filter, map, peek, sorted, takeWhile, reduce, collect, anyMatch and the rest. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline.
ExceptionAsFlowControlTest is 13 cases and green; with the rule as on main two of them fail (the forEach one and the map one). The branch is rebased on current main, the PR title and description are reworded, and the release notes have entries under Fixed Issues and Changed Rules.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+43
to
+45
|
|
* The Java rule {% rule java/design/ExceptionAsFlowControl %} now reports exceptions thrown inside a lambda
|
|
|
that the method it is passed to calls itself, such as `Iterable.forEach` or a `Stream` operation that
|
|
|
takes a lambda. Exceptions thrown inside any other lambda are still not reported.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did so in the current, release version as well. So this isn't really a change.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+42
to
+76
|
|
private static final Set<String> CALLS_THE_LAMBDA_ARGUMENT = new HashSet<>(Arrays.asList(
|
|
|
"java.lang.Iterable#forEach",
|
|
|
"java.util.Iterator#forEachRemaining",
|
|
|
"java.util.Map#forEach",
|
|
|
"java.util.Optional#ifPresent",
|
|
|
"java.util.Optional#ifPresentOrElse",
|
|
|
"java.util.stream.Stream#allMatch",
|
|
|
"java.util.stream.Stream#anyMatch",
|
|
|
"java.util.stream.Stream#collect",
|
|
|
"java.util.stream.Stream#dropWhile",
|
|
|
"java.util.stream.Stream#filter",
|
|
|
"java.util.stream.Stream#flatMap",
|
|
|
"java.util.stream.Stream#flatMapToDouble",
|
|
|
"java.util.stream.Stream#flatMapToInt",
|
|
|
"java.util.stream.Stream#flatMapToLong",
|
|
|
"java.util.stream.Stream#forEach",
|
|
|
"java.util.stream.Stream#forEachOrdered",
|
|
|
"java.util.stream.Stream#generate",
|
|
|
"java.util.stream.Stream#iterate",
|
|
|
"java.util.stream.Stream#map",
|
|
|
"java.util.stream.Stream#mapMulti",
|
|
|
"java.util.stream.Stream#mapMultiToDouble",
|
|
|
"java.util.stream.Stream#mapMultiToInt",
|
|
|
"java.util.stream.Stream#mapMultiToLong",
|
|
|
"java.util.stream.Stream#mapToDouble",
|
|
|
"java.util.stream.Stream#mapToInt",
|
|
|
"java.util.stream.Stream#mapToLong",
|
|
|
"java.util.stream.Stream#max",
|
|
|
"java.util.stream.Stream#min",
|
|
|
"java.util.stream.Stream#noneMatch",
|
|
|
"java.util.stream.Stream#peek",
|
|
|
"java.util.stream.Stream#reduce",
|
|
|
"java.util.stream.Stream#sorted",
|
|
|
"java.util.stream.Stream#takeWhile"
|
|
|
));
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really looks like a CompoundInvocationMatcher. Would also simplify lines 131-135.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Replaced the manual declaring-type/method-name lookup with CompoundInvocationMatcher and its anyMatch call. Removed the Changed Rules entry and retained the Fixed Issues entry. Rebuilt the Java module and ran ExceptionAsFlowControlTest: all 13 tests pass.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
adangel
changed the title
[java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to
[java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to
Oct 1, 2026
Reviewers
zbynek
zbynek approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|