GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@Eljees Eljees commented Sep 27, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • Fix [java] ExceptionAsFlowControl: False negative when the lambda is invoked by the method it is passed to #7117

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.

All reactions
@Eljees Eljees mentioned this pull request Sep 27, 2026
[java] Fix #4815: ExceptionAsFlowControl: stop at a lambda body #7105
Merged
2 of 4 tasks
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 27, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented Sep 27, 2026

Copy link
Copy Markdown
Member

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).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zbynek

zbynek commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Probably all methods of Stream that accept a lambda should be treated the same.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Eljees
[java] ExceptionAsFlowControl: a throw in a lambda invoked by the met…
4e64d1a
…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
Eljees force-pushed the fix/4815-lambda-invoked-in-calling-thread branch from 0b03905 to 4e64d1a Compare September 28, 2026 08:06
@Eljees 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
@Eljees

Eljees commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen
UncleOwen reviewed Sep 28, 2026
View reviewed changes
Comment thread docs/pages/release_notes.md Outdated
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.

@UncleOwen UncleOwen Sep 28, 2026

Copy link
Copy Markdown
Member

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.

All reactions
UncleOwen
UncleOwen reviewed Sep 28, 2026
View reviewed changes
Comment thread ...java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java Outdated
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"
));

@UncleOwen UncleOwen Sep 28, 2026

Copy link
Copy Markdown
Member

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.

All reactions
@Eljees
Use compound invocation matcher and clarify release notes
f61786a
@Eljees

Eljees commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zbynek
zbynek reviewed Sep 30, 2026
View reviewed changes
Comment thread ...java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Eljees
[java] Handle primitive streams in ExceptionAsFlowControl
dfab14b
zbynek
zbynek approved these changes Sep 30, 2026
View reviewed changes
@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 30, 2026
UncleOwen
UncleOwen reviewed Sep 30, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

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.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit 5c9271c into pmd:main Sep 30, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@adangel 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen left review comments
@zbynek zbynek zbynek approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.29.0

Development

Successfully merging this pull request may close these issues.

[java] ExceptionAsFlowControl: False negative when the lambda is invoked by the method it is passed to

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Eljees @UncleOwen @zbynek
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.