Generate correct hashCode when callSuper and no fields included. Fixes #1505

This commit is contained in:
Roel Spilker committed 2017-11-06 23:46:09 +01:00
1 parent 7fad34b0a6
commit 045638ec1f
12 files changed
+41 -48

No files matched your search

@@ -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();
@@ -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("$");
@@ -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;
}
@@ -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;
}
}
@@ -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;
}
@@ -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;
}
@@ -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
@@ -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;
}
@@ -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;
}
}
@@ -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;
}
@@ -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;
}
@@ -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() {