Java – can there be any hint that I object to type Equals (incompatible type)?

Is there any tool to remind me not to use the following code:

if ( someClass.equals( someString ))

For example:

if ( myObject.getClass().equals( myClassName ))

Such a thing is legal Java (equal to taking an object), but it will never be evaluated as true (a class will never be equal to a string), so it is almost certainly an error

I've checked eclipse, findbugs and PMD, but it doesn't seem to support this function?

Solution

Yes, IntelliJ idea has such a check. I believe it is enabled by default It is marked as follows:

Class<?> clazz = String.class;
if (clazz.equals("foo")) {
   //...
}

Warning:

You can enable / disable checking by setting – > project settings – > checking, and then check / uncheck the equal sign () between objects of non convertible type under suspicious errors

Findbugs should also use "EC: call to equals() comparing different types" error checking It can integrate with eclipse because it's what you know

Not a silver bullet; They can't understand your mind What you want most is that it will prefer false masks to false negatives

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>