aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2011-07-18 20:59:52 +0200
committerRoel Spilker <r.spilker@gmail.com>2011-07-18 21:05:00 +0200
commit83e2fb5e00e1868f0b4f0fe38b1ea1383119f8ee (patch)
tree68f44e863293287e989b120a1d15de88de338a2a /src
parentaa1a0e7ac87f5e96a39d0bad670aa8c7b7df85d4 (diff)
downloadlombok-83e2fb5e00e1868f0b4f0fe38b1ea1383119f8ee.tar.gz
lombok-83e2fb5e00e1868f0b4f0fe38b1ea1383119f8ee.tar.bz2
lombok-83e2fb5e00e1868f0b4f0fe38b1ea1383119f8ee.zip
Now either all or none of equals/hashCode/canEqual is generated. Fixes issue 240.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java2
-rw-r--r--src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java58
-rw-r--r--src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java63
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java2
4 files changed, 49 insertions, 76 deletions
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<EqualsAndH
}
}
- boolean needsCanEqual = false;
- switch (methodExists("equals", typeNode)) {
- case NOT_EXISTS:
- boolean isFinal = (typeDecl.modifiers & ClassFileConstants.AccFinal) != 0;
- needsCanEqual = !isDirectDescendantOfObject || !isFinal;
-
- MethodDeclaration equals = createEquals(typeNode, nodesForEquality, callSuper, errorNode.get(), fieldAccess, needsCanEqual);
- injectMethod(typeNode, equals);
- break;
+ boolean isFinal = (typeDecl.modifiers & ClassFileConstants.AccFinal) != 0;
+ boolean needsCanEqual = !isDirectDescendantOfObject || !isFinal;
+ java.util.List<MemberExistsResult> existsResults = new ArrayList<MemberExistsResult>();
+ 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<EclipseNode> fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess) {
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
index d77be508..eb733eb4 100644
--- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -22,6 +22,10 @@
package lombok.javac.handlers;
import static lombok.javac.handlers.JavacHandlerUtil.*;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
import lombok.EqualsAndHashCode;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
@@ -170,51 +174,36 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
}
}
- boolean needsCanEqual = false;
- switch (methodExists("equals", typeNode)) {
- case NOT_EXISTS:
- boolean isFinal = (((JCClassDecl)typeNode.get()).mods.flags & Flags.FINAL) != 0;
- needsCanEqual = !isFinal || !isDirectDescendantOfObject;
-
- JCMethodDecl method = createEquals(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, needsCanEqual, source.get());
- injectMethod(typeNode, method);
- break;
+ boolean isFinal = (((JCClassDecl)typeNode.get()).mods.flags & Flags.FINAL) != 0;
+ boolean needsCanEqual = !isFinal || !isDirectDescendantOfObject;
+ java.util.List<MemberExistsResult> existsResults = new ArrayList<MemberExistsResult>();
+ 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) {
- source.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");
+ source.addWarning(msg);
}
- break;
+ return;
+ case NOT_EXISTS:
+ default:
+ //fallthrough
}
+ JCMethodDecl equalsMethod = createEquals(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, needsCanEqual, source.get());
+ injectMethod(typeNode, equalsMethod);
+
if (needsCanEqual) {
- switch (methodExists("canEqual", typeNode)) {
- case NOT_EXISTS:
- JCMethodDecl method = createCanEqual(typeNode, source.get());
- injectMethod(typeNode, method);
- break;
- case EXISTS_BY_LOMBOK:
- case EXISTS_BY_USER:
- default:
- break;
- }
- }
- switch (methodExists("hashCode", typeNode)) {
- case NOT_EXISTS:
- JCMethodDecl method = createHashCode(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, source.get());
- injectMethod(typeNode, method);
- break;
- case EXISTS_BY_LOMBOK:
- break;
- default:
- case EXISTS_BY_USER:
- if (whineIfExists) {
- source.addWarning("Not generating hashCode(): A method with that name already exists");
- }
- break;
+ JCMethodDecl canEqualMethod = createCanEqual(typeNode, source.get());
+ injectMethod(typeNode, canEqualMethod);
}
+
+ JCMethodDecl hashCodeMethod = createHashCode(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, source.get());
+ injectMethod(typeNode, hashCodeMethod);
}
private JCMethodDecl createHashCode(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, JCTree source) {
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 0cbe2c83..03a9142b 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -185,7 +185,7 @@ public class JavacHandlerUtil {
/** 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;
}
/**