From 4e0ffbc32913001082dd2387a20b480076ddd20a Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 13 Jul 2020 17:15:03 +0200 Subject: Eclipse impl and tests --- .../eclipse/handlers/HandleEqualsAndHashCode.java | 77 ++++++++++++++++++++-- 1 file changed, 72 insertions(+), 5 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 7147343e..76a46814 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -36,6 +36,7 @@ import java.util.Set; import lombok.AccessLevel; import lombok.ConfigurationKeys; import lombok.EqualsAndHashCode; +import lombok.EqualsAndHashCode.CacheStrategy; import lombok.core.AST.Kind; import lombok.core.handlers.HandlerUtil; import lombok.core.handlers.InclusionExclusionUtils; @@ -47,6 +48,7 @@ import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; +import lombok.javac.JavacTreeMaker; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; @@ -59,9 +61,11 @@ import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FalseLiteral; +import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; import org.eclipse.jdt.internal.compiler.ast.IntLiteral; +import org.eclipse.jdt.internal.compiler.ast.IntLiteralMinValue; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -76,6 +80,7 @@ import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; +import org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.eclipse.jdt.internal.compiler.ast.SuperReference; import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TrueLiteral; @@ -84,16 +89,24 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; import org.eclipse.jdt.internal.compiler.ast.Wildcard; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; +import org.eclipse.jdt.internal.compiler.lookup.MethodScope; import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; import org.mangosdk.spi.ProviderFor; +import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.tree.JCTree.JCModifiers; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; + /** * Handles the {@code EqualsAndHashCode} annotation for eclipse. */ @ProviderFor(EclipseAnnotationHandler.class) public class HandleEqualsAndHashCode extends EclipseAnnotationHandler { + private static final String HASH_CODE_CACHE_NAME = "$hashCodeCache"; + + private final char[] HASH_CODE_CACHE_NAME_ARR = HASH_CODE_CACHE_NAME.toCharArray(); private final char[] PRIME = "PRIME".toCharArray(); private final char[] RESULT = "result".toCharArray(); @@ -116,7 +129,9 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler()); + generateMethods(typeNode, errorNode, members, null, false, false, access, new ArrayList()); } public void generateMethods(EclipseNode typeNode, EclipseNode errorNode, List> members, - Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, List onParam) { + Boolean callSuper, boolean whineIfExists, boolean cacheHashCode, FieldAccess fieldAccess, List onParam) { TypeDeclaration typeDecl = null; @@ -222,12 +237,38 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { + public MethodDeclaration createHashCode(EclipseNode type, Collection> members, boolean callSuper, boolean cacheHashCode, ASTNode source, FieldAccess fieldAccess) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; @@ -262,6 +303,20 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler Date: Mon, 13 Jul 2020 18:50:12 +0200 Subject: Finish implementation, add tests --- .../eclipse/handlers/HandleEqualsAndHashCode.java | 9 ++++-- .../javac/handlers/HandleEqualsAndHashCode.java | 18 +++++++----- .../after-delombok/EqualsAndHashCodeCache.java | 19 +++++++----- .../resource/after-ecj/EqualsAndHashCodeCache.java | 34 ++++++++++++---------- .../EqualsAndHashCodeCache.java.messages | 2 ++ 5 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 76a46814..6e9423d5 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -62,6 +62,7 @@ import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FalseLiteral; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; +import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; import org.eclipse.jdt.internal.compiler.ast.IntLiteral; @@ -305,8 +306,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler>> 32 ^ $d); result = result * PRIME + (this.b ? 79 : 97); - $hashCodeCache = result; + this.$hashCodeCache = result; return result; } } final class EqualsAndHashCode3 extends EqualsAndHashCode { + @java.lang.SuppressWarnings("all") private transient int $hashCodeCache = 0; @java.lang.Override @java.lang.SuppressWarnings("all") @@ -96,9 +98,9 @@ final class EqualsAndHashCode3 extends EqualsAndHashCode { @java.lang.Override @java.lang.SuppressWarnings("all") public int hashCode() { - if ($hashCodeCache != 0) return $hashCodeCache; + if (this.$hashCodeCache != 0) return this.$hashCodeCache; final int result = 1; - $hashCodeCache = result; + this.$hashCodeCache = result; return result; } } @@ -125,13 +127,14 @@ class EqualsAndHashCode4 extends EqualsAndHashCode { } } class EqualsAndHashCode5 extends EqualsAndHashCode { + @java.lang.SuppressWarnings("all") private transient int $hashCodeCache = 0; @java.lang.Override @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { if (o == this) return true; if (!(o instanceof EqualsAndHashCode5)) return false; - final EqualsAndHashCode5 other = (EqualsAndHashCode4) o; + final EqualsAndHashCode5 other = (EqualsAndHashCode5) o; if (!other.canEqual((java.lang.Object) this)) return false; if (!super.equals(o)) return false; return true; @@ -143,9 +146,9 @@ class EqualsAndHashCode5 extends EqualsAndHashCode { @java.lang.Override @java.lang.SuppressWarnings("all") public int hashCode() { - if ($hashCodeCache != 0) return $hashCodeCache; + if (this.$hashCodeCache != 0) return this.$hashCodeCache; final int result = super.hashCode(); - $hashCodeCache = result; + this.$hashCodeCache = result; return result; } } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java index 4eb4fbb3..3c4d0daa 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java @@ -48,12 +48,12 @@ } } final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode2 { + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; int x; long y; float f; double d; boolean b; - private transient int $hashCodeCache = 0; EqualsAndHashCode2() { super(); } @@ -76,8 +76,8 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt return true; } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { - if ($hashCodeCache != 0) - return $hashCodeCache; + if ((this.$hashCodeCache != 0)) + return this.$hashCodeCache; final int PRIME = 59; int result = 1; result = ((result * PRIME) + this.x); @@ -87,11 +87,12 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt final long $d = java.lang.Double.doubleToLongBits(this.d); result = ((result * PRIME) + (int) ($d ^ ($d >>> 32))); result = ((result * PRIME) + (this.b ? 79 : 97)); - $hashCodeCache = result; + this.$hashCodeCache = result; return result; } } -final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode { +final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode { + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; EqualsAndHashCode3() { super(); } @@ -109,11 +110,14 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn return (other instanceof EqualsAndHashCode3); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + if ((this.$hashCodeCache != 0)) + return this.$hashCodeCache; final int result = 1; + this.$hashCodeCache = result; return result; } } -@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode { +@lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode { EqualsAndHashCode4() { super(); } @@ -137,10 +141,10 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn return result; } } -@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode { - private transient int $hashCodeCache = 0; +final @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode { + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; EqualsAndHashCode5() { - super(); + super(); } public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { if ((o == this)) @@ -149,19 +153,19 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn return false; final EqualsAndHashCode5 other = (EqualsAndHashCode5) o; if ((! other.canEqual((java.lang.Object) this))) - return false; + return false; if ((! super.equals(o))) - return false; - return true; + return false; + return true; } protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof EqualsAndHashCode5); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { - if ($hashCodeCache != 0) - return $hashCodeCache; + if ((this.$hashCodeCache != 0)) + return this.$hashCodeCache; final int result = super.hashCode(); - $hashCodeCache = result; + this.$hashCodeCache = result; return result; } } diff --git a/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages b/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages new file mode 100644 index 00000000..24d202bd --- /dev/null +++ b/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages @@ -0,0 +1,2 @@ +1 Not caching the result of hashCode: Annotated type is not final. +23 Not caching the result of hashCode: Annotated type is not final. \ No newline at end of file -- cgit From beec8309ef5c088f0e9a2738ffd087d8777cdb50 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 13 Jul 2020 19:14:03 +0200 Subject: Removed unused imports --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 6e9423d5..47eceff0 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -48,8 +48,6 @@ import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; -import lombok.javac.JavacTreeMaker; - import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; @@ -66,7 +64,6 @@ import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; import org.eclipse.jdt.internal.compiler.ast.IntLiteral; -import org.eclipse.jdt.internal.compiler.ast.IntLiteralMinValue; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -81,7 +78,6 @@ import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; -import org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.eclipse.jdt.internal.compiler.ast.SuperReference; import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TrueLiteral; @@ -90,15 +86,10 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; import org.eclipse.jdt.internal.compiler.ast.Wildcard; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; -import org.eclipse.jdt.internal.compiler.lookup.MethodScope; import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; import org.mangosdk.spi.ProviderFor; -import com.sun.tools.javac.code.Flags; -import com.sun.tools.javac.tree.JCTree.JCModifiers; -import com.sun.tools.javac.tree.JCTree.JCVariableDecl; - /** * Handles the {@code EqualsAndHashCode} annotation for eclipse. */ -- cgit From 4788df3fb39a981fd7c6f055823ead221de04643 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 13 Jul 2020 19:15:06 +0200 Subject: Readd line removed by Eclipse --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 47eceff0..b1fc2bb2 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -48,6 +48,7 @@ import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; + import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; -- cgit From 04a10be919ffe89142d173cf384f23c9b9b39f73 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Tue, 14 Jul 2020 10:01:39 +0200 Subject: Change documentation to reflect code --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index b1fc2bb2..93bc92de 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -296,7 +296,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler Date: Tue, 14 Jul 2020 11:17:07 +0200 Subject: Fix using source start instead of source end Co-authored-by: Rawi01 --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 93bc92de..44676b3c 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -304,7 +304,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler Date: Tue, 14 Jul 2020 11:18:07 +0200 Subject: Add missing set source/statement start/end Co-authored-by: Rawi01 --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 44676b3c..4c5f3083 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -450,6 +450,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler Date: Tue, 14 Jul 2020 12:25:25 +0200 Subject: Undo unnecessary change, add TODO --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 1 + src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 4c5f3083..cf735dee 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -272,6 +272,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler> members = InclusionExclusionUtils.handleEqualsAndHashCodeMarking(typeNode, null, null); generateMethods(typeNode, source, members, null, false, false, access, List.nil()); -- cgit From 32db0ef71022040cb574a96d448f4b44518f8637 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Tue, 14 Jul 2020 16:43:04 +0200 Subject: Generate Pure annotation for cached hashCode --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 6 ++++-- src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index cf735dee..37379bb9 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -272,8 +272,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandlernil()); List annsOnMethod = List.of(overrideAnnotation); CheckerFrameworkVersion checkerFramework = getCheckerFrameworkVersion(typeNode); - // TODO: maybe not add this annotation if cacheHashCode is true because we *do* modify a field - if (checkerFramework.generateSideEffectFree()) annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())); + if (cacheHashCode && checkerFramework.generatePure()) { + annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__PURE), List.nil())); + } else if (checkerFramework.generateSideEffectFree()) { + annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())); + } JCModifiers mods = maker.Modifiers(Flags.PUBLIC, annsOnMethod); JCExpression returnType = maker.TypeIdent(CTC_INT); ListBuffer statements = new ListBuffer(); -- cgit From 8d004c0638e4b865c38527a500c493288328eac8 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Wed, 19 Aug 2020 17:59:19 +0200 Subject: 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 --- .../eclipse/handlers/HandleEqualsAndHashCode.java | 23 +++++++++++++++------- .../javac/handlers/HandleEqualsAndHashCode.java | 18 ++++++++++------- .../after-delombok/EqualsAndHashCodeCache.java | 14 ++++++++++--- .../resource/after-ecj/EqualsAndHashCodeCache.java | 14 ++++++++++--- .../EqualsAndHashCodeCache.java.messages | 4 ++-- .../EqualsAndHashCodeCache.java.messages | 4 ++-- 6 files changed, 53 insertions(+), 24 deletions(-) (limited to 'src/core/lombok/eclipse') 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>> 32 ^ $d); result = result * PRIME + (this.b ? 79 : 97); - this.$hashCodeCache = result; + this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; return result; } } @@ -100,11 +104,13 @@ final class EqualsAndHashCode3 extends EqualsAndHashCode { public int hashCode() { if (this.$hashCodeCache != 0) return this.$hashCodeCache; final int result = 1; - this.$hashCodeCache = result; + this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; return result; } } class EqualsAndHashCode4 extends EqualsAndHashCode { + @java.lang.SuppressWarnings("all") + private transient int $hashCodeCache = 0; @java.lang.Override @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { @@ -122,7 +128,9 @@ class EqualsAndHashCode4 extends EqualsAndHashCode { @java.lang.Override @java.lang.SuppressWarnings("all") public int hashCode() { + if (this.$hashCodeCache != 0) return this.$hashCodeCache; final int result = super.hashCode(); + this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; return result; } } @@ -148,7 +156,7 @@ final class EqualsAndHashCode5 extends EqualsAndHashCode { public int hashCode() { if (this.$hashCodeCache != 0) return this.$hashCodeCache; final int result = super.hashCode(); - this.$hashCodeCache = result; + this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; return result; } } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java index 3c4d0daa..b29fd6b3 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java @@ -1,4 +1,5 @@ @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode { + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; int x; boolean[] y; Object[] z; @@ -35,6 +36,8 @@ return (other instanceof EqualsAndHashCode); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + if ((this.$hashCodeCache != 0)) + return this.$hashCodeCache; final int PRIME = 59; int result = 1; result = ((result * PRIME) + this.x); @@ -44,6 +47,7 @@ result = ((result * PRIME) + (($a == null) ? 43 : $a.hashCode())); final java.lang.Object $b = this.b; result = ((result * PRIME) + (($b == null) ? 43 : $b.hashCode())); + this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); return result; } } @@ -87,7 +91,7 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt final long $d = java.lang.Double.doubleToLongBits(this.d); result = ((result * PRIME) + (int) ($d ^ ($d >>> 32))); result = ((result * PRIME) + (this.b ? 79 : 97)); - this.$hashCodeCache = result; + this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); return result; } } @@ -113,11 +117,12 @@ final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsA if ((this.$hashCodeCache != 0)) return this.$hashCodeCache; final int result = 1; - this.$hashCodeCache = result; + this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); return result; } } @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode { + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; EqualsAndHashCode4() { super(); } @@ -137,7 +142,10 @@ final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsA return (other instanceof EqualsAndHashCode4); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + if ((this.$hashCodeCache != 0)) + return this.$hashCodeCache; final int result = super.hashCode(); + this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); return result; } } @@ -165,7 +173,7 @@ final @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAn if ((this.$hashCodeCache != 0)) return this.$hashCodeCache; final int result = super.hashCode(); - this.$hashCodeCache = result; + this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); return result; } } diff --git a/test/transform/resource/messages-delombok/EqualsAndHashCodeCache.java.messages b/test/transform/resource/messages-delombok/EqualsAndHashCodeCache.java.messages index 24d202bd..f1686a6e 100644 --- a/test/transform/resource/messages-delombok/EqualsAndHashCodeCache.java.messages +++ b/test/transform/resource/messages-delombok/EqualsAndHashCodeCache.java.messages @@ -1,2 +1,2 @@ -1 Not caching the result of hashCode: Annotated type is not final. -23 Not caching the result of hashCode: Annotated type is not final. \ No newline at end of file +1 Caching the result of hashCode for non-final type. +23 Caching the result of hashCode for non-final type. \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages b/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages index 24d202bd..f1686a6e 100644 --- a/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages +++ b/test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages @@ -1,2 +1,2 @@ -1 Not caching the result of hashCode: Annotated type is not final. -23 Not caching the result of hashCode: Annotated type is not final. \ No newline at end of file +1 Caching the result of hashCode for non-final type. +23 Caching the result of hashCode for non-final type. \ No newline at end of file -- cgit From ee6c0ca7f2274b24571afdc8586eff9052f5eda1 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 14 Sep 2020 19:59:54 +0200 Subject: Remove warning for final classes --- src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 4 ---- src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 4 ---- .../resource/messages-delombok/EqualsAndHashCodeCache.java.messages | 2 -- .../resource/messages-ecj/EqualsAndHashCodeCache.java.messages | 2 -- 4 files changed, 12 deletions(-) delete mode 100644 test/transform/resource/messages-delombok/EqualsAndHashCodeCache.java.messages delete mode 100644 test/transform/resource/messages-ecj/EqualsAndHashCodeCache.java.messages (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index d5fa5618..2f0ce227 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -237,10 +237,6 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler Date: Fri, 25 Sep 2020 00:13:50 +0200 Subject: Cache hashCode: - Fix bug where 0 would be returned once - Fix Eclipse position error - Don't initialize field with default value 0 --- .../eclipse/handlers/HandleEqualsAndHashCode.java | 44 +++++++++++++++------- .../javac/handlers/HandleEqualsAndHashCode.java | 22 ++++++----- .../after-delombok/EqualsAndHashCodeCache.java | 31 ++++++++------- .../resource/after-ecj/EqualsAndHashCodeCache.java | 36 +++++++++++------- 4 files changed, 84 insertions(+), 49 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 2f0ce227..deb19c00 100755 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -246,12 +246,11 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler member : members) { @@ -345,14 +344,17 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler>> 32 ^ $d); result = result * PRIME + (this.b ? 79 : 97); - this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; + if (result == 0) result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } final class EqualsAndHashCode3 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") - private transient int $hashCodeCache = 0; + private transient int $hashCodeCache; @java.lang.Override @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { @@ -103,14 +105,15 @@ final class EqualsAndHashCode3 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") public int hashCode() { if (this.$hashCodeCache != 0) return this.$hashCodeCache; - final int result = 1; - this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; + int result = 1; + if (result == 0) result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } class EqualsAndHashCode4 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") - private transient int $hashCodeCache = 0; + private transient int $hashCodeCache; @java.lang.Override @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { @@ -129,14 +132,15 @@ class EqualsAndHashCode4 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") public int hashCode() { if (this.$hashCodeCache != 0) return this.$hashCodeCache; - final int result = super.hashCode(); - this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; + int result = super.hashCode(); + if (result == 0) result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } final class EqualsAndHashCode5 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") - private transient int $hashCodeCache = 0; + private transient int $hashCodeCache; @java.lang.Override @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { @@ -155,8 +159,9 @@ final class EqualsAndHashCode5 extends EqualsAndHashCode { @java.lang.SuppressWarnings("all") public int hashCode() { if (this.$hashCodeCache != 0) return this.$hashCodeCache; - final int result = super.hashCode(); - this.$hashCodeCache = result != 0 ? result : java.lang.Integer.MIN_VALUE; + int result = super.hashCode(); + if (result == 0) result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java index b29fd6b3..7094b636 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java @@ -1,5 +1,5 @@ @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode { - private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache; int x; boolean[] y; Object[] z; @@ -47,12 +47,14 @@ result = ((result * PRIME) + (($a == null) ? 43 : $a.hashCode())); final java.lang.Object $b = this.b; result = ((result * PRIME) + (($b == null) ? 43 : $b.hashCode())); - this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); + if ((result == 0)) + result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode2 { - private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache; int x; long y; float f; @@ -91,12 +93,14 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt final long $d = java.lang.Double.doubleToLongBits(this.d); result = ((result * PRIME) + (int) ($d ^ ($d >>> 32))); result = ((result * PRIME) + (this.b ? 79 : 97)); - this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); + if ((result == 0)) + result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode { - private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache; EqualsAndHashCode3() { super(); } @@ -116,13 +120,15 @@ final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsA public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { if ((this.$hashCodeCache != 0)) return this.$hashCodeCache; - final int result = 1; - this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); + int result = 1; + if ((result == 0)) + result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode { - private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache; EqualsAndHashCode4() { super(); } @@ -144,13 +150,15 @@ final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsA public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { if ((this.$hashCodeCache != 0)) return this.$hashCodeCache; - final int result = super.hashCode(); - this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); + int result = super.hashCode(); + if ((result == 0)) + result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } final @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode { - private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0; + private transient @java.lang.SuppressWarnings("all") int $hashCodeCache; EqualsAndHashCode5() { super(); } @@ -172,8 +180,10 @@ final @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAn public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { if ((this.$hashCodeCache != 0)) return this.$hashCodeCache; - final int result = super.hashCode(); - this.$hashCodeCache = ((result != 0) ? result : java.lang.Integer.MIN_VALUE); + int result = super.hashCode(); + if ((result == 0)) + result = java.lang.Integer.MIN_VALUE; + this.$hashCodeCache = result; return result; } } -- cgit