简体   繁体   English

Java - SonarQube,关于单例中“实用程序类不应具有公共构造函数”(squid:S1118)的问题

[英]Java - SonarQube, issue on 'Utility classes should not have public constructors' (squid:S1118) in singleton

I am performing static code analysis on old code using a SonarLint analysis.我正在使用 SonarLint 分析对旧代码执行静态代码分析。 I cannot paste the code here but it is similar to:我无法在此处粘贴代码,但它类似于:

@SuppressWarnings("static-access")
public class SuperClass {

    private SuperClass() {
    }

    public static SuperClass getInstance() {
        return InstanceHolder.instance;
    }

    private static class InstanceHolder {
        public final static SuperClass instance = new SuperClass();
    }

    public void doSomething() {
        //do something
    }

}

SonarQube (sonar-java: 4.2.1.6971), reports an issue on S1118 . SonarQube (sonar-java: 4.2.1.6971),报告S1118上的问题。

Adding a private constructor to InstanceHolder has no solving effect here, since SuperClass is the only class that can create an instance of it due to its private modifier.InstanceHolder添加私有构造函数在这里没有解决效果,因为SuperClass是唯一可以创建它的实例的类,因为它有私有修饰符。

SuperClass can still create an instance, even with ÌnstanceHolder having a private constructor. SuperClass仍然可以创建一个实例,即使ÌnstanceHolder具有私有构造函数。

BTW: adding the constructor removes the sonar-issue, so I think the analyzer marked this as a rule violation because of the internal 'UtilityClass' without further investigation.顺便说一句:添加构造函数消除了声纳问题,所以我认为分析器将此标记为违反规则,因为内部“UtilityClass”没有进一步调查。

Is this a bug?这是一个错误吗? Instead of a design flaw, this is an example of a thread-safe singleton.这不是设计缺陷,而是线程安全单例的示例。

Make your class final so that Instance creation can be avoided.将您的类设为 final,以避免创建实例。

    @SuppressWarnings("static-access")
    public final class SuperClass {

        private SuperClass() {
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 它是什么意思以及如何修复SonarQube Java问题“应该删除包之间的循环”(squid:CycleBetweenPackages) - What does it mean and how to fix SonarQube Java issue “Cycles between packages should be removed” (squid:CycleBetweenPackages) SonarQube规则squid:S1200限制java。*类为20吗? - SonarQube rule squid:S1200 counts java.* classes in its limit of 20? SonarQube:不应使用幻数(鱿鱼:S109) - SonarQube: Magic numbers should not be used (squid:S109) 使用 Spring 时,如何抑制 checkstyle 消息“Utility classes should not have a public of default constructor” - How can I supress the checkstyle message "Utility classes should not have a public of default constructor" when using Spring 为什么Java的Pattern和Matcher类中没有公共构造函数? - Why are there no public constructors in Pattern and Matcher classes of java? 为什么某些类(例如java regex Pattern类)没有任何公共构造函数? - Why do some classes like the java regex Pattern class do not have any public constructors? Java EE应用程序中的实用程序类应该是EJB的类还是具有静态方法的类 - Should utility classes in Java EE application be EJB's or classes with static methods 公共静态变量的 SonarQube 问题 | JAVA - SonarQube issue with public static variables | JAVA sonarqube java规则不扩展类应该是最终的 - sonarqube java rule not extended classes should be final Sonarqube鱿鱼:如果JSONPath验证,则为S2699 - Sonarqube squid:S2699 if jsonPath validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM