From f397c2b4825828b5ad8a29ad1bee934a23199527 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Wed, 23 Sep 2020 23:29:25 +0200 Subject: [fixes #2586] Add Java 15 support for delombok --- src/delombok/lombok/delombok/Delombok.java | 8 ++++++-- src/stubs/com/sun/tools/javac/main/Arguments.java | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index a1fd0e56..6c15068a 100755 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -701,8 +701,12 @@ public class Delombok { if (!disablePreview && Javac.getJavaCompilerVersion() >= 11) argsList.add("--enable-preview"); - String[] argv = argsList.toArray(new String[0]); - args.init("javac", argv); + if (Javac.getJavaCompilerVersion() < 15) { + String[] argv = argsList.toArray(new String[0]); + args.init("javac", argv); + } else { + args.init("javac", argsList); + } options.put("diags.legacy", "TRUE"); options.put("allowStringFolding", "FALSE"); } else { diff --git a/src/stubs/com/sun/tools/javac/main/Arguments.java b/src/stubs/com/sun/tools/javac/main/Arguments.java index ea866b6e..3d91734c 100644 --- a/src/stubs/com/sun/tools/javac/main/Arguments.java +++ b/src/stubs/com/sun/tools/javac/main/Arguments.java @@ -10,4 +10,7 @@ public class Arguments { public void init(String ownName, String... argv) {} public Map getDeferredFileManagerOptions() { return null; } public boolean validate() { return false; } + + // JDK15 + public void init(String ownName, Iterable args) {} } -- cgit From 3019e73f965c40f0b72ad2b04553f3c9a694b3d2 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 9 Jul 2020 12:50:38 +0200 Subject: Attempt to implement cacheHashCode --- src/core/lombok/EqualsAndHashCode.java | 11 +++++ .../javac/handlers/HandleEqualsAndHashCode.java | 50 ++++++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index 6805d214..d73afe13 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -71,6 +71,17 @@ public @interface EqualsAndHashCode { * @return If {@code true}, always use direct field access instead of calling the getter method. */ boolean doNotUseGetters() default false; + + /** + * Enables caching the result of {@code hashCode}. + * This is useful to prevent running expensive calculations of {@code hashCode} multiple times for fully immutable objects, where it would always return the same result. + * It is similar to what {@link java.lang.String#hashCode} does. + * This should only be used for fully immutable classes (classes with all-immutable fields). + * default: false + * + * @return If {@code true}, cache the result of {@code hashCode} to avoid recalculating it in future invocations. + */ + boolean cacheHashCode() default false; /** * Any annotations listed here are put on the generated parameter of {@code equals} and {@code canEqual}. diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index d490738e..ade1e7c1 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -76,7 +76,8 @@ import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult; public class HandleEqualsAndHashCode extends JavacAnnotationHandler { private static final String RESULT_NAME = "result"; private static final String PRIME_NAME = "PRIME"; - + private static final String HASH_CODE_CACHE_NAME = "$hashCodeCache"; + @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.EQUALS_AND_HASH_CODE_FLAG_USAGE, "@EqualsAndHashCode"); @@ -92,8 +93,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler> members = InclusionExclusionUtils.handleEqualsAndHashCodeMarking(typeNode, null, null); - generateMethods(typeNode, source, members, null, false, access, List.nil()); + generateMethods(typeNode, source, members, null, false, false, access, List.nil()); } public void generateMethods(JavacNode typeNode, JavacNode source, java.util.List> members, - Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, List onParam) { + Boolean callSuper, boolean whineIfExists, boolean cacheHashCode, FieldAccess fieldAccess, List onParam) { boolean notAClass = true; if (typeNode.get() instanceof JCClassDecl) { @@ -195,21 +198,46 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler> members, boolean callSuper, FieldAccess fieldAccess, JCTree source) { + public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List> members, boolean callSuper, boolean cacheHashCode, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.nil()); 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())); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, annsOnMethod); JCExpression returnType = maker.TypeIdent(CTC_INT); ListBuffer statements = new ListBuffer(); + + Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME); + if (cacheHashCode) { + JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(cacheHashCodeName), maker.Literal(CTC_INT, 0)); + statements.append(maker.If(cacheNotZero, maker.Return(maker.Ident(cacheHashCodeName)), null)); + } Name primeName = typeNode.toName(PRIME_NAME); Name resultName = typeNode.toName(RESULT_NAME); @@ -305,6 +333,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler Date: Thu, 9 Jul 2020 13:11:52 +0200 Subject: Restore some missing white spaces removed by IDEA --- src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index ade1e7c1..6c3eccf4 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -77,7 +77,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.EQUALS_AND_HASH_CODE_FLAG_USAGE, "@EqualsAndHashCode"); @@ -198,7 +198,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler statements = new ListBuffer(); - + Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME); if (cacheHashCode) { JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(cacheHashCodeName), maker.Literal(CTC_INT, 0)); @@ -333,7 +333,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler Date: Mon, 13 Jul 2020 17:15:03 +0200 Subject: Eclipse impl and tests --- AUTHORS | 1 + src/core/lombok/EqualsAndHashCode.java | 139 ++++++++++------- .../eclipse/handlers/HandleEqualsAndHashCode.java | 77 +++++++++- .../javac/handlers/HandleEqualsAndHashCode.java | 38 +++-- .../after-delombok/EqualsAndHashCodeCache.java | 151 +++++++++++++++++++ .../resource/after-ecj/EqualsAndHashCodeCache.java | 167 +++++++++++++++++++++ .../resource/before/EqualsAndHashCodeCache.java | 29 ++++ 7 files changed, 531 insertions(+), 71 deletions(-) create mode 100644 test/transform/resource/after-delombok/EqualsAndHashCodeCache.java create mode 100644 test/transform/resource/after-ecj/EqualsAndHashCodeCache.java create mode 100644 test/transform/resource/before/EqualsAndHashCodeCache.java (limited to 'src') diff --git a/AUTHORS b/AUTHORS index 9adf2005..1b315c1b 100755 --- a/AUTHORS +++ b/AUTHORS @@ -2,6 +2,7 @@ Lombok contributors in alphabetical order: Adam Juraszek Aleksandr Zhelezniak +Andre Brait Bulgakov Alexander Caleb Brinkman Christian Nüssgens diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index d73afe13..7f60880c 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -27,18 +27,21 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Generates implementations for the {@code equals} and {@code hashCode} methods inherited by all objects, based on relevant fields. + * Generates implementations for the {@code equals} and {@code hashCode} methods + * inherited by all objects, based on relevant fields. *

- * Complete documentation is found at the project lombok features page for @EqualsAndHashCode. + * Complete documentation is found at + * the project + * lombok features page for @EqualsAndHashCode. */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.SOURCE) -public @interface EqualsAndHashCode { +@Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface EqualsAndHashCode { /** - * Any fields listed here will not be taken into account in the generated {@code equals} and {@code hashCode} implementations. - * Mutually exclusive with {@link #of()}. + * Any fields listed here will not be taken into account in the generated + * {@code equals} and {@code hashCode} implementations. Mutually exclusive + * with {@link #of()}. *

- * Will soon be marked {@code @Deprecated}; use the {@code @EqualsAndHashCode.Exclude} annotation instead. + * Will soon be marked {@code @Deprecated}; use the + * {@code @EqualsAndHashCode.Exclude} annotation instead. * * @return A list of fields to exclude. */ @@ -50,97 +53,129 @@ public @interface EqualsAndHashCode { *

* Mutually exclusive with {@link #exclude()}. *

- * Will soon be marked {@code @Deprecated}; use the {@code @EqualsAndHashCode.Include} annotation together with {@code @EqualsAndHashCode(onlyExplicitlyIncluded = true)}. + * Will soon be marked {@code @Deprecated}; use the + * {@code @EqualsAndHashCode.Include} annotation together with + * {@code @EqualsAndHashCode(onlyExplicitlyIncluded = true)}. * * @return A list of fields to use (default: all of them). */ String[] of() default {}; /** - * Call on the superclass's implementations of {@code equals} and {@code hashCode} before calculating for the fields in this class. + * Call on the superclass's implementations of {@code equals} and + * {@code hashCode} before calculating for the fields in this class. * default: false * - * @return Whether to call the superclass's {@code equals} implementation as part of the generated equals algorithm. + * @return Whether to call the superclass's {@code equals} implementation as + * part of the generated equals algorithm. */ boolean callSuper() default false; /** - * Normally, if getters are available, those are called. To suppress this and let the generated code use the fields directly, set this to {@code true}. - * default: false + * Normally, if getters are available, those are called. To suppress this + * and let the generated code use the fields directly, set this to + * {@code true}. default: false * - * @return If {@code true}, always use direct field access instead of calling the getter method. + * @return If {@code true}, always use direct field access instead of + * calling the getter method. */ boolean doNotUseGetters() default false; - + /** - * Enables caching the result of {@code hashCode}. - * This is useful to prevent running expensive calculations of {@code hashCode} multiple times for fully immutable objects, where it would always return the same result. - * It is similar to what {@link java.lang.String#hashCode} does. - * This should only be used for fully immutable classes (classes with all-immutable fields). - * default: false + * Determines how the result of the {@code hashCode} method will be cached. + * default: {@link CacheStrategy#NEVER} * - * @return If {@code true}, cache the result of {@code hashCode} to avoid recalculating it in future invocations. + * @return The {@code hashCode} cache strategy to be used. */ - boolean cacheHashCode() default false; + CacheStrategy cacheStrategy() default CacheStrategy.NEVER; /** - * Any annotations listed here are put on the generated parameter of {@code equals} and {@code canEqual}. - * This is useful to add for example a {@code Nullable} annotation.
- * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).
+ * Any annotations listed here are put on the generated parameter of + * {@code equals} and {@code canEqual}. This is useful to add for example a + * {@code Nullable} annotation.
+ * The syntax for this feature depends on JDK version (nothing we can do + * about that; it's to work around javac bugs).
* up to JDK7:
- * {@code @EqualsAndHashCode(onParam=@__({@AnnotationsGoHere}))}
+ * {@code @EqualsAndHashCode(onParam=@__({@AnnotationsGoHere}))}
* from JDK8:
- * {@code @EqualsAndHashCode(onParam_={@AnnotationsGohere})} // note the underscore after {@code onParam}. - * - * @return List of annotations to apply to the generated parameter in the {@code equals()} method. + * {@code @EqualsAndHashCode(onParam_={@AnnotationsGohere})} // note the + * underscore after {@code onParam}. + * + * @return List of annotations to apply to the generated parameter in the + * {@code equals()} method. */ AnyAnnotation[] onParam() default {}; /** - * Placeholder annotation to enable the placement of annotations on the generated code. + * Placeholder annotation to enable the placement of annotations on the + * generated code. + * * @deprecated Don't use this annotation, ever - Read the documentation. */ - @Deprecated - @Retention(RetentionPolicy.SOURCE) - @Target({}) - @interface AnyAnnotation {} + @Deprecated @Retention(RetentionPolicy.SOURCE) @Target({}) + @interface AnyAnnotation { + } /** - * Only include fields and methods explicitly marked with {@code @EqualsAndHashCode.Include}. - * Normally, all (non-static, non-transient) fields are included by default. + * Only include fields and methods explicitly marked with + * {@code @EqualsAndHashCode.Include}. Normally, all (non-static, + * non-transient) fields are included by default. * - * @return If {@code true}, don't include non-static non-transient fields automatically (default: {@code false}). + * @return If {@code true}, don't include non-static non-transient fields + * automatically (default: {@code false}). */ boolean onlyExplicitlyIncluded() default false; /** - * If present, do not include this field in the generated {@code equals} and {@code hashCode} methods. + * If present, do not include this field in the generated {@code equals} and + * {@code hashCode} methods. */ - @Target(ElementType.FIELD) - @Retention(RetentionPolicy.SOURCE) - public @interface Exclude {} + @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interface Exclude { + } /** - * Configure the behaviour of how this member is treated in the {@code equals} and {@code hashCode} implementation; if on a method, include the method's return value as part of calculating hashCode/equality. + * Configure the behaviour of how this member is treated in the + * {@code equals} and {@code hashCode} implementation; if on a method, + * include the method's return value as part of calculating + * hashCode/equality. */ - @Target({ElementType.FIELD, ElementType.METHOD}) - @Retention(RetentionPolicy.SOURCE) - public @interface Include { + @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.SOURCE) public @interface Include { /** - * Defaults to the method name of the annotated member. - * If on a method and the name equals the name of a default-included field, this member takes its place. + * Defaults to the method name of the annotated member. If on a method + * and the name equals the name of a default-included field, this member + * takes its place. * - * @return If present, this method serves as replacement for the named field. + * @return If present, this method serves as replacement for the named + * field. */ String replaces() default ""; - + /** - * Higher ranks are considered first. Members of the same rank are considered in the order they appear in the source file. + * Higher ranks are considered first. Members of the same rank are + * considered in the order they appear in the source file. * - * If not explicitly set, the {@code default} rank for primitives is 1000, and for primitive wrappers 800. + * If not explicitly set, the {@code default} rank for primitives is + * 1000, and for primitive wrappers 800. * - * @return ordering within the generating {@code equals} and {@code hashCode} methods; higher numbers are considered first. + * @return ordering within the generating {@code equals} and + * {@code hashCode} methods; higher numbers are considered + * first. */ int rank() default 0; } + + public enum CacheStrategy { + /** + * Never cache. Perform the calculation every time the method is called. + */ + NEVER, + /** + * Cache the result of the first invocation of {@code hashCode} and use it for subsequent invocations. + * This can improve performance in if all fields used for calculating the {@code hashCode} are immutable + * and thus every invocation of {@code hashCode} will always return the same value. + * Do not use this if there's any chance that different invocations of {@code hashCode} + * might return different values. + */ + LAZY + } } 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> members, boolean callSuper, boolean cacheHashCode, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); @@ -233,18 +238,20 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler statements = new ListBuffer(); - Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME); - if (cacheHashCode) { - JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(cacheHashCodeName), maker.Literal(CTC_INT, 0)); - statements.append(maker.If(cacheNotZero, maker.Return(maker.Ident(cacheHashCodeName)), null)); - } - Name primeName = typeNode.toName(PRIME_NAME); Name resultName = typeNode.toName(RESULT_NAME); long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext()); boolean isEmpty = members.isEmpty(); + /* if ($hashCodeCache != 0) return $hashCodeCache; */ { + if (cacheHashCode) { + Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME); + JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(cacheHashCodeName), maker.Literal(CTC_INT, 0)); + statements.append(maker.If(cacheNotZero, maker.Return(maker.Ident(cacheHashCodeName)), null)); + } + } + /* final int PRIME = X; */ { if (!isEmpty) { statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode()))); @@ -334,8 +341,11 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler>> 32 ^ $y); + result = result * PRIME + java.lang.Float.floatToIntBits(this.f); + final long $d = java.lang.Double.doubleToLongBits(this.d); + result = result * PRIME + (int) ($d >>> 32 ^ $d); + result = result * PRIME + (this.b ? 79 : 97); + $hashCodeCache = result; + return result; + } +} +final class EqualsAndHashCode3 extends EqualsAndHashCode { + 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 EqualsAndHashCode3)) return false; + final EqualsAndHashCode3 other = (EqualsAndHashCode3) o; + if (!other.canEqual((java.lang.Object) this)) return false; + return true; + } + @java.lang.SuppressWarnings("all") + protected boolean canEqual(final java.lang.Object other) { + return other instanceof EqualsAndHashCode3; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public int hashCode() { + if ($hashCodeCache != 0) return $hashCodeCache; + final int result = 1; + $hashCodeCache = result; + return result; + } +} +class EqualsAndHashCode4 extends EqualsAndHashCode { + @java.lang.Override + @java.lang.SuppressWarnings("all") + public boolean equals(final java.lang.Object o) { + if (o == this) return true; + if (!(o instanceof EqualsAndHashCode4)) return false; + final EqualsAndHashCode4 other = (EqualsAndHashCode4) o; + if (!other.canEqual((java.lang.Object) this)) return false; + if (!super.equals(o)) return false; + return true; + } + @java.lang.SuppressWarnings("all") + protected boolean canEqual(final java.lang.Object other) { + return other instanceof EqualsAndHashCode4; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public int hashCode() { + final int result = super.hashCode(); + return result; + } +} +class EqualsAndHashCode5 extends EqualsAndHashCode { + 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; + if (!other.canEqual((java.lang.Object) this)) return false; + if (!super.equals(o)) return false; + return true; + } + @java.lang.SuppressWarnings("all") + protected boolean canEqual(final java.lang.Object other) { + return other instanceof EqualsAndHashCode5; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public int hashCode() { + if ($hashCodeCache != 0) return $hashCodeCache; + final int result = super.hashCode(); + $hashCodeCache = result; + return result; + } +} diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java new file mode 100644 index 00000000..4eb4fbb3 --- /dev/null +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeCache.java @@ -0,0 +1,167 @@ +@lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode { + int x; + boolean[] y; + Object[] z; + String a; + String b; + EqualsAndHashCode() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCode))) + return false; + final EqualsAndHashCode other = (EqualsAndHashCode) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + if ((this.x != other.x)) + return false; + if ((! java.util.Arrays.equals(this.y, other.y))) + return false; + if ((! java.util.Arrays.deepEquals(this.z, other.z))) + return false; + final java.lang.Object this$a = this.a; + final java.lang.Object other$a = other.a; + if (((this$a == null) ? (other$a != null) : (! this$a.equals(other$a)))) + return false; + final java.lang.Object this$b = this.b; + final java.lang.Object other$b = other.b; + if (((this$b == null) ? (other$b != null) : (! this$b.equals(other$b)))) + return false; + return true; + } + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + return (other instanceof EqualsAndHashCode); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + final int PRIME = 59; + int result = 1; + result = ((result * PRIME) + this.x); + result = ((result * PRIME) + java.util.Arrays.hashCode(this.y)); + result = ((result * PRIME) + java.util.Arrays.deepHashCode(this.z)); + final java.lang.Object $a = this.a; + result = ((result * PRIME) + (($a == null) ? 43 : $a.hashCode())); + final java.lang.Object $b = this.b; + result = ((result * PRIME) + (($b == null) ? 43 : $b.hashCode())); + return result; + } +} +final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode2 { + int x; + long y; + float f; + double d; + boolean b; + private transient int $hashCodeCache = 0; + EqualsAndHashCode2() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCode2))) + return false; + final EqualsAndHashCode2 other = (EqualsAndHashCode2) o; + if ((this.x != other.x)) + return false; + if ((this.y != other.y)) + return false; + if ((java.lang.Float.compare(this.f, other.f) != 0)) + return false; + if ((java.lang.Double.compare(this.d, other.d) != 0)) + return false; + if ((this.b != other.b)) + return false; + return true; + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + if ($hashCodeCache != 0) + return $hashCodeCache; + final int PRIME = 59; + int result = 1; + result = ((result * PRIME) + this.x); + final long $y = this.y; + result = ((result * PRIME) + (int) ($y ^ ($y >>> 32))); + result = ((result * PRIME) + java.lang.Float.floatToIntBits(this.f)); + 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; + return result; + } +} +final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode { + EqualsAndHashCode3() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCode3))) + return false; + final EqualsAndHashCode3 other = (EqualsAndHashCode3) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + return true; + } + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + return (other instanceof EqualsAndHashCode3); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + final int result = 1; + return result; + } +} +@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode { + EqualsAndHashCode4() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCode4))) + return false; + final EqualsAndHashCode4 other = (EqualsAndHashCode4) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + if ((! super.equals(o))) + return false; + return true; + } + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + return (other instanceof EqualsAndHashCode4); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + final int result = super.hashCode(); + return result; + } +} +@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode { + private transient int $hashCodeCache = 0; + EqualsAndHashCode5() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCode5))) + return false; + final EqualsAndHashCode5 other = (EqualsAndHashCode5) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + if ((! super.equals(o))) + 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; + final int result = super.hashCode(); + $hashCodeCache = result; + return result; + } +} diff --git a/test/transform/resource/before/EqualsAndHashCodeCache.java b/test/transform/resource/before/EqualsAndHashCodeCache.java new file mode 100644 index 00000000..3c84ee59 --- /dev/null +++ b/test/transform/resource/before/EqualsAndHashCodeCache.java @@ -0,0 +1,29 @@ +@lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) +class EqualsAndHashCode { + int x; + boolean[] y; + Object[] z; + String a; + String b; +} + +@lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) +final class EqualsAndHashCode2 { + int x; + long y; + float f; + double d; + boolean b; +} + +@lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) +final class EqualsAndHashCode3 extends EqualsAndHashCode { +} + +@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) +class EqualsAndHashCode4 extends EqualsAndHashCode { +} + +@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) +final class EqualsAndHashCode5 extends EqualsAndHashCode { +} \ No newline at end of file -- cgit From d00ab552e199410c283049cd8d9359a69855f1f2 Mon Sep 17 00:00:00 2001 From: Andre Brait 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') 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 03d1119e08956f8940edb6fe600af16fb9f424a0 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 13 Jul 2020 18:56:32 +0200 Subject: Revert unnecessary changes --- src/core/lombok/EqualsAndHashCode.java | 121 +++++++++++++-------------------- 1 file changed, 49 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index 7f60880c..5191ee0c 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -27,21 +27,18 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Generates implementations for the {@code equals} and {@code hashCode} methods - * inherited by all objects, based on relevant fields. + * Generates implementations for the {@code equals} and {@code hashCode} methods inherited by all objects, based on relevant fields. *

- * Complete documentation is found at - * the project - * lombok features page for @EqualsAndHashCode. + * Complete documentation is found at the project lombok features page for @EqualsAndHashCode. */ -@Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface EqualsAndHashCode { +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.SOURCE) +public @interface EqualsAndHashCode { /** - * Any fields listed here will not be taken into account in the generated - * {@code equals} and {@code hashCode} implementations. Mutually exclusive - * with {@link #of()}. + * Any fields listed here will not be taken into account in the generated {@code equals} and {@code hashCode} implementations. + * Mutually exclusive with {@link #of()}. *

- * Will soon be marked {@code @Deprecated}; use the - * {@code @EqualsAndHashCode.Exclude} annotation instead. + * Will soon be marked {@code @Deprecated}; use the {@code @EqualsAndHashCode.Exclude} annotation instead. * * @return A list of fields to exclude. */ @@ -53,36 +50,30 @@ import java.lang.annotation.Target; *

* Mutually exclusive with {@link #exclude()}. *

- * Will soon be marked {@code @Deprecated}; use the - * {@code @EqualsAndHashCode.Include} annotation together with - * {@code @EqualsAndHashCode(onlyExplicitlyIncluded = true)}. + * Will soon be marked {@code @Deprecated}; use the {@code @EqualsAndHashCode.Include} annotation together with {@code @EqualsAndHashCode(onlyExplicitlyIncluded = true)}. * * @return A list of fields to use (default: all of them). */ String[] of() default {}; /** - * Call on the superclass's implementations of {@code equals} and - * {@code hashCode} before calculating for the fields in this class. + * Call on the superclass's implementations of {@code equals} and {@code hashCode} before calculating for the fields in this class. * default: false * - * @return Whether to call the superclass's {@code equals} implementation as - * part of the generated equals algorithm. + * @return Whether to call the superclass's {@code equals} implementation as part of the generated equals algorithm. */ boolean callSuper() default false; /** - * Normally, if getters are available, those are called. To suppress this - * and let the generated code use the fields directly, set this to - * {@code true}. default: false + * Normally, if getters are available, those are called. To suppress this and let the generated code use the fields directly, set this to {@code true}. + * default: false * - * @return If {@code true}, always use direct field access instead of - * calling the getter method. + * @return If {@code true}, always use direct field access instead of calling the getter method. */ boolean doNotUseGetters() default false; - + /** - * Determines how the result of the {@code hashCode} method will be cached. + * Determines how the result of the {@code hashCode} method will be cached. * default: {@link CacheStrategy#NEVER} * * @return The {@code hashCode} cache strategy to be used. @@ -90,80 +81,66 @@ import java.lang.annotation.Target; CacheStrategy cacheStrategy() default CacheStrategy.NEVER; /** - * Any annotations listed here are put on the generated parameter of - * {@code equals} and {@code canEqual}. This is useful to add for example a - * {@code Nullable} annotation.
- * The syntax for this feature depends on JDK version (nothing we can do - * about that; it's to work around javac bugs).
+ * Any annotations listed here are put on the generated parameter of {@code equals} and {@code canEqual}. + * This is useful to add for example a {@code Nullable} annotation.
+ * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).
* up to JDK7:
- * {@code @EqualsAndHashCode(onParam=@__({@AnnotationsGoHere}))}
+ * {@code @EqualsAndHashCode(onParam=@__({@AnnotationsGoHere}))}
* from JDK8:
- * {@code @EqualsAndHashCode(onParam_={@AnnotationsGohere})} // note the - * underscore after {@code onParam}. - * - * @return List of annotations to apply to the generated parameter in the - * {@code equals()} method. + * {@code @EqualsAndHashCode(onParam_={@AnnotationsGohere})} // note the underscore after {@code onParam}. + * + * @return List of annotations to apply to the generated parameter in the {@code equals()} method. */ AnyAnnotation[] onParam() default {}; /** - * Placeholder annotation to enable the placement of annotations on the - * generated code. - * + * Placeholder annotation to enable the placement of annotations on the generated code. * @deprecated Don't use this annotation, ever - Read the documentation. */ - @Deprecated @Retention(RetentionPolicy.SOURCE) @Target({}) - @interface AnyAnnotation { - } + @Deprecated + @Retention(RetentionPolicy.SOURCE) + @Target({}) + @interface AnyAnnotation {} /** - * Only include fields and methods explicitly marked with - * {@code @EqualsAndHashCode.Include}. Normally, all (non-static, - * non-transient) fields are included by default. + * Only include fields and methods explicitly marked with {@code @EqualsAndHashCode.Include}. + * Normally, all (non-static, non-transient) fields are included by default. * - * @return If {@code true}, don't include non-static non-transient fields - * automatically (default: {@code false}). + * @return If {@code true}, don't include non-static non-transient fields automatically (default: {@code false}). */ boolean onlyExplicitlyIncluded() default false; /** - * If present, do not include this field in the generated {@code equals} and - * {@code hashCode} methods. + * If present, do not include this field in the generated {@code equals} and {@code hashCode} methods. */ - @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interface Exclude { - } + @Target(ElementType.FIELD) + @Retention(RetentionPolicy.SOURCE) + public @interface Exclude {} /** - * Configure the behaviour of how this member is treated in the - * {@code equals} and {@code hashCode} implementation; if on a method, - * include the method's return value as part of calculating - * hashCode/equality. + * Configure the behaviour of how this member is treated in the {@code equals} and {@code hashCode} implementation; if on a method, include the method's return value as part of calculating hashCode/equality. */ - @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.SOURCE) public @interface Include { + @Target({ElementType.FIELD, ElementType.METHOD}) + @Retention(RetentionPolicy.SOURCE) + public @interface Include { /** - * Defaults to the method name of the annotated member. If on a method - * and the name equals the name of a default-included field, this member - * takes its place. + * Defaults to the method name of the annotated member. + * If on a method and the name equals the name of a default-included field, this member takes its place. * - * @return If present, this method serves as replacement for the named - * field. + * @return If present, this method serves as replacement for the named field. */ String replaces() default ""; - + /** - * Higher ranks are considered first. Members of the same rank are - * considered in the order they appear in the source file. + * Higher ranks are considered first. Members of the same rank are considered in the order they appear in the source file. * - * If not explicitly set, the {@code default} rank for primitives is - * 1000, and for primitive wrappers 800. + * If not explicitly set, the {@code default} rank for primitives is 1000, and for primitive wrappers 800. * - * @return ordering within the generating {@code equals} and - * {@code hashCode} methods; higher numbers are considered - * first. + * @return ordering within the generating {@code equals} and {@code hashCode} methods; higher numbers are considered first. */ int rank() default 0; } - + public enum CacheStrategy { /** * Never cache. Perform the calculation every time the method is called. @@ -171,9 +148,9 @@ import java.lang.annotation.Target; NEVER, /** * Cache the result of the first invocation of {@code hashCode} and use it for subsequent invocations. - * This can improve performance in if all fields used for calculating the {@code hashCode} are immutable + * This can improve performance in if all fields used for calculating the {@code hashCode} are immutable * and thus every invocation of {@code hashCode} will always return the same value. - * Do not use this if there's any chance that different invocations of {@code hashCode} + * Do not use this if there's any chance that different invocations of {@code hashCode} * might return different values. */ LAZY -- 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') 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') 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 b62ce8fb2fe5348e37da4025b7550f21876f6e28 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Mon, 13 Jul 2020 19:45:29 +0200 Subject: Fix typo --- src/core/lombok/EqualsAndHashCode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index 5191ee0c..279e6e3d 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -148,7 +148,7 @@ public @interface EqualsAndHashCode { NEVER, /** * Cache the result of the first invocation of {@code hashCode} and use it for subsequent invocations. - * This can improve performance in if all fields used for calculating the {@code hashCode} are immutable + * This can improve performance if all fields used for calculating the {@code hashCode} are immutable * and thus every invocation of {@code hashCode} will always return the same value. * Do not use this if there's any chance that different invocations of {@code hashCode} * might return different values. -- 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') 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') 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') 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') 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') 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 0808d4271a495e1b6ea4f9a636e63a1c62567133 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Tue, 21 Jul 2020 12:56:48 +0200 Subject: Remove CacheStrategy compilation unit for javac --- src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index c2da0274..58078038 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -85,6 +85,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler> members = InclusionExclusionUtils.handleEqualsAndHashCodeMarking(annotationNode.up(), annotation, annotationNode); JavacNode typeNode = annotationNode.up(); -- 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') 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') 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') 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