diff options
author | Roel Spilker <r.spilker@gmail.com> | 2017-11-06 23:45:52 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2017-11-06 23:46:09 +0100 |
commit | 045638ec1f79f68747f135061d2e026faa719642 (patch) | |
tree | debabf20d4cb9af4c8c6ed20d6044744c537705f | |
parent | 7fad34b0a6fbda12f07c62a571cbe870b550be97 (diff) | |
download | lombok-045638ec1f79f68747f135061d2e026faa719642.tar.gz lombok-045638ec1f79f68747f135061d2e026faa719642.tar.bz2 lombok-045638ec1f79f68747f135061d2e026faa719642.zip |
Generate correct hashCode when callSuper and no fields included. Fixes #1505
12 files changed, 41 insertions, 48 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 37fa78ab..0d0f51db 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -299,7 +299,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH /* final int PRIME = X; */ { /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */ - if (!isEmpty || callSuper) { + if (!isEmpty) { LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE); setGeneratedBy(primeDecl, source); primeDecl.modifiers |= Modifier.FINAL; @@ -311,26 +311,30 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH } } - /* int result = 1; */ { - LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); + /*int result = ... */{ + LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); setGeneratedBy(resultDecl, source); - resultDecl.initialization = makeIntLiteral("1".toCharArray(), source); + final Expression init; + if (callSuper) { + /* ... super.hashCode(); */ + MessageSend callToSuper = new MessageSend(); + setGeneratedBy(callToSuper, source); + callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; + callToSuper.receiver = new SuperReference(pS, pE); + setGeneratedBy(callToSuper.receiver, source); + callToSuper.selector = "hashCode".toCharArray(); + init = callToSuper; + } else { + /* ... 1; */ + init = makeIntLiteral("1".toCharArray(), source); + } + resultDecl.initialization = init; resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; setGeneratedBy(resultDecl.type, source); statements.add(resultDecl); } - if (callSuper) { - MessageSend callToSuper = new MessageSend(); - setGeneratedBy(callToSuper, source); - callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; - callToSuper.receiver = new SuperReference(pS, pE); - setGeneratedBy(callToSuper.receiver, source); - callToSuper.selector = "hashCode".toCharArray(); - statements.add(createResultCalculation(source, callToSuper)); - } - for (EclipseNode field : fields) { TypeReference fType = getFieldType(field, fieldAccess); char[] dollarFieldName = ("$" + field.getName()).toCharArray(); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 4bd1ce4b..da2db909 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -258,20 +258,23 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext()); /* final int PRIME = X; */ { - if (!fields.isEmpty() || callSuper) { + if (!fields.isEmpty()) { statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode()))); } } - /* int result = 1; */ { - statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(CTC_INT), maker.Literal(1))); - } - - if (callSuper) { - JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), - maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")), - List.<JCExpression>nil()); - statements.append(createResultCalculation(typeNode, callToSuper)); + /* int result = ... */ { + final JCExpression init; + if (callSuper) { + /* ... super.hashCode(); */ + init = maker.Apply(List.<JCExpression>nil(), + maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")), + List.<JCExpression>nil()); + } else { + /* ... 1; */ + init = maker.Literal(1); + } + statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(CTC_INT), init)); } Name dollar = typeNode.toName("$"); diff --git a/test/transform/resource/after-delombok/DataPlain.java b/test/transform/resource/after-delombok/DataPlain.java index 6d109653..b4178878 100644 --- a/test/transform/resource/after-delombok/DataPlain.java +++ b/test/transform/resource/after-delombok/DataPlain.java @@ -217,8 +217,7 @@ final class Data4 extends java.util.Timer { @javax.annotation.Generated("lombok") public int hashCode() { final int PRIME = 59; - int result = 1; - result = result * PRIME + super.hashCode(); + int result = super.hashCode(); result = result * PRIME + this.getX(); return result; } diff --git a/test/transform/resource/after-delombok/EqualsAndHashCode.java b/test/transform/resource/after-delombok/EqualsAndHashCode.java index 67e07d7f..dbb4f151 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCode.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCode.java @@ -125,9 +125,7 @@ class EqualsAndHashCode4 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") public int hashCode() { - final int PRIME = 59; - int result = 1; - result = result * PRIME + super.hashCode(); + int result = super.hashCode(); return result; } } diff --git a/test/transform/resource/after-delombok/EqualsAndHashCodeConfigKeys2.java b/test/transform/resource/after-delombok/EqualsAndHashCodeConfigKeys2.java index 333b6b58..9ba32222 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCodeConfigKeys2.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCodeConfigKeys2.java @@ -70,8 +70,7 @@ class EqualsAndHashCodeConfigKeys2 extends EqualsAndHashCodeConfigKeys2Parent { @javax.annotation.Generated("lombok") public int hashCode() { final int PRIME = 59; - int result = 1; - result = result * PRIME + super.hashCode(); + int result = super.hashCode(); result = result * PRIME + this.x; return result; } diff --git a/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java b/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java index d9e4a913..ea7d504b 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java @@ -32,8 +32,7 @@ final class EqualsAndHashCodeWithExistingMethods3 extends EqualsAndHashCodeWithE @javax.annotation.Generated("lombok") public int hashCode() { final int PRIME = 59; - int result = 1; - result = result * PRIME + super.hashCode(); + int result = super.hashCode(); result = result * PRIME + this.x; return result; } diff --git a/test/transform/resource/after-delombok/ValueCallSuper.java b/test/transform/resource/after-delombok/ValueCallSuper.java index e0065885..6214e64f 100644 --- a/test/transform/resource/after-delombok/ValueCallSuper.java +++ b/test/transform/resource/after-delombok/ValueCallSuper.java @@ -25,9 +25,7 @@ final class ValueCallSuper extends ValueParent { @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") public int hashCode() { - final int PRIME = 59; - int result = 1; - result = result * PRIME + super.hashCode(); + int result = super.hashCode(); return result; } @java.lang.Override diff --git a/test/transform/resource/after-ecj/DataPlain.java b/test/transform/resource/after-ecj/DataPlain.java index ba6c75c8..125d1dfa 100644 --- a/test/transform/resource/after-ecj/DataPlain.java +++ b/test/transform/resource/after-ecj/DataPlain.java @@ -168,8 +168,7 @@ final @Data @lombok.EqualsAndHashCode(callSuper = true) class Data4 extends java } public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() { final int PRIME = 59; - int result = 1; - result = ((result * PRIME) + super.hashCode()); + int result = super.hashCode(); result = ((result * PRIME) + this.getX()); return result; } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCode.java b/test/transform/resource/after-ecj/EqualsAndHashCode.java index c0bb5bbb..74936ff2 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCode.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCode.java @@ -129,9 +129,7 @@ final @lombok.EqualsAndHashCode(callSuper = false) class EqualsAndHashCode3 exte return (other instanceof EqualsAndHashCode4); } public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() { - final int PRIME = 59; - int result = 1; - result = ((result * PRIME) + super.hashCode()); + int result = super.hashCode(); return result; } } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeConfigKeys2.java b/test/transform/resource/after-ecj/EqualsAndHashCodeConfigKeys2.java index ff0e45c7..a0cc1046 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeConfigKeys2.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeConfigKeys2.java @@ -66,8 +66,7 @@ } public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() { final int PRIME = 59; - int result = 1; - result = ((result * PRIME) + super.hashCode()); + int result = super.hashCode(); result = ((result * PRIME) + this.x); return result; } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java b/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java index e4efb336..3b12e4e4 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java @@ -40,8 +40,7 @@ final @lombok.EqualsAndHashCode(callSuper = true) class EqualsAndHashCodeWithExi } public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() { final int PRIME = 59; - int result = 1; - result = ((result * PRIME) + super.hashCode()); + int result = super.hashCode(); result = ((result * PRIME) + this.x); return result; } diff --git a/test/transform/resource/after-ecj/ValueCallSuper.java b/test/transform/resource/after-ecj/ValueCallSuper.java index ecd4a1ef..4e84f42c 100644 --- a/test/transform/resource/after-ecj/ValueCallSuper.java +++ b/test/transform/resource/after-ecj/ValueCallSuper.java @@ -20,9 +20,7 @@ final @lombok.Value class ValueCallSuper extends ValueParent { return (other instanceof ValueCallSuper);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int hashCode() {
- final int PRIME = 59;
- int result = 1;
- result = ((result * PRIME) + super.hashCode());
+ int result = super.hashCode();
return result;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() {
|