Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive SING_SINGLETON_IMPLEMENTS_SERIALIZABLE when class is not a singleton #2964

Closed
colin-janke opened this issue Apr 23, 2024 · 3 comments

Comments

@colin-janke
Copy link

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).

Copy link

welcome bot commented Apr 23, 2024

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.

@colin-janke
Copy link
Author

I should add this is with spotbugs v4.8.4

@JuditKnoll
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants