在 Eclipse 中抑制 FindBugs 警告

发布于 2024-09-28 06:01:42 字数 665 浏览 4 评论 0原文

我使用字符串作为锁,因此想确保该对象是一个新实例。 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.

I am using a string as a lock and so want to ensure the object is a new instance. FindBugs complains because it's generally more efficient to define the string directly (with double quotes). My code looks like:

/** 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");

Am I doing something wrong here? The Eclipse FindBugs plugin is still reporting this as a problem:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红颜悴 2024-10-05 06:01:42

为什么不直接将锁对象声明为一个新对象呢?您不需要将其设置为字符串,因为您不需要执行任何需要锁的字符串性的操作,并且可能您不会将其用于除锁定之外的任何其他用途。

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

Why not just declare the lock object as a new Object? You don't need to make it a String, since you don't do anything that requires the String-ness of the lock, and presumable you don't use it for anything other than locking.

Without seeing the rest of your code I can hazard a guess that you're locking on access to a list of some kind. You could use the list itself as the lock object. If it's private then there is no chance that someone else will cause a deadlock.

苍暮颜 2024-10-05 06:01:42

通常的做法是这样做:

private final Object inputListLock = new Object();

这样可以节省空间(相对于new String("someLock"))并消除讨厌的 PMD 警告。但是,如果您确实希望锁是一个字符串,那么还有其他方法可以创建 PMD 不太可能反对的字符串副本;例如

private final Object inputListLock = "some".concat("Lock");

(请注意,"someLock".concat("")实际上并没有创建一个新的字符串!)

The normal idiom is to do this:

private final Object inputListLock = new Object();

which saves space (relative to new String("someLock")) and gets rid of the pesky PMD warning. But if you really want the lock to be a String, there are other ways to create a copy of a String that PMD is unlikely to object to; e.g.

private final Object inputListLock = "some".concat("Lock");

(Note that "someLock".concat("") doesn't actually create a new String!)

暖伴 2024-10-05 06:01:42

好吧,虽然其他两个答案都很有趣且有用(两者都+1),但我最终没有更改代码,我将接受我自己的答案。为了满足 FindBugs 的要求,我将注释从成员变量移到了周围的类。

我已经查找了一段时间,但没有找到任何表明 SuppressWarnings 可能仅适用于类和方法的信息。我也没有找到任何将其应用于成员变量的示例。因此,尽管这个解决方案有效,但我不知道它是“正确”的解决方案(例如,我的 FindBugs/Eclipse 设置可能仍然存在问题)。

Ok, so although both the other answers were interesting and useful (+1 for both), I didn't end up changing the code and I'm going to accept my own answer. To satisfy FindBugs I moved the annotation from the member variable to the surrounding class.

I've looked for some time but I haven't found any information suggesting that the SuppressWarnings may only be applied to classes and methods. Neither have I found any examples of it being applied to member variables. So though this solution works I don't know that it's the 'right' solution (maybe there's still something wrong with my FindBugs/Eclipse setup for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文