From 83e2fb5e00e1868f0b4f0fe38b1ea1383119f8ee Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 18 Jul 2011 20:59:52 +0200 Subject: Now either all or none of equals/hashCode/canEqual is generated. Fixes issue 240. --- .../eclipse/handlers/EclipseHandlerUtil.java | 2 +- .../eclipse/handlers/HandleEqualsAndHashCode.java | 58 ++++++++-------------- 2 files changed, 22 insertions(+), 38 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 159a1994..627acecb 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -292,7 +292,7 @@ public class EclipseHandlerUtil { /** Serves as return value for the methods that check for the existence of fields and methods. */ public enum MemberExistsResult { - NOT_EXISTS, EXISTS_BY_USER, EXISTS_BY_LOMBOK; + NOT_EXISTS, EXISTS_BY_LOMBOK, EXISTS_BY_USER; } /** diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 05fbd0e2..db45c087 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -203,52 +203,36 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler existsResults = new ArrayList(); + existsResults.add(methodExists("equals", typeNode)); + existsResults.add(methodExists("hashCode", typeNode)); + existsResults.add(methodExists("canEqual", typeNode)); + switch (Collections.max(existsResults)) { case EXISTS_BY_LOMBOK: - break; - default: + return; case EXISTS_BY_USER: if (whineIfExists) { - errorNode.addWarning("Not generating equals(Object other): A method with that name already exists"); + String msg = String.format("Not generating equals%s: A method with one of those names already exists. (Either all or none of these methods will be generated).", needsCanEqual ? ", hashCode and canEquals" : " and hashCode"); + errorNode.addWarning(msg); } - break; + return; + case NOT_EXISTS: + default: + //fallthrough } + MethodDeclaration equalsMethod = createEquals(typeNode, nodesForEquality, callSuper, errorNode.get(), fieldAccess, needsCanEqual); + injectMethod(typeNode, equalsMethod); + if (needsCanEqual) { - switch (methodExists("canEqual", typeNode)) { - case NOT_EXISTS: - MethodDeclaration equals = createCanEqual(typeNode, errorNode.get()); - injectMethod(typeNode, equals); - break; - case EXISTS_BY_LOMBOK: - case EXISTS_BY_USER: - default: - break; - } + MethodDeclaration canEqualMethod = createCanEqual(typeNode, errorNode.get()); + injectMethod(typeNode, canEqualMethod); } - switch (methodExists("hashCode", typeNode)) { - case NOT_EXISTS: - MethodDeclaration hashCode = createHashCode(typeNode, nodesForEquality, callSuper, errorNode.get(), fieldAccess); - injectMethod(typeNode, hashCode); - break; - case EXISTS_BY_LOMBOK: - break; - default: - case EXISTS_BY_USER: - if (whineIfExists) { - errorNode.addWarning("Not generating hashCode(): A method with that name already exists"); - } - break; - } + MethodDeclaration hashCodeMethod = createHashCode(typeNode, nodesForEquality, callSuper, errorNode.get(), fieldAccess); + injectMethod(typeNode, hashCodeMethod); } private MethodDeclaration createHashCode(EclipseNode type, Collection fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { -- cgit