java - FindBugs 中的误报 MS_PKGPROTECT 错误

标签 java findbugs

我很确定我从 FindBugs 得到了误报。我的代码如下:

public class MyClass {
    protected static String fileNamePrefix;
}        

这是我遇到的错误:

Field should be package protected

A mutable static field could be changed by malicious code or by accident. The field could be made package protected to avoid this vulnerability.

Bug kind and pattern: MS - MS_PKGPROTECT

这真的是误报还是我遗漏了什么?

最佳答案

protected 修饰符允许第三方子类意外地提升对该字段的访问权限。

class MyAccidentSubClass extends MyClass {
    static {
        fileNamePrefix = "../" + fileNamePrefix;
    }
}

FindBugs 指出,“ protected ”并不能保护该领域免受意外的公共(public)访问。如果该字段受到包保护,那么我们知道其意图是在值得信赖的 friend 之间共享一些 secret 。

一般来说,隐藏字段并创建访问器方法,因为它在封装、线程安全和延迟加载方面更加灵活。

关于java - FindBugs 中的误报 MS_PKGPROTECT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649958/

相关文章:

java - 查找 ArrayList 的 ArrayList 的最大值,key = length

java - 我在 eclipse 中收到错误,提示 Jar 中缺少一个类,并且该类不是 JAR ,在这种情况下可以做什么?

java - 强制 Maven 因非空违规而导致构建失败

sonarqube - 如何在 IDE 中使用 SonarQube 服务器上定义的 PMD 和 FinBugs 规则?

java - TestNG parallel 属性对方法执行没有影响

java - "If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0."

java - 是否可以在 String 中声明 Java 中的默认参数?

java - 当基类中存在 someObject 时,使用 PMD 检查 someObject.methodCall

xml - FindBugs/SpotBugs : Filters: DTO or XML Schema

java - 简单日期格式