Skip to content

FP SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR for record #2981

@xzel23

Description

@xzel23

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions