Skip to content

Fix potential NPE in getJdkHome() with null guards - #185

Open
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-npe-getJdkHome
Open

Fix potential NPE in getJdkHome() with null guards#185
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-npe-getJdkHome

Conversation

@elharo

@elharo elharo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Fix potential NullPointerException in SelectJdkToolchainMojo.getJdkHome() by adding null guards for:

  • toolchain.getModel() returning null
  • model.getConfiguration() returning null
  • configuration.getChild("jdkHome") returning null

Uses instanceof Xpp3Dom to also prevent ClassCastException if configuration is not Xpp3Dom.

Includes unit test with ToolchainPrivate stubs that prove all three NPE scenarios against the original code.

fixes #168

.getValue();
ToolchainModel model = toolchain.getModel();
if (model == null) {
return null;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the calling code react when this method returns null? Have we just moved the NullPointerException up a method in the stack?

@elharo elharo Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.equals(null, null) returns true, and Objects.equals(null, "value") returns false. So if we cannot determine the JDK home of the selected toolchain (null), it will not match the current JDK home (unless that is also null), and the IfSame block is simply skipped the toolchain is used as-is. No NPE is moved upstream; the null is handled safely by Objects.equals.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens SelectJdkToolchainMojo.getJdkHome() against null/invalid configuration chains so JdkMode.IfSame comparisons don’t fail with NullPointerException (and now also avoid a potential ClassCastException).

Changes:

  • Add null guards for toolchain.getModel(), model.getConfiguration(), and missing jdkHome child.
  • Use instanceof Xpp3Dom before casting configuration to prevent ClassCastException.
  • Add a new unit test class covering the null-chain scenarios for getJdkHome().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/main/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojo.java Reworks getJdkHome() to be null-safe and type-safe when reading jdkHome from toolchain configuration.
src/test/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojoTest.java Adds unit tests for getJdkHome() null scenarios (and should be extended to cover non-Xpp3Dom config).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +48 to +55
@Test
void testGetJdkHomeWithMissingJdkHomeChild() throws Exception {
Xpp3Dom config = new Xpp3Dom("configuration");
ToolchainModel model = new ToolchainModel();
model.setConfiguration(config);
ToolchainPrivate toolchain = createStub(model);
assertNull(invokeGetJdkHome(toolchain));
}
Comment on lines +69 to +74
private Object invokeGetJdkHome(ToolchainPrivate toolchain) throws Exception {
SelectJdkToolchainMojo mojo = new SelectJdkToolchainMojo();
Method method = SelectJdkToolchainMojo.class.getDeclaredMethod("getJdkHome", ToolchainPrivate.class);
method.setAccessible(true);
return method.invoke(mojo, toolchain);
}
Add null guards to SelectJdkToolchainMojo.getJdkHome() to prevent
NullPointerException when:
- toolchain.getModel() returns null
- model.getConfiguration() returns null
- configuration.getChild('jdkHome') returns null

Includes unit test proving all three NPE scenarios.
@elharo
elharo force-pushed the fix-npe-getJdkHome branch from 8c8bc92 to 436191a Compare July 23, 2026 11:02
@elharo
elharo requested a review from gnodet July 23, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential NPE in SelectJdkToolchainMojo.getJdkHome() from unchecked null chain

2 participants