Is there any way to ensure "package-info.java" is present in every package (using Findbugs, Checkstyle or PMD)

StackOverflow https://stackoverflow.com/questions/20842494

  •  22-09-2022
  •  | 
  •  

Domanda

In our project we're using Findbugs, Checkstyle and PMD. One of validations from Findbugs is check for potential NullPointerExceptions. By default we're defining everything as @Nonnull on the package level (applying appropriate annotation to the package within "package-info.java").

Problem is that from time to time developers don't add this "package-info.java" to the newly created package and there is no any automatic checks to validate this.

I would like to add some custom rule to one of the listed above tools to ensure, that "package-info.java" is present in each package (we have continuous integration build, which will be broken in this case). Is there any ability to do this?

Another option will be to somehow configure Findbugs to treat everything as @Nonnull without "package-info.java" (but as far as I know - it's impossible).

È stato utile?

Soluzione

You can enable Checkstyle's JavadocPackage check. Leave the allowLegacy property at its default value of false in order to ensure that people use a package-info.java instead of package.html.

There is no documented way that I know of to change the FindBugs defaults for null analysis annotations. So your next task may be to make sure that every package-info.java contains the appropriate annotation ...


Let me add a piece of un-asked-for advice: Personally, I would advise against using the defaults annotations, and instead explicitly annotate every method argument that must be non-null. This means that the default will be nullable, but FindBugs is clever enough to check the method code for parts which assume an argument to be non-null and flag that as an error. For large code bases, this is more reliable and easier from the governance point of view. Of course this path may be unavailable to you if you've got an existing code base.

Altri suggerimenti

Not that I'm aware of, but since it isn't a "code" issue, but more a housekeeping issue, you could create a simple build step that calls a shell script to assert that for every java file, the directory should contain a package-info.java.

The shell script to do that would be only a few lines. You could have it exit with different return codes, say 0 for OK and 1 for missing, and output all directories that have the missing file and include that in the build error message.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top