简体   繁体   English

等于的奇怪Findbugs错误

[英]Strange Findbugs error with equals

I have equals this method, but Findbugs is reporting error, any idea? 我等于这个方法,但是Findbugs报告错误,知道吗?

@Override
public boolean equals(final Object obj) {
    return obj instanceof String && this.value != null  
                              && this.value.equals(obj);      // this.value is a String
}

The error is : 错误是:

Myclass.equals(Object) checks for operand being a String Myclass.equals(Object)检查操作数是否为字符串

Your implementation of equals for your MyClass will break at least the symmetric- and reflexive properties of the equals-contract : 您为MyClass实现的equals至少会破坏equals-contract的对称和自反属性:

symmetric: 对称的:

for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. 对于任何非空参考值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才应返回true。

In your case: 在您的情况下:

MyClass A for example with value="a": A.equals("a") will be true, but "a".equals(A) is false. 例如, MyClass A的值为=“ a”: A.equals("a")为true,但是"a".equals(A)为false。 This violates the symmetric-property. 这违反了对称属性。

reflexive: 反身:

for any non-null reference value x, x.equals(x) should return true. 对于任何非空参考值x,x.equals(x)应该返回true。

But your implementation will return false for 但是您的实现将为返回false

A.equals(A) 

but must return true. 但必须返回true。

Etc. 等等。

Your equals implementation sure is strange. 您的平等实施肯定很奇怪。

For one it looks very much like its violating the requirements of 对于它,它看起来非常违反其要求

a.equals(a) == true

=== Update in response to the comment === ===更新以回应评论===

This is part of the contract of equals: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals%28java.lang.Object%29 这是平等合同的一部分: http : //download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals%28java.lang.Object%29

This kind of behavior is important when you put your Object into a Set or Map. 当您将对象放入集合或地图时,这种行为很重要。 Without the mentioned property you would get the weired behavior that you can add an instance to a Set and afterwards calling contains on the set with the exact same object as an argument would result in false. 没有提到的属性,您将得到奇怪的行为,可以将实例添加到Set中,然后调用该set上包含与参数完全相同的对象的对象将导致false。

=== Another update in response to your changed question === ===针对您更改的问题的另一种更新===

Since you check that the operand is a String, but your class isn't a subclass of String, an instance of your class will never be equal to itself according to your definition of equals. 由于您检查操作数是否为String,但您的类不是String的子类,因此根据您对equals的定义,该类的实例永远不会等于其自身。 Also as stated by another answer symmetry will be broken. 同样如另一个答案所述,对称性将被破坏。

This might be helpful as well: http://findbugs.sourceforge.net/bugDescriptions.html#EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS 这也可能会有所帮助: http : //findbugs.sourceforge.net/bugDescriptions.html#EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS

 @Override
 public boolean equals(final Object obj) {
    return (obj instanceof YourClass) && (this.value.equals(((YourClass)obj).value));      // this.value is a String
}

As I understand Findbugs points to potential bugs. 据我了解,Findbugs指出了潜在的错误。

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

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