new JavaMethod (
Modifier.PUBLIC | Modifier.FINAL,
JavaTypeName.INT,
"hashCode");
JavaField hashCodeField =
new JavaField.Instance (
null,
HASH_CODE_FIELD_NAME,
JavaTypeName.INT);
JavaExpression condition =
new OperatorExpression.Binary (
JavaOperator.EQUALS_INT,
hashCodeField,
LiteralWrapper.make(new Integer(0)));
JavaStatement.Block thenBlock = new JavaStatement.Block();
IfThenElseStatement ifThen =
new IfThenElseStatement (condition, thenBlock);
hashCode.addStatement (ifThen);
// build up the hashcode value.
JavaExpression.LocalVariable result =
new LocalVariable ("result", JavaTypeName.INT);
LocalVariableDeclaration localVarDecl =
new LocalVariableDeclaration(result, LiteralWrapper.make(new Integer(17)));
thenBlock.addStatement(localVarDecl);
LiteralWrapper thirtySeven = LiteralWrapper.make (new Integer(37));
JavaExpression thirtySevenTimesResult =
new OperatorExpression.Binary(JavaOperator.MULTIPLY_INT, thirtySeven, result);
LiteralWrapper zero = LiteralWrapper.make (new Integer (0));
LiteralWrapper one = LiteralWrapper.make (new Integer (1));
// Start by including dc name in the hashcode.
// get objects hashcode
JavaExpression nameExpression =
new MethodInvocation.Instance (
new MethodInvocation.Instance(
null,
GET_DC_NAME_METHOD_NAME,
JavaTypeName.STRING,
MethodInvocation.InvocationType.VIRTUAL),
"hashCode",
JavaTypeName.INT,
MethodInvocation.InvocationType.VIRTUAL);
JavaExpression newResult =
new OperatorExpression.Binary (JavaOperator.ADD_INT, thirtySevenTimesResult, nameExpression);
JavaExpression assignResult = new Assignment (result, newResult);
thenBlock.addStatement(new ExpressionStatement (assignResult));
// Now get the contribution from each dc field.
for (FieldName fn : fieldNames) {
String javaFieldName = typeConstructorInfo.fieldJavaNames.get (fn);
JavaTypeName fieldType = fieldNameToType.get (fn);
JavaField field = new JavaField.Instance(null, javaFieldName, fieldType);
JavaExpression fieldExpression;
if (fieldType instanceof JavaTypeName.Primitive) {
if (fieldType instanceof JavaTypeName.Primitive.Boolean) {
fieldExpression =