diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 23 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 18 |
2 files changed, 27 insertions, 14 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 37379bb9..d5fa5618 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -231,16 +231,16 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH } if (cacheHashCode){ - if (!isFinal) { - String msg = "Not caching the result of hashCode: Annotated type is not final."; - errorNode.addWarning(msg); - cacheHashCode = false; - } else if (fieldExists(HASH_CODE_CACHE_NAME, typeNode) != MemberExistsResult.NOT_EXISTS) { + if (fieldExists(HASH_CODE_CACHE_NAME, typeNode) != MemberExistsResult.NOT_EXISTS) { String msg = String.format("Not caching the result of hashCode: A field named %s already exists.", HASH_CODE_CACHE_NAME); errorNode.addWarning(msg); cacheHashCode = false; } else { createHashCodeCacheField(typeNode, errorNode.get()); + if (!isFinal) { + String msg = "Caching the result of hashCode for non-final type."; + errorNode.addWarning(msg); + } } } @@ -444,7 +444,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH } } - /* this.$hashCodeCache = result; */ { + /* this.$hashCodeCache = result != 0 ? result : Integer.MIN_VALUE; */ { if (cacheHashCode) { FieldReference hashCodeCacheRef = new FieldReference(HASH_CODE_CACHE_NAME_ARR, p); hashCodeCacheRef.receiver = new ThisReference(pS, pE); @@ -452,7 +452,16 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH setGeneratedBy(hashCodeCacheRef.receiver, source); SingleNameReference resultRef = new SingleNameReference(RESULT, p); setGeneratedBy(resultRef, source); - Assignment cacheResult = new Assignment(hashCodeCacheRef, resultRef, pE); + EqualExpression resultNotZero = new EqualExpression(resultRef, makeIntLiteral("0".toCharArray(), source), OperatorIds.NOT_EQUAL); + setGeneratedBy(resultNotZero, source); + FieldReference integerMinValue = new FieldReference("MIN_VALUE".toCharArray(), p); + integerMinValue.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_INTEGER); + setGeneratedBy(integerMinValue, source); + resultRef = new SingleNameReference(RESULT, p); + setGeneratedBy(resultRef, source); + ConditionalExpression notZeroOrIntegerMin = new ConditionalExpression(resultNotZero, resultRef, integerMinValue); + setGeneratedBy(notZeroOrIntegerMin, source); + Assignment cacheResult = new Assignment(hashCodeCacheRef, notZeroOrIntegerMin, pE); cacheResult.sourceStart = pS; cacheResult.statementEnd = cacheResult.sourceEnd = pE; setGeneratedBy(cacheResult, source); statements.add(cacheResult); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 58078038..d90fdada 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -40,6 +40,7 @@ import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree; import com.sun.tools.javac.tree.JCTree.JCBinary; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCConditional; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; @@ -204,16 +205,16 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas } if (cacheHashCode){ - if (!isFinal) { - String msg = "Not caching the result of hashCode: Annotated type is not final."; - source.addWarning(msg); - cacheHashCode = false; - } else if (fieldExists(HASH_CODE_CACHE_NAME, typeNode) != MemberExistsResult.NOT_EXISTS) { + if (fieldExists(HASH_CODE_CACHE_NAME, typeNode) != MemberExistsResult.NOT_EXISTS) { String msg = String.format("Not caching the result of hashCode: A field named %s already exists.", HASH_CODE_CACHE_NAME); source.addWarning(msg); cacheHashCode = false; } else { createHashCodeCacheField(typeNode, source.get()); + if (!isFinal) { + String msg = "Caching the result of hashCode for non-final type."; + source.addWarning(msg); + } } } @@ -348,11 +349,14 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas } } - /* this.$hashCodeCache = result; */ { + /* this.$hashCodeCache = result != 0 ? result : Integer.MIN_VALUE; */ { if (cacheHashCode) { JCIdent receiver = maker.Ident(typeNode.toName("this")); JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME)); - statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, maker.Ident(resultName)))); + JCExpression resultNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(resultName), maker.Literal(CTC_INT, 0)); + JCExpression integerMinValue = genJavaLangTypeRef(typeNode, "Integer", "MIN_VALUE"); + JCConditional notZeroOrIntegerMin = maker.Conditional(resultNotZero, maker.Ident(resultName), integerMinValue); + statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, notZeroOrIntegerMin))); } } |