Eclipse equals generation: null check missing?
I let Eclipse generate the equals method for my class and it starts with:
@Override
public boolean equals(Object o开发者_如何学Pythonbj) {
if (this == obj)
return true;
if (getClass() != obj.getClass())
return false;
[...]
It seems to me, a check like if (obj == null) return false;
is missing. Otherwise, if a null reference is passed to equals there will be a null pointer exception in obj.getClass()
. Am I wrong or is Eclipse wrong?
Perhaps you are having an old eclipse version. My eclipse generates this:
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
You are right, if Eclipse does it that way. But it doesn't. On my machine, Eclipse Indigo / Ubuntu, given this Class:
public class Foo {
private String bar;
}
Eclipse would generate the following equals() method:
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Foo other = (Foo) obj;
if (bar == null) {
if (other.bar != null) return false;
} else if (!bar.equals(other.bar)) return false;
return true;
}
For comparison, here's the equals() method I would write for the same class (using Guava):
@Override
public boolean equals(final Object obj) {
return obj instanceof Foo ? Objects.equal(bar, ((Foo) obj).bar) : false;
// ^--- implicit null check here
}
I use this Eclipse code template to achieve this:
${:import(com.google.common.base.Objects)}
@Override
public boolean equals(final Object obj){
return obj instanceof ${enclosing_type} ? Objects.equal(${field1:field}, ((${enclosing_type}) obj).${field1}) : false;
}
@Override
public int hashCode(){
return Objects.hashCode(${field1});
}
@Override
public String toString(){
return MoreObjects.toStringHelper(this).add("${field1}", ${field1}).toString();
}
Unfortunately, I have to keep one of these around for each cardinality of fields, so I have templates named eq1 (the above), eq2, eq3, eq4 etc. It's a small nuissance, but it's still a lot better than the monster methods generated by Eclipse. Guava docs
Yes, passing null
as obj
would give a NullPointerException
with that code.
You indeed need a null check. My version of Eclipse generates equals methods with a null check.
if (obj) is null, the obj.getClass() will throw an NPE
It seems to me, a check like if (obj == false) return false; is missing.
Why do you want to compare object with a Boolean value? ;-)
I suppose you though about obj == null check, and that is exactly what Eclipse generates in my case:
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestGetClass other = (TestGetClass) obj;
if (id != other.id)
return false;
return true;
}
I am using 3.7 version. Anyway, you are right... actually Eclipse... too ;-)
精彩评论