}
private MethodDeclaration createEquals(EmitContext context, ModelDeclaration model) {
assert context != null;
assert model != null;
ModelFactory f = context.getModelFactory();
List<Statement> statements = Lists.create();
SimpleName obj = context.createVariableName("obj"); //$NON-NLS-1$
statements.add(f.newIfStatement(
new ExpressionBuilder(f, f.newThis())
.apply(InfixOperator.EQUALS, obj)
.toExpression(),
f.newBlock(f.newReturnStatement(Models.toLiteral(f, true)))));
statements.add(f.newIfStatement(
new ExpressionBuilder(f, obj)
.apply(InfixOperator.EQUALS, Models.toNullLiteral(f))
.toExpression(),
f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
statements.add(f.newIfStatement(
new ExpressionBuilder(f, f.newThis())
.method("getClass") //$NON-NLS-1$
.apply(InfixOperator.NOT_EQUALS, new ExpressionBuilder(f, obj)
.method("getClass") //$NON-NLS-1$
.toExpression())
.toExpression(),
f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
SimpleName other = context.createVariableName("other"); //$NON-NLS-1$
Type self = context.resolve(context.getQualifiedTypeName());
statements.add(new ExpressionBuilder(f, obj)
.castTo(self)
.toLocalVariableDeclaration(self, other));
for (PropertyDeclaration property : model.getDeclaredProperties()) {
SimpleName field = context.getFieldName(property);
statements.add(f.newIfStatement(
new ExpressionBuilder(f, f.newThis())
.field(field)
.method("equals", new ExpressionBuilder(f, other) //$NON-NLS-1$
.field(field)
.toExpression())
.apply(InfixOperator.EQUALS, Models.toLiteral(f, false))
.toExpression(),
f.newBlock(f.newReturnStatement(Models.toLiteral(f, false)))));
}
statements.add(f.newReturnStatement(Models.toLiteral(f, true)));
return f.newMethodDeclaration(
null,
new AttributeBuilder(f)
.annotation(context.resolve(Override.class))
.Public()
.toAttributes(),
Models.toType(f, boolean.class),
f.newSimpleName("equals"), //$NON-NLS-1$
Collections.singletonList(f.newFormalParameterDeclaration(
context.resolve(Object.class),
obj)),
statements);
}