spotbugs / spotbugs

SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.

Home Page:https://spotbugs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False positive SING_SINGLETON_IMPLEMENTS_SERIALIZABLE when class is not a singleton

colin-janke opened this issue · comments

Code like this (somewhat anonymised, but I think this expresses the idea)

public class MyFancyCollection implements Serializable {
    private static final MyFancyCollection EMPTY = new MyFancyCollection(new IntArraySet(0)) {
        private Object readResolve() {
            return EMPTY;
        }
    };

    private MyFancyCollection(IntSet set) {
        this.set = set;
    }

    public static MyFancyCollection empty() {
        return EMPTY;
    }

    public static MyFancyCollection copyOf(Set<Integer> sourceData) {
        IntSet set = new IntOpenHashSet(sourceData.size());
        sourceData.forEach(i -> set.add(i));
        return new ImmutableCoordinateSet(set);
    }
...

is producing SING_SINGLETON_IMPLEMENTS_SERIALIZABLE, when the class is not a singleton. It has a constant EMPTY field, but that is just utility and there is no real harm to creating multiple empty instances (you will note that the copyOf method does not return EMPTY if the copied set is EMPTY).

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

I should add this is with spotbugs v4.8.4

This seems to be the duplicate of #2934 (see the second example). It will be fixed by #2951.