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

FP SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR for record #2981

Open
xzel23 opened this issue May 9, 2024 · 5 comments
Open

FP SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR for record #2981

xzel23 opened this issue May 9, 2024 · 5 comments

Comments

@xzel23
Copy link

xzel23 commented May 9, 2024

After #2934 was fixed, most problems with singletons seem to be solved. There still is one left. I don't know if there's an easy way to fix this - if not, I'll add an exclusion rule in my code.

I have the following record class where SpotBugs still reports a false positive SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR:

    record RowInfo(int firstRow, int lastRow) {
        private static final RowInfo NONE = new RowInfo(0, -1);

        public static RowInfo none() {
            return NONE;
        }
    }

The problem I think is that it's not easy to tell if something should be a singleton or not. And you could implement a singleton as a record, but it is rather usual IMHO. I this example, the static final member is not intended as a singleton instance, but is used as a guard value.

@JuditKnoll
Copy link
Collaborator

Thanks for reporting this issue. I will take a closer look at it next week.

@gtoison
Copy link
Contributor

gtoison commented May 12, 2024

A record must have a non private constructor, so it can't be a singleton here.
For non record classes it will be difficult to decide if the intent was to write a singleton

@xzel23
Copy link
Author

xzel23 commented May 13, 2024

@gtoison a private record can have only private constructors. I don't know how relevant that is for "real" code. Could be used as a singleton holder for the enclosing class.

@gtoison
Copy link
Contributor

gtoison commented May 13, 2024

Ah you're right, I got confused with the canonical record constructor

@JuditKnoll
Copy link
Collaborator

This month was quite busy for me, and I could not look into this issue. If anyone is interested, feel free to take it. If no one takes it up, I'll look into it at the earliest 2 weeks from now.

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

3 participants