0

I have an interface with a default method and a private method, where the private method is called from the default method.

When running Spotbugs, it issues an error that the private method is never called: UPM_UNCALLED_PRIVATE_METHOD.

public interface Foo {

    default boolean foo(int value1, int value2) {
        return bar(value1 + value2);
    }

    private boolean bar(int value) {
        return value == 0;
    }

}

I'm doing something wrong or it's a Spotbugs issue?

Note 1: When modifying the private method to static, it doesn't report the error.

Note 2: I've seen similar issues reported, but all are closed and related to class instead of interface.


UPDATE

A similar issue was recently reported on Github (#1988), but hasn't been solved yet.

Oboe
  • 2,643
  • 2
  • 9
  • 17

1 Answers1

1

Spotbugs 4.5.3 reports this as "low priority" bug.

IMHO you have basically two options:

The solution with the annotation is probably easier to implement but clutters your source code.

The solution with the filter file might be harder to implement (if you do not use a filter file already) and the connection between the actual code and the filter might get lost, but your source code is not cluttered with annotations just to mute a noisy tool.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Thanks for the answer. I'm already using a filter file that includes this case. Just wanted to be sure I'm not using `interface` private method in a way I'm not supposed to. Where did you find that '*Spotbugs 4.5.3 reports this as "low priority" bug*' ? – Oboe Jan 29 '22 at 21:47
  • @Oboe i use spotbugs myself (it runs as part of the gradle build process) and its report contains bugs in three different colors: red for high priority bugs, orange for medium priority bugs and green for low priority bugs. These levels matches with the report option on the [CLI](https://spotbugs.readthedocs.io/en/stable/running.html): `-high`, `-medium` and `-low`. UPM is reported in a green color, indicating its a "low priority" bug. – Thomas Kläger Jan 29 '22 at 22:01
  • Thanks for the clarification. To make sure I understood your answer, do you think this is a spotbugs issue that I should report rather than a bug in my code? – Oboe Jan 30 '22 at 15:39
  • @Oboe IMHO yes this is a bug in spotbugs. The default method `boolean foo(int value1, int value2)` calls the `boolean bar(int value)` method. Spotbugs cannot know if there is a implementation of the `Foo` interface somewhere that doesn't override the `foo()` method and therefore calls the `bar()` method. – Thomas Kläger Jan 30 '22 at 15:49
  • You have a point that an implementation of the `Foo` interface can override the `foo()` method, in which case the `bar(int value)` method may never be called. But, that's also true if the `bar(int value)` method is `static`, however in that case spotbugs doesn't report any bug. Thanks for your help ;) – Oboe Jan 30 '22 at 16:05