3

I have a very simple class and using Immutables library. The auto-generated code defines equals method like so:

  @Override
  public boolean equals(@Nullable Object another) {

The @Nullable annotation causes the following FindBugs error:

NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION: Method tightens nullness annotation on parameter

A method should always implement the contract of a method it overrides. Thus, if a method takes a parameter that is marked as @Nullable, you shouldn't override that method in a subclass with a method where that parameter is @Nonnull. Doing so violates the contract that the method should handle a null parameter.

I am using Immutables-value-2.5.6.jar

Has anyone seen this error?

I have mitigated the issue temporarily by adding:

@SuppressFBWarnings

to the Immutables class. But I don't think this is a long term solution. There must be something else I am missing.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • 1
    Do you have a subclass of the generated code, perhaps, that overrides `equals` and doesn't add the annotation? – ThrawnCA Oct 16 '17 at 00:47
  • I would consider it ok if an overriding class broadens the input it accepts, as it happens in this case. Going from `@Nullable` to `@Nonnull` would definitely be a breach of contract, while going the other way is merely an extension of the contract. FB is wrong in this case. – Henrik Aasted Sørensen Oct 18 '17 at 14:15

1 Answers1

4

This appears to be an open bug in the FindBugs project (https://sourceforge.net/p/findbugs/bugs/1385/), so I would say that disabling the warning using an annotation is fine until the next release.

This class suggests that the SpotBugs project, which is the successor to FindBugs, have addressed the issue. Perhaps consider migrating?


Update : The FindBugs issue has since been closed.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60