简体   繁体   English

在Eclipse中使用相等运算符(==)进行字符串比较时突出显示

[英]Highlight when equality operator (==) is used for string comparisons in Eclipse

Is there any way I can get Eclipse to highlight the use of the == operator to test String equality? 有没有什么方法可以让Eclipse强调使用==运算符来测试字符串相等性? I keep mistakenly using it instead of calling .equals() . 我一直错误地使用它而不是调用.equals()

I'd really like to make that into a warning and require an @SuppressWarnings annotation to remove it, in the yet-to-happen case that I actually want to compare strings for object equality. 我真的想把它变成一个警告并要求@SuppressWarnings注释删除它,在尚未发生的情况下,我实际上想要比较字符串以获得对象相等性。

Are there any tools can I use to help break this bad habit at edit-time? 我是否可以使用任何工具来帮助在编辑时打破这种坏习惯?

Use a static analysis tool such as FindBugs , PMD , or CheckStyle . 使用静态分析工具,如FindBugsPMDCheckStyle

There are Eclipse plugins for each, along with Ant tasks, Maven plugins, etc. 每个都有Eclipse插件,以及Ant任务,Maven插件等。

Each of these has rules relating to String equality ( Findbugs rule , PMD rule , Checkstyle rule ). 其中每个都有与字符串相等相关的规则Findbugs规则PMD规则Checkstyle规则 )。

The obvious answer to the question has already been given , but here is a warning that is not a direct answer: obj.equals can also fail if obj is null. 已经给出了问题的明显答案,但这里的警告不是直接答案:如果obj为null,则obj.equals也会失败。 So you'll often have to use code like this: 所以你经常需要使用这样的代码:

if(mystr1 != null && mystr1.equals(mystr2))

because this 因为这

if(mystr1.equals(mystr2))

would fail with a NullPointerException if mystr1 is null. 如果mystr1为null,则会因NullPointerException而失败。

Which is why, when the comparison string is a known constant, the following syntax is often used: 这就是为什么,当比较字符串是已知常量时,通常使用以下语法:

if("ABCDEF".equals(mystr1))

rather than 而不是

if(mystr1.equals("ABCDEF"))

For this reason, many libraries (like apache commons / lang ) provide utility functions that combine these checks: 出于这个原因,许多库(如apache commons / lang )提供了组合这些检查的实用程序功能:

// this is the definition of org.apache.commons.lang.StringUtils.equals(String, String)
public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

// this is the definition of  org.apache.commons.lang.ObjectUtils.equals(Object, Object)
public static boolean equals(Object object1, Object object2) {
    if (object1 == object2) {
        return true;
    }
    if ((object1 == null) || (object2 == null)) {
        return false;
    }
    return object1.equals(object2);
}

Using these methods is usually safer than plain equals, unless you know for sure that one of the two objects is not null 除非您确定两个对象中的一个不为null,否则使用这些方法通常比plain equals更安全

I disagree with previous answers - it is a bug in eclipse and you can vote for it here : https://bugs.eclipse.org/bugs/show_bug.cgi?id=39095 . 我不同意以前的答案 - 这是eclipse中的一个错误,你可以在这里投票: https//bugs.eclipse.org/bugs/show_bug.cgi?id = 39095

Eclipse can very well warn you when you compare Strings with == as this is seldom what you wanted (or what the original author wanted). 当您将字符串与==进行比较时,Eclipse可以很好地警告您,因为这很少是您想要的(或原始作者想要的)。

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

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