简体   繁体   English

在Eclipse中抑制FindBugs警告

[英]Suppress FindBugs warnings in Eclipse

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). FindBugs抱怨,因为直接定义字符串通常更有效(使用双引号)。 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: 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.

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. 你不需要把它变成一个String,因为你没有做任何需要锁的String-ness的东西,并且你不会将它用于锁定以外的任何东西。

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. 如果它是私有的,则其他人不可能导致死锁。

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. 这节省了空间(相对于new String("someLock") )并摆脱了令人讨厌的PMD警告。 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; 但是如果你真的希望锁是一个String,还有其他方法来创建一个PMD不太可能反对的String副本; eg 例如

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

(Note that "someLock".concat("") doesn't actually create a new String!) (注意"someLock".concat("")实际上并没有创建新的String!)

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. 好的,所以尽管其他答案都很有趣且有用(两者都是+1),但我最终没有改变代码,我将接受我自己的答案。 To satisfy FindBugs I moved the annotation from the member variable to the surrounding class. 为了满足FindBugs,我将注释从成员变量移动到了周围的类。

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. 我找了一些时间,但我没有发现任何信息表明SuppressWarnings可能只适用于类和方法。 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). 因此,虽然这个解决方案有效但我不知道它是'正确'的解决方案(例如,我的FindBugs / Eclipse设置可能还有问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM