java - 在 Eclipse 中抑制 FindBugs 警告

标签 java eclipse findbugs suppress-warnings

我使用一个字符串作为锁,所以想确保该对象是一个新实例。 FindBugs 提示是因为直接定义字符串(使用双引号)通常更有效。我的代码如下所示:

/** A lock for the list of inputs. */
@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_STRING_CTOR")
//We want a new String object here as this is a lock.
private final Object inputListLock = new String("inputListLock");

我是不是做错了什么? Eclipse FindBugs 插件仍然将此报告为问题:

Pattern id: DM_STRING_CTOR, type: Dm, category: PERFORMANCE

Using the java.lang.String(String) constructor wastes memory because the object so constructed will be functionally indistinguishable from the String passed as a parameter.  Just use the argument String directly.

最佳答案

为什么不直接将锁对象声明为一个新对象呢?您不需要将它变成一个字符串,因为您没有做任何需要锁的字符串特性的事情,并且可以推测您除了锁定之外不会将其用于任何其他用途。

在没有看到您的其余代码的情况下,我可以冒险猜测您正在锁定对某种列表的访问。您可以使用列表本身作为锁定对象。如果它是私有(private)的,那么其他人就不可能造成死锁。

关于java - 在 Eclipse 中抑制 FindBugs 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976328/

相关文章:

java - 如何根据屏幕尺寸启动 Activity ?

java - 为什么 CodePro junit 测试方法会抛出异常(在 Eclipse 中)?

java - 在 Eclipse 中创建 META-INF/services 文件夹

Java - 从 Eclipse 到 Excel 从 SQL 查询

java - 将格式化规则应用于整个 Eclipse 工作空间

java - 未抛出异常时捕获异常

android - 在android项目上运行 Sonar : The class 'foo.bar.R$string' could not be matched to its original source file

java - 有没有一种简单的方法可以在 Oracle 中克隆表的结构?

java - 我应该在客户端计算机上使用 JVM 还是 JRE 来运行嵌入式 Java DB?

java - 来自 Throwable 的 findbugs 未经检查/未经确认的转换