奇怪的 Findbugs 错误与 equals

发布于 2024-10-09 01:20:57 字数 353 浏览 0 评论 0原文

我有 equals 这个方法,但 Findbugs 报告错误,知道吗?

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

错误是:

Myclass.equals(Object) 检查操作数是否为字符串

I have equals this method, but Findbugs is reporting error, any idea?

@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

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

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

发布评论

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

评论(3

逆蝶 2024-10-16 01:20:57

您对 MyClass 实现 equals 至少会破坏 equals-contract

对称:

对于任何非空
参考值x和y,x.equals(y)
应该返回 true 当且仅当
y.equals(x) 返回 true。

在您的情况下:

MyClass A 例如 value="a":
A.equals("a") 将为 true,但 "a".equals(A) 为 false。这违反了对称性。

反身性:

对于任何非空引用值 x,x.equals(x) 应返回 true。

但是您的实现将返回 false,

A.equals(A) 

但必须返回 true。

ETC。

Your implementation of equals for your MyClass will break at least the symmetric- and reflexive properties of the 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.

In your case:

MyClass A for example with value="a":
A.equals("a") will be true, but "a".equals(A) is false. This violates the symmetric-property.

reflexive:

for any non-null reference value x, x.equals(x) should return true.

But your implementation will return false for

A.equals(A) 

but must return true.

Etc.

╰沐子 2024-10-16 01:20:57

你的 equals 实现确实很奇怪。

其一,它看起来很像违反了要求

a.equals(a) == true

=== 更新以响应评论 ===

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

当您将对象放入 Set 或 Map 时,这种行为非常重要。如果没有提到的属性,您会得到奇怪的行为,您可以将实例添加到 Set 中,然后使用完全相同的对象作为参数调用 Set 上的 contains 将导致 false。

=== 针对您更改的问题的另一个更新 ===

由于您检查操作数是 String,但您的类不是 String 的子类,因此根据您的定义,您的类的实例永远不会等于自身的平等。正如另一个答案所述,对称性将被打破。

这也可能有帮助:

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

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.

=== 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. 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

暗藏城府 2024-10-16 01:20:57
 @Override
 public boolean equals(final Object obj) {
    return (obj instanceof YourClass) && (this.value.equals(((YourClass)obj).value));      // this.value is a String
}

据我了解,Findbugs 指出了潜在的错误。

 @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.

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