// add paramter for equals()-method
final JVar vObj = equalsMethod.param(Object.class, "obj");
// is it me? this==obj -> true
final JConditional condMe = equalsMethod.body()._if(JExpr._this().eq(vObj));
condMe._then()._return(JExpr.TRUE);
// it's null? obj==null -> false
final JConditional condNull = equalsMethod.body()._if(vObj.eq(JExpr._null()));
condNull._then()._return(JExpr.FALSE);
// paternity test (only add if equals in superclass isn't overridden)?
// !super.equals(obj) -> false --> (super.equals(obj) == false)
if (isSuperClass) {
final JConditional condSuper = equalsMethod.body()._if(JExpr._super().invoke("equals").arg(vObj).eq(JExpr.FALSE));
condSuper._then()._return(JExpr.FALSE);
}
// suit the class? !(obj instanceof TypeB) -> false --> if ((obj instanceof TypeB) == false)
final JConditional condInstance = equalsMethod.body()._if(vObj._instanceof(implClass).eq(JExpr.FALSE));
condInstance._then()._return(JExpr.FALSE);
// cast: TYPE other = (TYPE) obj;
final JVar vOther = equalsMethod.body().decl(implClass, "other", JExpr.cast(implClass, vObj));
/*
* check for each declared field distinguish between primitive types:
*
* <pre> if (field != other.field) { return false; } </pre>
*
* and reference types: <pre> if (field == null) { if (other.field != null) { return false; } } else if (!field.equals(other.field)) {
* // --> if (field.equals(other.field) == false) return false; } </code>
*/
JConditional condFieldCheck;
boolean containsDouble = false;
for (final JFieldVar jFieldVar : fields) {
if (jFieldVar.type().fullName().equals("java.lang.Double") || jFieldVar.type().fullName().equals("double")) {
jFieldVar.type(implClass.owner().DOUBLE);
containsDouble = true;
// break;
}
}
for (final JFieldVar jFieldVar : fields) {
if (jFieldVar.type().isPrimitive()) {
// LOG.info("JConditional: " + jFieldVar.name() +
// " is PRIMITIVE");
condFieldCheck = equalsMethod.body()._if(JExpr.ref(jFieldVar.name()).ne(vOther.ref(jFieldVar.name())));
condFieldCheck._then()._return(JExpr.FALSE);
} else {
// LOG.info("JConditional: " + jFieldVar.name() +
// " is REFERENCE");
condFieldCheck = equalsMethod.body()._if(JExpr.ref(jFieldVar.name()).eq(JExpr._null()));
condFieldCheck._then()._if(vOther.ref(jFieldVar.name()).ne(JExpr._null()))._then()._return(JExpr.FALSE);
condFieldCheck._elseif(JExpr.ref(jFieldVar.name()).invoke("equals").arg(vOther.ref(jFieldVar.name())).eq(JExpr.FALSE))._then()
._return(JExpr.FALSE);
}
}
// ir all works out, the objects are equal, return true