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 --- src/core/lombok/EqualsAndHashCode.java | 139 +++++++++++++-------- .../eclipse/handlers/HandleEqualsAndHashCode.java | 77 +++++++++++- .../javac/handlers/HandleEqualsAndHashCode.java | 38 +++--- 3 files changed, 183 insertions(+), 71 deletions(-) (limited to 'src') 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