Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-459] Avoid NullPointerException where there is no plugins #193

Merged
merged 3 commits into from
Jan 10, 2023

Conversation

turbanoff
Copy link
Contributor

When I debugged build of my project I noticed that maven-enforcer-plugin generates lots of NPEs with this stack trace:

  at java.util.ArrayList.addAll(ArrayList.java:702)
  at org.apache.maven.plugins.enforcer.RequirePluginVersions.getReportingPlugins(RequirePluginVersions.java:1062)
  at org.apache.maven.plugins.enforcer.RequirePluginVersions.getAllPluginEntries(RequirePluginVersions.java:970)
  at org.apache.maven.plugins.enforcer.RequirePluginVersions.execute(RequirePluginVersions.java:237)
  at org.apache.maven.plugins.enforcer.EnforceMojo.execute(EnforceMojo.java:199)
  - locked <0x1eb6> (a org.apache.maven.plugins.enforcer.RequirePluginVersions)
  at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
  at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:301)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:211)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:165)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:157)
  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:121)
  at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:210)
  at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:195)
  at java.util.concurrent.FutureTask.run(FutureTask.java:264)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
  at java.util.concurrent.FutureTask.run(FutureTask.java:264)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  at java.lang.Thread.run(Thread.java:829)

Exceptions shouldn't be used for non-exceptional flow.
Also it's considered bad practice to return null from a method which returns collection. Empty collection should be returned instead.
I checked all current usages of this method. All of them use this pattern:

        try
        {
            //...
            plugins.addAll( PluginWrapper.addAll( /*..*/ );
        }
        catch ( NullPointerException e )
        {
            // guess there are no plugins here.
        }

So, it's safe to change return value to emptyList() - nothing will be added to plugins in either case.

Exceptions shouldn't be used for non-exceptional flow.
Also it's considered bad practice to return null from a method which returns collection.
@slawekjaranowski
Copy link
Member

Thanks for fix
I assume that we can also remove catch ( NullPointerException e ) patterns 😄

Please resolve conflicts, and create an issue for tracking.

@turbanoff
Copy link
Contributor Author

Created issue https://issues.apache.org/jira/browse/MENFORCER-459

Andrey Turbanov added 2 commits January 10, 2023 10:51
…pper

# Conflicts:
#	enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/utils/PluginWrapper.java
@slawekjaranowski slawekjaranowski changed the title Avoid NullPointerException where there is no plugins [MENFORCER-459] Avoid NullPointerException where there is no plugins Jan 10, 2023
@slawekjaranowski slawekjaranowski merged commit c04468d into apache:master Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants