Menu

#1355 Incompatibility of null checks between Eclipse and Findbugs

3.x
closed-rejected
nobody
None
5
2017-10-23
2015-02-05
No

Incompatibility between Eclipse and Findbugs:

Eclipse is supporting 3 different (customizable) types of null check annotations

  1. @Nullable -> element can be null
  2. @NonNull -> element must never be null
  3. @NonNullByDefault -> all elements should not be null in this package/class

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcompiler%2Fref-preferences-errors-warnings.htm&anchor=null_analysis

JSR 305/Findbugs has the following annotations

  1. @Nullable -> element can be null but basically no check inferred from this
  2. @CheckForNull -> only allowed on methods, specifies that the return value should be checked for null
  3. @NonNull -> same as Eclipse
  4. @ParametersAreNonNullByDefault -> almost the same as within Eclipse, but will not apply to return values!

The basic problem now is: Which annotations should I use to get the same checks within Eclipse and Findbugs?
If I want to rely on the @ParametersAreNonNullByDefault annotation, for Findbugs I have to rely on the 2 other annotations for indicating that something may be null as well: I would need to annotate methods which may return null with @CheckForNull and parameters which may be null with @Nullable (due to the @ParametersAreNonNullByDefault annotation)
Unfortunately I can configure Eclipse only to understand one(!) annotation for a parameter/return value which may be null (so either @CheckForNull or @Nullable is ignored by Eclipse).

To be compatible with Eclipse and Findbugs I would have to restrict myself to the following 2 annotations:

  1. @CheckForNull (must be defined as Nullable annotation in Eclipse)
  2. @NonNull (must be defined as NonNull annotation in Eclipse)

That way I would have to annotate methods with @CheckForNull whose return value should be checked for null and @NonNull for all parameters which are not supposed to be null (which is most of the parameters in a good API).
The drawback is that this is quite verbose!

Is there any way to use the @ParametersAreNonNullByDefault and still be compatible with Eclipse? A switch in Findbugs would be good to treat @Nullable exactly like @CheckForNull (in case it is set on a method).

Discussion

  • Andrey Loskutov

    Andrey Loskutov - 2017-10-22
    • Status: open --> closed-rejected
     
  • Konrad Windszus

    Konrad Windszus - 2017-10-23
     

    Last edit: Konrad Windszus 2017-10-23

Log in to post a comment.

MongoDB Logo MongoDB