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_HAS_NONPRIVATE_CONSTRUCTOR with reused instances #2934
Comments
In this particular case, the fact that the class is NOT a singleton can easily be inferred:
|
What heuristics does SpotBugs use to detect singleton classes? After upgrading to the new release, I also get several warnings of this type for classes that are clearly not singletons. For example one class has several factory methods that each return new instances. And there are several package private constructors declared. Also AFAIK synchronization is not always needed, i. e. if the instance is held in a static field that is unconditionally initialized either at declaration or in a static initializer block. This rule needs some rework. |
@rovarga Thank you for the example and your explanation. I proposed a PR fixing this FP. @xzel23 Yes, you are right about the synchronization. It was a bug in the detector in SpotBugs, which was reported in #2932 and the PR fixing it was already merged to master, so it will be in the next release. |
@JuditKnoll Here is an example. Even if you say the error reported by spotbugs is already fixed, the important thing is: this simply is not a singleton class, so none of the singleton rules should apply. This class provides public factory methods to create new instances, this should be detected and the class not be marked as singleton:
|
@JuditKnoll Thank you for your fixes. This works in most cases and I was able to remove the exclusions in most projects. The problem persists with classes that have implement an SPI service (because a public no args constructor must be present). But I will change this so that these classes can be real singletons and SPI loads a provider that simply provides the singleton instance. That's the correct way to do this anyway. |
Thank you @xzel23 for checking it and getting back about this 😄 |
Singleton detection seems to be over-eager. A bit of nest-private hierarchy, like demonstrated in https://github.com/opendaylight/yangtools/blob/2e257731e354e6cc2fd45a6f6eec79bd01490c07/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/CanonicalValueViolation.java triggers three singleton errors:
The problem is that CanonicalValueViolation is assumed to be a singleton, whereas it only uses two constants for common instantiateions.
The text was updated successfully, but these errors were encountered: