diff options
author | Andre Brait <andrebrait@gmail.com> | 2020-08-19 17:59:19 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2020-09-24 22:25:15 +0200 |
commit | 8d004c0638e4b865c38527a500c493288328eac8 (patch) | |
tree | 33c9db820c79f980ab596d6501a802ceb7b0237a /src/core/lombok/eclipse | |
parent | 0808d4271a495e1b6ea4f9a636e63a1c62567133 (diff) | |
download | lombok-8d004c0638e4b865c38527a500c493288328eac8.tar.gz lombok-8d004c0638e4b865c38527a500c493288328eac8.tar.bz2 lombok-8d004c0638e4b865c38527a500c493288328eac8.zip |
Allow non-final types, use Integer.MIN_VALUE
Allow caching hash code for non-final classes (but will warn)
Use Integer.MIN_VALUE to differentiate uncached and 0 cached
Diffstat (limited to 'src/core/lombok/eclipse')
-rwxr-xr-x | src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 23 |
1 files changed, 16 insertions, 7 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); |