I've made several attempts at getting package annotation @ParametersAreNonnullByDefault to work for me in a maven project but with no success. Could someone share a link to a minimal/sample maven project where this is setup (or post the pom.xml and package-info.java and demo class)?
I'm talking about having findbugs processor enforce it for me.
The @ParametersAreNonnullByDefault annotation can be used with a package, a class, or a method. To use the annotation, add the jsr305 library to module dependencies: Open the Project Structure dialog Ctrl+Alt+Shift+S , and go to Modules | Dependencies.
JSR 305 (Annotations for Software Defect Detection) is a Java Specification Request created in 2006, which has been dormant since 2012. The JCP page doesn't provide many details, but we can read there that: This JSR would attempt to develop a standard set of annotations that can assist defect detection tools. [...]
@ParametersAreNonnullByDefault
Create a file package-info.java
in your package where you want to enforce the desired behavior.
In that file, do the following:
/** * You should do it like this! */ @ParametersAreNonnullByDefault package com.stackoverflow; import javax.annotation.ParametersAreNonnullByDefault;
@ParametersAreNonnullByDefault
Don't do the following in a Java source file:
/** * But you shouldn't do it this way! */ @ParametersAreNonnullByDefault package com.stackoverflow; import javax.annotation.ParametersAreNonnullByDefault; public class Answer { ...
Declaring annotations like this is ambiguous.
It would be OK to apply the annotation directly on Answer
class.
package com.stackoverflow; import javax.annotation.ParametersAreNonnullByDefault; /** * You can do it like this also. */ @ParametersAreNonnullByDefault public class Answer { ...
The exact same things apply to @ParametersAreNullableByDefault
also.
For Android projects (for example when using libraries like Retrofit etc), this is the dependency to put in app/build.gradle
(as mentioned by @Saket):
dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2'
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With