在 Eclipse 中抑制 FindBugs 警告
我使用字符串作为锁,因此想确保该对象是一个新实例。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接将锁对象声明为一个新对象呢?您不需要将其设置为字符串,因为您不需要执行任何需要锁的字符串性的操作,并且可能您不会将其用于除锁定之外的任何其他用途。
如果没有看到您的其余代码,我可以冒险猜测您正在锁定对某种列表的访问。您可以使用列表本身作为锁定对象。如果它是私有的,那么其他人就不可能造成僵局。
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.
通常的做法是这样做:
这样可以节省空间(相对于
new String("someLock")
)并消除讨厌的 PMD 警告。但是,如果您确实希望锁是一个字符串,那么还有其他方法可以创建 PMD 不太可能反对的字符串副本;例如(请注意,
"someLock".concat("")
实际上并没有创建一个新的字符串!)The normal idiom is to do this:
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.(Note that
"someLock".concat("")
doesn't actually create a new String!)好吧,虽然其他两个答案都很有趣且有用(两者都+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).