Skip to content

[Backport 4.0.x] Fix NPE in DefaultLookup.lookupOptional() when container returns null - #12340

Merged
gnodet merged 1 commit into
maven-4.0.xfrom
cherry-pick-12336-to-4.0.x
Jun 22, 2026
Merged

gnodet merged 1 commit into
maven-4.0.xfrom
cherry-pick-12336-to-4.0.x

Conversation

@gnodet

@gnodet gnodet commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Backport of #12336 to maven-4.0.x.

Cherry-pick of 712b5c0 — clean, no conflicts.

Summary

  • PlexusContainer.lookup() can return null instead of throwing ComponentLookupException. DefaultLookup.lookupOptional() wraps the result with Optional.of(), which throws NullPointerException on null input. Changed to Optional.ofNullable() so a null result is correctly represented as Optional.empty().
  • This fixes maven-jdeprscan-plugin integration tests (list-default, list-forremoval) which fail on Maven 4.0.x because DefaultToolchainManager.retrieveContext() calls lookupOptional(Project.class) and the container returns null.

…#12336)

PlexusContainer.lookup() can return null instead of throwing
ComponentLookupException. Using Optional.of(null) causes a
NullPointerException. Use Optional.ofNullable() instead so that
a null lookup result is correctly represented as Optional.empty().

This fixes maven-jdeprscan-plugin integration tests (list-default,
list-forremoval) which fail on Maven 4.0.x because
DefaultToolchainManager.retrieveContext() calls
lookupOptional(Project.class) and the container returns null.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet gnodet added this to the 4.0.0-rc-6 milestone Jun 22, 2026
@gnodet gnodet added the bug Something isn't working label Jun 22, 2026
@gnodet
gnodet merged commit 780104f into maven-4.0.x Jun 22, 2026
43 of 44 checks passed
@gnodet
gnodet deleted the cherry-pick-12336-to-4.0.x branch June 22, 2026 21:33
ascheman added a commit to aschemaven/maven that referenced this pull request Jun 28, 2026
The backported NPE fix for DefaultLookup.lookupOptional (apache#12340) —
switching Optional.of to Optional.ofNullable so a null component lookup
returns an empty Optional instead of throwing NullPointerException —
shipped without a regression test.

Add DefaultLookupTest in maven-core covering both lookupOptional
overloads: null component -> empty Optional (the guarded NPE case),
present component -> value, and a ComponentLookupException whose cause is
NoSuchElementException -> empty.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ascheman added a commit to aschemaven/maven that referenced this pull request Jul 3, 2026
The backported NPE fix for DefaultLookup.lookupOptional (apache#12340) —
switching Optional.of to Optional.ofNullable so a null component lookup
returns an empty Optional instead of throwing NullPointerException —
shipped without a regression test.

Add DefaultLookupTest in maven-core covering both lookupOptional overloads
across four scenarios: null component -> empty Optional (the guarded NPE
case), present component -> value, ComponentLookupException caused by
NoSuchElementException -> empty, and any other cause -> rethrown as
LookupException.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
gnodet pushed a commit that referenced this pull request Jul 7, 2026
The backported NPE fix for DefaultLookup.lookupOptional (#12340) —
switching Optional.of to Optional.ofNullable so a null component lookup
returns an empty Optional instead of throwing NullPointerException —
shipped without a regression test.

Add DefaultLookupTest in maven-core covering both lookupOptional overloads
across four scenarios: null component -> empty Optional (the guarded NPE
case), present component -> value, ComponentLookupException caused by
NoSuchElementException -> empty, and any other cause -> rethrown as
LookupException.

Co-authored-by: Gerd Aschemann <ascheman@apache.org>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport bug Something isn't working mvn40

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant