SonarQube违规:内部类私有构造函数未使用私有方法
问题描述:
我在我的项目中使用了以下代码。当我使用声纳对其进行评估时,它显示Unused private method
侵犯了ObjectTypes
内部类中定义的私人构造函数。如果我删除ObjectTypes
构造函数,它会显示Hide Utility Class Constructor
违规。请帮助我找到解决此问题的最佳方法。SonarQube违规:内部类私有构造函数未使用私有方法
public final class Constants
{
private Constants()
{
}
public static final String KEY_SEPARATOR = " ~ ";
public static final String COMMON_SEPARATOR = " : ";
public final class ObjectTypes
{
private ObjectTypes()
{
}
public static final String ACTION_CODES = "Action Codes";
public static final String ALL_ACTION_CODES = "All Action Codes";
//more lines
}
}
答
这就是问题所在:
private Constants()
{
}
这:
private ObjectTypes()
{
}
您不必申报EMPTY构造。
我可以通过将私有构造函数更改为Inner类中的受保护构造函数来解决此问题。但是,我认为在最终的类中使用受保护的构造函数并不是一个好主意。 – Madhujith