private NodeAndImports<MethodDeclaration> createPrimaryKeyEquals(Collection<FieldDescriptor> primaryKeyDescriptors, String enclosingClassName) {
final MethodDeclaration equals = new MethodDeclaration(ModifierSet.PUBLIC, new PrimitiveType(PrimitiveType.Primitive.Boolean), "equals",
Collections.singletonList(new Parameter(new ClassOrInterfaceType("Object"), new VariableDeclaratorId("other"))));
equals.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override"))));
final Statement ifEqualNullStmt = new IfStmt(new BinaryExpr(new NameExpr("other"), new NullLiteralExpr(), BinaryExpr.Operator.equals), new ReturnStmt(new BooleanLiteralExpr(false)), null);
final Statement ifEqualThisStmt = new IfStmt(new BinaryExpr(new NameExpr("other"), new ThisExpr(), BinaryExpr.Operator.equals), new ReturnStmt(new BooleanLiteralExpr(true)), null);
final Statement ifEqualClassStmt = new IfStmt(new BinaryExpr(new MethodCallExpr(new NameExpr("other"), "getClass"), new MethodCallExpr(new ThisExpr(), "getClass"), BinaryExpr.Operator.notEquals), new ReturnStmt(new BooleanLiteralExpr(false)), null);
final Statement rhsStmt = new ExpressionStmt(new VariableDeclarationExpr(ModifierSet.FINAL,
new ClassOrInterfaceType(enclosingClassName), Collections.singletonList(new VariableDeclarator(
new VariableDeclaratorId("rhs"),
new CastExpr(new ClassOrInterfaceType(enclosingClassName), new NameExpr("other"))))));
Expression equalsBuilderExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("EqualsBuilder"), Collections.<Expression>emptyList());
for (FieldDescriptor f : primaryKeyDescriptors) {
final List<Expression> args = new ArrayList<Expression>();
args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName()));
args.add(new FieldAccessExpr(new NameExpr("rhs"), f.getAttributeName()));
equalsBuilderExpr = new MethodCallExpr(equalsBuilderExpr, "append", args);
}
equalsBuilderExpr = new MethodCallExpr(equalsBuilderExpr, "isEquals");
final List<Statement> statements = new ArrayList<Statement>();
statements.add(ifEqualNullStmt);
statements.add(ifEqualThisStmt);
statements.add(ifEqualClassStmt);
statements.add(rhsStmt);
statements.add(new ReturnStmt(equalsBuilderExpr));
final BlockStmt equalsBody = new BlockStmt(statements);
equals.setBody(equalsBody);
return new NodeAndImports<MethodDeclaration>(equals,
Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "EqualsBuilder"), false, false)));
}