Skip to content

Avoid NPE when combined location strategy sub strategies is immutable…#639

Merged
garydgregory merged 1 commit into
apache:masterfrom
weih-kahoot:avoid-immutable-list-npe
May 21, 2026
Merged

Avoid NPE when combined location strategy sub strategies is immutable…#639
garydgregory merged 1 commit into
apache:masterfrom
weih-kahoot:avoid-immutable-list-npe

Conversation

@weih-kahoot
Copy link
Copy Markdown
Contributor

@weih-kahoot weih-kahoot commented May 21, 2026

In our project, we use CombiendLocationStrategy(List.of(...)), this will result a NPE as List#contains will be called for the passed-in immutable list instance.

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@weih-kahoot
Copy link
Copy Markdown
Contributor Author

mvn failed for me locally with the following log, I don't think it's related to my change though.

[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:31: warning: no comment
[WARNING] protected int maxNextCharInd = 0;
[WARNING] ^
[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:25: warning: no comment
[WARNING] protected boolean prevCharIsCR = false;
[WARNING] ^
[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:26: warning: no comment
[WARNING] protected boolean prevCharIsLF = false;
[WARNING] ^
[WARNING] 100 warnings
[ERROR] Error while creating javadoc report: Project contains Javadoc Warnings
org.apache.maven.reporting.MavenReportException: Project contains Javadoc Warnings
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.doExecuteJavadocCommandLine (AbstractJavadocMojo.java:5146)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.executeJavadocCommandLine (AbstractJavadocMojo.java:4987)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.executeReport (AbstractJavadocMojo.java:2059)
    at org.apache.maven.plugins.javadoc.JavadocReport.generate (JavadocReport.java:142)
    at org.apache.maven.plugins.javadoc.JavadocReport.doExecute (JavadocReport.java:307)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.execute (AbstractJavadocMojo.java:1851)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:919)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:285)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:207)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:565)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:362)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)

Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

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

Hello @weih-kahoot

Thank you for your PR.

Please see my comments.

final Collection<FileLocationStrategy> collectionThatThrowsNPE = new ArrayList<FileLocationStrategy>(Arrays.asList(getSubStrategies())) {
@Override
public boolean contains(Object o) {
if (o == null) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a test, use assertNotNull().

@Override
public boolean contains(Object o) {
if (o == null) {
throw new NullPointerException("Collection does not support null");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use Objects.requireNonNull().

// Create a collection that throws NPE on contains(null) like ImmutableList does
final Collection<FileLocationStrategy> collectionThatThrowsNPE = new ArrayList<FileLocationStrategy>(Arrays.asList(getSubStrategies())) {
@Override
public boolean contains(Object o) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use final.

*/
@Test
void testInitCollectionThrowsNPEOnContainsNull() {
// Create a collection that throws NPE on contains(null) like ImmutableList does
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whose ImmutableList? Don't you mean implementations like java.util.ImmutableCollections.List12.indexOf(Object)?

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.

Yes, I've slightly updated the comment.

@garydgregory
Copy link
Copy Markdown
Member

mvn failed for me locally with the following log, I don't think it's related to my change though.

[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:31: warning: no comment
[WARNING] protected int maxNextCharInd = 0;
[WARNING] ^
[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:25: warning: no comment
[WARNING] protected boolean prevCharIsCR = false;
[WARNING] ^
[WARNING] /home/weih/sources/commons-configuration/target/generated-sources/javacc/org/apache/commons/configuration2/plist/SimpleCharStream.java:26: warning: no comment
[WARNING] protected boolean prevCharIsLF = false;
[WARNING] ^
[WARNING] 100 warnings
[ERROR] Error while creating javadoc report: Project contains Javadoc Warnings
org.apache.maven.reporting.MavenReportException: Project contains Javadoc Warnings
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.doExecuteJavadocCommandLine (AbstractJavadocMojo.java:5146)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.executeJavadocCommandLine (AbstractJavadocMojo.java:4987)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.executeReport (AbstractJavadocMojo.java:2059)
    at org.apache.maven.plugins.javadoc.JavadocReport.generate (JavadocReport.java:142)
    at org.apache.maven.plugins.javadoc.JavadocReport.doExecute (JavadocReport.java:307)
    at org.apache.maven.plugins.javadoc.AbstractJavadocMojo.execute (AbstractJavadocMojo.java:1851)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:919)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:285)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:207)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
    at java.lang.reflect.Method.invoke (Method.java:565)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:362)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)

You are correct, these Javadoc warning occur in generated code and we are ignoring them for now.

@garydgregory
Copy link
Copy Markdown
Member

garydgregory commented May 21, 2026

Hello @weih-kahoot

You MUST run mvn before you push (without arguments, to run the default goal), and then fix whatever comes up. This is the mentioned in the description's checklist!

TY!

@weih-kahoot
Copy link
Copy Markdown
Contributor Author

weih-kahoot commented May 21, 2026

@garydgregory Sorry, I forgot that. I have pushed another commit to fix checkstyle errors(we should squash my commits before merging).

@garydgregory
Copy link
Copy Markdown
Member

@garydgregory Sorry, I forgot that. I have pushed another commit to fix checkstyle errors(we should squash my commits before merging).

I normally squash before a merge but that's something you can also do on your end.

@weih-kahoot weih-kahoot force-pushed the avoid-immutable-list-npe branch from f33cfbf to 0733706 Compare May 21, 2026 12:47
@weih-kahoot
Copy link
Copy Markdown
Contributor Author

@garydgregory Okay, I've squashed my commits

@garydgregory garydgregory merged commit 694c5c2 into apache:master May 21, 2026
8 of 9 checks passed
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.

2 participants