// ==> final int prime = 31;
hashCodeMethod.body().assign(JExpr.ref("final int prime"), JExpr.ref("31"));
// ==> int result = 1; || int result = super.hashCode();
JExpression initializeResult = null;
if (isSuperClass) {
initializeResult = JExpr._super().invoke("hashCode");
} else {
initializeResult = JExpr.ref("1");
}
hashCodeMethod.body().assign(JExpr.ref("int result"), initializeResult);
// if class contains double
// long temp;
JVar tempVariableForDoubleChecks = null;
JType doubleClass = null;
if (containsDouble == true) {
tempVariableForDoubleChecks = hashCodeMethod.body().decl(implClass.owner().LONG, "temp");
doubleClass = implClass.owner()._ref(Double.class);
}
JExpression tenaryCond;
for (final JFieldVar jFieldVar : fields) {
if (jFieldVar.type().isPrimitive()) {
if (jFieldVar.type().fullName().equals("double")) {
// temp = Double.doubleToLongBits(altitude);
// result = prime * result + (int) (temp ^ (temp >>> 32));
// LOG.info("implClass: " + implClass.name());
// LOG.info("jFieldVar: " + jFieldVar.name());
// LOG.info(" is " + jFieldVar.type().fullName());
// LOG.info(" + " + tempVariableForDoubleChecks.name());
// LOG.info(" + " + doubleClass.boxify().fullName());
hashCodeMethod.body().assign(tempVariableForDoubleChecks, doubleClass.boxify().staticInvoke("doubleToLongBits").arg(jFieldVar));
tenaryCond = JExpr.cast(implClass.owner().INT, tempVariableForDoubleChecks.xor(tempVariableForDoubleChecks.shrz(JExpr
.direct("32"))));
} else {
// if field is primitive:
// ==> result = prime * result + field;
tenaryCond = JExpr.ref(jFieldVar.name());
}
} else {
// else:
// ==> result = prime * result + ((field == null) ? 0 :
tenaryCond = JOp.cond(JOp.eq(JExpr.ref(jFieldVar.name()), JExpr._null()), JExpr.ref("0"), JExpr.invoke(JExpr.ref(jFieldVar.name()),
"hashCode"));
}
final JExpression hashCodeCollection = JOp.mul(vPrime, vResult);
final JExpression addition = JOp.plus(hashCodeCollection, tenaryCond);
// add each value that influence to the method
hashCodeMethod.body().assign(vResult, addition);
}