diff options
42 files changed, 160 insertions, 81 deletions
diff --git a/doc/changelog.markdown b/doc/changelog.markdown index cf2d286b..d2fd6bcd 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,6 +2,7 @@ Lombok Changelog ---------------- ### v1.12.7 "Edgy Guinea Pig" +* CHANGE: The `canEqual` method generated by `@EqualsAndHashCode`, `@Value` and `@Data` is now `protected` instead of `public`. [Issue #660](https://code.google.com/p/projectlombok/issues/detail?id=660) * BUGFIX: Deadlocks would occasionally occur in eclipse when using lazy getters [Issue #590](https://code.google.com/p/projectlombok/issues/detail?id=590) ### v1.12.6 (March 6th, 2014) diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 2ae7aba4..8b4d14ed 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -212,21 +212,20 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH MemberExistsResult equalsExists = methodExists("equals", typeNode, 1); MemberExistsResult hashCodeExists = methodExists("hashCode", typeNode, 0); MemberExistsResult canEqualExists = methodExists("canEqual", typeNode, 1); - switch (Collections.max(Arrays.asList(equalsExists, hashCodeExists, canEqualExists))) { + switch (Collections.max(Arrays.asList(equalsExists, hashCodeExists))) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: if (whineIfExists) { - String msg = String.format("Not generating equals%s: A method with one of those names already exists. (Either all or none of these methods will be generated).", needsCanEqual ? ", hashCode and canEquals" : " and hashCode"); + String msg = "Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated)."; errorNode.addWarning(msg); } else if (equalsExists == MemberExistsResult.NOT_EXISTS || hashCodeExists == MemberExistsResult.NOT_EXISTS) { - // This means equals OR hashCode exists and not both (or neither, but canEqual is there). + // This means equals OR hashCode exists and not both. // Even though we should suppress the message about not generating these, this is such a weird and surprising situation we should ALWAYS generate a warning. - // The user code couldn't possibly (barring really weird subclassing shenanigans) be in a shippable state anyway; the implementations of these 3 methods are + // The user code couldn't possibly (barring really weird subclassing shenanigans) be in a shippable state anyway; the implementations of these 2 methods are // all inter-related and should be written by the same entity. - String msg = String.format("Not generating %s: One of equals, hashCode, and canEqual exists. " + - "You should either write all of these or none of these (in the latter case, lombok generates them).", - equalsExists == MemberExistsResult.NOT_EXISTS && hashCodeExists == MemberExistsResult.NOT_EXISTS ? "equals and hashCode" : + String msg = String.format("Not generating %s: One of equals or hashCode exists. " + + "You should either write both of these or none of these (in the latter case, lombok generates them).", equalsExists == MemberExistsResult.NOT_EXISTS ? "equals" : "hashCode"); errorNode.addWarning(msg); } @@ -240,7 +239,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH equalsMethod.traverse(new SetGeneratedByVisitor(errorNode.get()), ((TypeDeclaration)typeNode.get()).scope); injectMethod(typeNode, equalsMethod); - if (needsCanEqual) { + if (needsCanEqual && canEqualExists == MemberExistsResult.NOT_EXISTS) { MethodDeclaration canEqualMethod = createCanEqual(typeNode, errorNode.get()); canEqualMethod.traverse(new SetGeneratedByVisitor(errorNode.get()), ((TypeDeclaration)typeNode.get()).scope); injectMethod(typeNode, canEqualMethod); @@ -734,7 +733,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source); - method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); + method.modifiers = toEclipseModifier(AccessLevel.PROTECTED); method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0); method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; setGeneratedBy(method.returnType, source); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 2c998f48..05244f3b 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -187,21 +187,20 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas MemberExistsResult equalsExists = methodExists("equals", typeNode, 1); MemberExistsResult hashCodeExists = methodExists("hashCode", typeNode, 0); MemberExistsResult canEqualExists = methodExists("canEqual", typeNode, 1); - switch (Collections.max(Arrays.asList(equalsExists, hashCodeExists, canEqualExists))) { + switch (Collections.max(Arrays.asList(equalsExists, hashCodeExists))) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: if (whineIfExists) { - String msg = String.format("Not generating equals%s: A method with one of those names already exists. (Either all or none of these methods will be generated).", needsCanEqual ? ", hashCode and canEquals" : " and hashCode"); + String msg = "Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated)."; source.addWarning(msg); } else if (equalsExists == MemberExistsResult.NOT_EXISTS || hashCodeExists == MemberExistsResult.NOT_EXISTS) { - // This means equals OR hashCode exists and not both (or neither, but canEqual is there). + // This means equals OR hashCode exists and not both. // Even though we should suppress the message about not generating these, this is such a weird and surprising situation we should ALWAYS generate a warning. - // The user code couldn't possibly (barring really weird subclassing shenanigans) be in a shippable state anyway; the implementations of these 3 methods are + // The user code couldn't possibly (barring really weird subclassing shenanigans) be in a shippable state anyway; the implementations of these 2 methods are // all inter-related and should be written by the same entity. - String msg = String.format("Not generating %s: One of equals, hashCode, and canEqual exists. " + - "You should either write all of these or none of these (in the latter case, lombok generates them).", - equalsExists == MemberExistsResult.NOT_EXISTS && hashCodeExists == MemberExistsResult.NOT_EXISTS ? "equals and hashCode" : + String msg = String.format("Not generating %s: One of equals or hashCode exists. " + + "You should either write both of these or none of these (in the latter case, lombok generates them).", equalsExists == MemberExistsResult.NOT_EXISTS ? "equals" : "hashCode"); source.addWarning(msg); } @@ -214,7 +213,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas JCMethodDecl equalsMethod = createEquals(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, needsCanEqual, source.get()); injectMethod(typeNode, equalsMethod); - if (needsCanEqual) { + if (needsCanEqual && canEqualExists == MemberExistsResult.NOT_EXISTS) { JCMethodDecl canEqualMethod = createCanEqual(typeNode, source.get()); injectMethod(typeNode, canEqualMethod); } @@ -505,7 +504,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas */ JavacTreeMaker maker = typeNode.getTreeMaker(); - JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.<JCAnnotation>nil()); + JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); diff --git a/test/transform/resource/after-delombok/Accessors.java b/test/transform/resource/after-delombok/Accessors.java index 8b984236..0d15fe43 100644 --- a/test/transform/resource/after-delombok/Accessors.java +++ b/test/transform/resource/after-delombok/Accessors.java @@ -78,7 +78,7 @@ class AccessorsPrefix3 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof AccessorsPrefix3; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/ConflictingStaticConstructorNames.java b/test/transform/resource/after-delombok/ConflictingStaticConstructorNames.java index 172ed298..ae0c46bc 100644 --- a/test/transform/resource/after-delombok/ConflictingStaticConstructorNames.java +++ b/test/transform/resource/after-delombok/ConflictingStaticConstructorNames.java @@ -9,7 +9,7 @@ class ConflictingStaticConstructorNames { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof ConflictingStaticConstructorNames; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataExtended.java b/test/transform/resource/after-delombok/DataExtended.java index 07aa1f7e..6a5e7fa2 100644 --- a/test/transform/resource/after-delombok/DataExtended.java +++ b/test/transform/resource/after-delombok/DataExtended.java @@ -22,7 +22,7 @@ class DataExtended { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof DataExtended; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataIgnore.java b/test/transform/resource/after-delombok/DataIgnore.java index fa729ada..922de01b 100644 --- a/test/transform/resource/after-delombok/DataIgnore.java +++ b/test/transform/resource/after-delombok/DataIgnore.java @@ -21,7 +21,7 @@ class DataIgnore { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof DataIgnore; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataOnLocalClass.java b/test/transform/resource/after-delombok/DataOnLocalClass.java index bbd9ae4b..5d73e584 100644 --- a/test/transform/resource/after-delombok/DataOnLocalClass.java +++ b/test/transform/resource/after-delombok/DataOnLocalClass.java @@ -33,7 +33,7 @@ class DataOnLocalClass1 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Local; } @java.lang.Override @@ -93,7 +93,7 @@ class DataOnLocalClass2 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Local.InnerLocal; } @java.lang.Override @@ -130,7 +130,7 @@ class DataOnLocalClass2 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Local; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataPlain.java b/test/transform/resource/after-delombok/DataPlain.java index 6d536c8a..8988dc6f 100644 --- a/test/transform/resource/after-delombok/DataPlain.java +++ b/test/transform/resource/after-delombok/DataPlain.java @@ -32,7 +32,7 @@ class Data1 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Data1; } @java.lang.Override @@ -85,7 +85,7 @@ class Data2 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Data2; } @java.lang.Override @@ -181,7 +181,7 @@ final class Data4 extends java.util.Timer { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Data4; } @java.lang.Override @@ -208,7 +208,7 @@ class Data5 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Data5; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataWithGetter.java b/test/transform/resource/after-delombok/DataWithGetter.java index b429fe53..91f392ec 100644 --- a/test/transform/resource/after-delombok/DataWithGetter.java +++ b/test/transform/resource/after-delombok/DataWithGetter.java @@ -30,7 +30,7 @@ class DataWithGetter { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof DataWithGetter; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/DataWithGetterNone.java b/test/transform/resource/after-delombok/DataWithGetterNone.java index b063b4ff..5986ee9f 100644 --- a/test/transform/resource/after-delombok/DataWithGetterNone.java +++ b/test/transform/resource/after-delombok/DataWithGetterNone.java @@ -30,7 +30,7 @@ class DataWithGetterNone { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof DataWithGetterNone; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/EqualsAndHashCode.java b/test/transform/resource/after-delombok/EqualsAndHashCode.java index afe03dea..02ca2c42 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCode.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCode.java @@ -23,7 +23,7 @@ class EqualsAndHashCode { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof EqualsAndHashCode; } @java.lang.Override @@ -86,7 +86,7 @@ final class EqualsAndHashCode3 extends EqualsAndHashCode { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof EqualsAndHashCode3; } @java.lang.Override @@ -108,7 +108,7 @@ class EqualsAndHashCode4 extends EqualsAndHashCode { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof EqualsAndHashCode4; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java b/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java index 93b20e25..ac189f42 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCodeWithExistingMethods.java @@ -12,7 +12,27 @@ final class EqualsAndHashCodeWithExistingMethods2 { } final class EqualsAndHashCodeWithExistingMethods3 extends EqualsAndHashCodeWithExistingMethods { int x; - public boolean canEqual(Object other) { + private boolean canEqual(Object other) { return true; } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public boolean equals(final java.lang.Object o) { + if (o == this) return true; + if (!(o instanceof EqualsAndHashCodeWithExistingMethods3)) return false; + final EqualsAndHashCodeWithExistingMethods3 other = (EqualsAndHashCodeWithExistingMethods3)o; + if (!other.canEqual((java.lang.Object)this)) return false; + if (!super.equals(o)) return false; + if (this.x != other.x) return false; + return true; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public int hashCode() { + final int PRIME = 59; + int result = 1; + result = result * PRIME + super.hashCode(); + result = result * PRIME + this.x; + return result; + } }
\ No newline at end of file diff --git a/test/transform/resource/after-delombok/EqualsAndHashCodeWithSomeExistingMethods.java b/test/transform/resource/after-delombok/EqualsAndHashCodeWithSomeExistingMethods.java index aa9b984f..37eeb8df 100644 --- a/test/transform/resource/after-delombok/EqualsAndHashCodeWithSomeExistingMethods.java +++ b/test/transform/resource/after-delombok/EqualsAndHashCodeWithSomeExistingMethods.java @@ -17,7 +17,7 @@ class EqualsAndHashCodeWithSomeExistingMethods { } class EqualsAndHashCodeWithSomeExistingMethods2 { int x; - public boolean canEqual(Object other) { + protected boolean canEqual(Object other) { return false; } @java.lang.SuppressWarnings("all") @@ -25,6 +25,24 @@ class EqualsAndHashCodeWithSomeExistingMethods2 { } @java.lang.Override @java.lang.SuppressWarnings("all") + public boolean equals(final java.lang.Object o) { + if (o == this) return true; + if (!(o instanceof EqualsAndHashCodeWithSomeExistingMethods2)) return false; + final EqualsAndHashCodeWithSomeExistingMethods2 other = (EqualsAndHashCodeWithSomeExistingMethods2)o; + if (!other.canEqual((java.lang.Object)this)) return false; + if (this.x != other.x) return false; + return true; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public int hashCode() { + final int PRIME = 59; + int result = 1; + result = result * PRIME + this.x; + return result; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") public java.lang.String toString() { return "EqualsAndHashCodeWithSomeExistingMethods2(x=" + this.x + ")"; } @@ -63,7 +81,7 @@ class EqualsAndHashCodeWithNoExistingMethods { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof EqualsAndHashCodeWithNoExistingMethods; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/GetterLazyBoolean.java b/test/transform/resource/after-delombok/GetterLazyBoolean.java index eb6662e2..f684fc07 100644 --- a/test/transform/resource/after-delombok/GetterLazyBoolean.java +++ b/test/transform/resource/after-delombok/GetterLazyBoolean.java @@ -15,7 +15,7 @@ class GetterLazyBoolean { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof GetterLazyBoolean; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/GetterLazyEahcToString.java b/test/transform/resource/after-delombok/GetterLazyEahcToString.java index 3e98a25e..65c63fba 100644 --- a/test/transform/resource/after-delombok/GetterLazyEahcToString.java +++ b/test/transform/resource/after-delombok/GetterLazyEahcToString.java @@ -20,7 +20,7 @@ class GetterLazyEahcToString { } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof GetterLazyEahcToString; } diff --git a/test/transform/resource/after-delombok/GetterSetterJavadoc.java b/test/transform/resource/after-delombok/GetterSetterJavadoc.java index 743a3aac..7a782434 100644 --- a/test/transform/resource/after-delombok/GetterSetterJavadoc.java +++ b/test/transform/resource/after-delombok/GetterSetterJavadoc.java @@ -35,7 +35,7 @@ class GetterSetterJavadoc1 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof GetterSetterJavadoc1; } @java.lang.Override diff --git a/test/transform/resource/after-delombok/ValuePlain.java b/test/transform/resource/after-delombok/ValuePlain.java index e880454b..64e5b9b9 100644 --- a/test/transform/resource/after-delombok/ValuePlain.java +++ b/test/transform/resource/after-delombok/ValuePlain.java @@ -74,7 +74,7 @@ class Value2 { return true; } @java.lang.SuppressWarnings("all") - public boolean canEqual(final java.lang.Object other) { + protected boolean canEqual(final java.lang.Object other) { return other instanceof Value2; } @java.lang.Override diff --git a/test/transform/resource/after-ecj/Accessors.java b/test/transform/resource/after-ecj/Accessors.java index c8fdc3bf..6b8976d0 100644 --- a/test/transform/resource/after-ecj/Accessors.java +++ b/test/transform/resource/after-ecj/Accessors.java @@ -86,7 +86,7 @@ class AccessorsChain { return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof AccessorsPrefix3); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/ConflictingStaticConstructorNames.java b/test/transform/resource/after-ecj/ConflictingStaticConstructorNames.java index 8da11258..f8aff69c 100644 --- a/test/transform/resource/after-ecj/ConflictingStaticConstructorNames.java +++ b/test/transform/resource/after-ecj/ConflictingStaticConstructorNames.java @@ -9,7 +9,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof ConflictingStaticConstructorNames); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataExtended.java b/test/transform/resource/after-ecj/DataExtended.java index bcfd26e3..c2652ba4 100644 --- a/test/transform/resource/after-ecj/DataExtended.java +++ b/test/transform/resource/after-ecj/DataExtended.java @@ -18,7 +18,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof DataExtended); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataIgnore.java b/test/transform/resource/after-ecj/DataIgnore.java index d0e72e15..d077a5db 100644 --- a/test/transform/resource/after-ecj/DataIgnore.java +++ b/test/transform/resource/after-ecj/DataIgnore.java @@ -16,7 +16,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof DataIgnore); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataOnLocalClass.java b/test/transform/resource/after-ecj/DataOnLocalClass.java index 511291c2..3f4832ff 100644 --- a/test/transform/resource/after-ecj/DataOnLocalClass.java +++ b/test/transform/resource/after-ecj/DataOnLocalClass.java @@ -32,7 +32,7 @@ class DataOnLocalClass1 { return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Local); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { @@ -82,7 +82,7 @@ class DataOnLocalClass2 { return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Local.InnerLocal); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { @@ -120,7 +120,7 @@ class DataOnLocalClass2 { return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Local); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataPlain.java b/test/transform/resource/after-ecj/DataPlain.java index aa47fdaa..858f46dd 100644 --- a/test/transform/resource/after-ecj/DataPlain.java +++ b/test/transform/resource/after-ecj/DataPlain.java @@ -27,7 +27,7 @@ import lombok.Data; return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Data1); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { @@ -74,7 +74,7 @@ import lombok.Data; return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Data2); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { @@ -163,7 +163,7 @@ final @Data @lombok.EqualsAndHashCode(callSuper = true) class Data4 extends java return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Data4); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { @@ -185,7 +185,7 @@ final @Data @lombok.EqualsAndHashCode(callSuper = true) class Data4 extends java return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Data5); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataWithGetter.java b/test/transform/resource/after-ecj/DataWithGetter.java index 2ce74ceb..ee74ed4e 100644 --- a/test/transform/resource/after-ecj/DataWithGetter.java +++ b/test/transform/resource/after-ecj/DataWithGetter.java @@ -26,7 +26,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof DataWithGetter); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/DataWithGetterNone.java b/test/transform/resource/after-ecj/DataWithGetterNone.java index 087d0762..056e071d 100644 --- a/test/transform/resource/after-ecj/DataWithGetterNone.java +++ b/test/transform/resource/after-ecj/DataWithGetterNone.java @@ -26,7 +26,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof DataWithGetterNone); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/EqualsAndHashCode.java b/test/transform/resource/after-ecj/EqualsAndHashCode.java index 625a0f87..05cbf2a3 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCode.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCode.java @@ -31,7 +31,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + 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() { @@ -101,7 +101,7 @@ final @lombok.EqualsAndHashCode(callSuper = false) class EqualsAndHashCode3 exte return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + 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() { @@ -125,7 +125,7 @@ final @lombok.EqualsAndHashCode(callSuper = false) class EqualsAndHashCode3 exte return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + 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() { diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java b/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java index 50b219f1..feda33ce 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeWithExistingMethods.java @@ -21,7 +21,28 @@ final @lombok.EqualsAndHashCode(callSuper = true) class EqualsAndHashCodeWithExi EqualsAndHashCodeWithExistingMethods3() { super(); } - public boolean canEqual(Object other) { + private boolean canEqual(Object other) { return true; } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCodeWithExistingMethods3))) + return false; + final @java.lang.SuppressWarnings("all") EqualsAndHashCodeWithExistingMethods3 other = (EqualsAndHashCodeWithExistingMethods3) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + if ((! super.equals(o))) + return false; + if ((this.x != other.x)) + return false; + return true; + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + final int PRIME = 59; + int result = 1; + result = ((result * PRIME) + super.hashCode()); + result = ((result * PRIME) + this.x); + return result; + } }
\ No newline at end of file diff --git a/test/transform/resource/after-ecj/EqualsAndHashCodeWithSomeExistingMethods.java b/test/transform/resource/after-ecj/EqualsAndHashCodeWithSomeExistingMethods.java index 05f7ad97..ee846c64 100644 --- a/test/transform/resource/after-ecj/EqualsAndHashCodeWithSomeExistingMethods.java +++ b/test/transform/resource/after-ecj/EqualsAndHashCodeWithSomeExistingMethods.java @@ -14,9 +14,27 @@ import static lombok.AccessLevel.NONE; } @Data @Getter(NONE) @Setter(NONE) class EqualsAndHashCodeWithSomeExistingMethods2 { int x; - public boolean canEqual(Object other) { + protected boolean canEqual(Object other) { return false; } + public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) { + if ((o == this)) + return true; + if ((! (o instanceof EqualsAndHashCodeWithSomeExistingMethods2))) + return false; + final @java.lang.SuppressWarnings("all") EqualsAndHashCodeWithSomeExistingMethods2 other = (EqualsAndHashCodeWithSomeExistingMethods2) o; + if ((! other.canEqual((java.lang.Object) this))) + return false; + if ((this.x != other.x)) + return false; + return true; + } + public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { + final int PRIME = 59; + int result = 1; + result = ((result * PRIME) + this.x); + return result; + } public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { return (("EqualsAndHashCodeWithSomeExistingMethods2(x=" + this.x) + ")"); } @@ -53,7 +71,7 @@ import static lombok.AccessLevel.NONE; return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof EqualsAndHashCodeWithNoExistingMethods); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/GetterLazyBoolean.java b/test/transform/resource/after-ecj/GetterLazyBoolean.java index c37eece2..cd5bac20 100644 --- a/test/transform/resource/after-ecj/GetterLazyBoolean.java +++ b/test/transform/resource/after-ecj/GetterLazyBoolean.java @@ -53,7 +53,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof GetterLazyBoolean); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/GetterLazyEahcToString.java b/test/transform/resource/after-ecj/GetterLazyEahcToString.java index da9f6a83..df26f912 100644 --- a/test/transform/resource/after-ecj/GetterLazyEahcToString.java +++ b/test/transform/resource/after-ecj/GetterLazyEahcToString.java @@ -42,7 +42,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof GetterLazyEahcToString); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/GetterSetterJavadoc.java b/test/transform/resource/after-ecj/GetterSetterJavadoc.java index b7fc15b8..9f3bb9bd 100644 --- a/test/transform/resource/after-ecj/GetterSetterJavadoc.java +++ b/test/transform/resource/after-ecj/GetterSetterJavadoc.java @@ -18,7 +18,7 @@ return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof GetterSetterJavadoc1); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/after-ecj/ValuePlain.java b/test/transform/resource/after-ecj/ValuePlain.java index 5ca32af8..8162ab93 100644 --- a/test/transform/resource/after-ecj/ValuePlain.java +++ b/test/transform/resource/after-ecj/ValuePlain.java @@ -64,7 +64,7 @@ final @lombok.Value class Value1 { return false; return true; } - public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { + protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) { return (other instanceof Value2); } public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() { diff --git a/test/transform/resource/before/EqualsAndHashCodeWithExistingMethods.java b/test/transform/resource/before/EqualsAndHashCodeWithExistingMethods.java index be90d871..e21b82d9 100644 --- a/test/transform/resource/before/EqualsAndHashCodeWithExistingMethods.java +++ b/test/transform/resource/before/EqualsAndHashCodeWithExistingMethods.java @@ -20,7 +20,7 @@ final class EqualsAndHashCodeWithExistingMethods2 { final class EqualsAndHashCodeWithExistingMethods3 extends EqualsAndHashCodeWithExistingMethods { int x; - public boolean canEqual(Object other) { + private boolean canEqual(Object other) { return true; } }
\ No newline at end of file diff --git a/test/transform/resource/before/EqualsAndHashCodeWithSomeExistingMethods.java b/test/transform/resource/before/EqualsAndHashCodeWithSomeExistingMethods.java index 784e3b3f..debe75b9 100644 --- a/test/transform/resource/before/EqualsAndHashCodeWithSomeExistingMethods.java +++ b/test/transform/resource/before/EqualsAndHashCodeWithSomeExistingMethods.java @@ -18,7 +18,7 @@ class EqualsAndHashCodeWithSomeExistingMethods { class EqualsAndHashCodeWithSomeExistingMethods2 { int x; - public boolean canEqual(Object other) { + protected boolean canEqual(Object other) { return false; } } diff --git a/test/transform/resource/messages-delombok/EqualsAndHashCodeWithExistingMethods.java.messages b/test/transform/resource/messages-delombok/EqualsAndHashCodeWithExistingMethods.java.messages index b3490fd0..6f42cd7d 100644 --- a/test/transform/resource/messages-delombok/EqualsAndHashCodeWithExistingMethods.java.messages +++ b/test/transform/resource/messages-delombok/EqualsAndHashCodeWithExistingMethods.java.messages @@ -1,3 +1,2 @@ -1 Not generating equals, hashCode and canEquals: A method with one of those names already exists. (Either all or none of these methods will be generated). -10 Not generating equals and hashCode: A method with one of those names already exists. (Either all or none of these methods will be generated). -19 Not generating equals, hashCode and canEquals: A method with one of those names already exists. (Either all or none of these methods will be generated). +1 Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated). +10 Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated). diff --git a/test/transform/resource/messages-delombok/EqualsAndHashCodeWithSomeExistingMethods.java.messages b/test/transform/resource/messages-delombok/EqualsAndHashCodeWithSomeExistingMethods.java.messages index 75ddfa54..d3119bd6 100644 --- a/test/transform/resource/messages-delombok/EqualsAndHashCodeWithSomeExistingMethods.java.messages +++ b/test/transform/resource/messages-delombok/EqualsAndHashCodeWithSomeExistingMethods.java.messages @@ -1,2 +1 @@ -4 Not generating equals: One of equals, hashCode, and canEqual exists. You should either write all of these or none of these (in the latter case, lombok generates them). -15 Not generating equals and hashCode: One of equals, hashCode, and canEqual exists. You should either write all of these or none of these (in the latter case, lombok generates them). +4 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them). diff --git a/test/transform/resource/messages-ecj/EqualsAndHashCodeWithExistingMethods.java.messages b/test/transform/resource/messages-ecj/EqualsAndHashCodeWithExistingMethods.java.messages index b3490fd0..6f42cd7d 100644 --- a/test/transform/resource/messages-ecj/EqualsAndHashCodeWithExistingMethods.java.messages +++ b/test/transform/resource/messages-ecj/EqualsAndHashCodeWithExistingMethods.java.messages @@ -1,3 +1,2 @@ -1 Not generating equals, hashCode and canEquals: A method with one of those names already exists. (Either all or none of these methods will be generated). -10 Not generating equals and hashCode: A method with one of those names already exists. (Either all or none of these methods will be generated). -19 Not generating equals, hashCode and canEquals: A method with one of those names already exists. (Either all or none of these methods will be generated). +1 Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated). +10 Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated). diff --git a/test/transform/resource/messages-ecj/EqualsAndHashCodeWithSomeExistingMethods.java.messages b/test/transform/resource/messages-ecj/EqualsAndHashCodeWithSomeExistingMethods.java.messages index 75ddfa54..d3119bd6 100644 --- a/test/transform/resource/messages-ecj/EqualsAndHashCodeWithSomeExistingMethods.java.messages +++ b/test/transform/resource/messages-ecj/EqualsAndHashCodeWithSomeExistingMethods.java.messages @@ -1,2 +1 @@ -4 Not generating equals: One of equals, hashCode, and canEqual exists. You should either write all of these or none of these (in the latter case, lombok generates them). -15 Not generating equals and hashCode: One of equals, hashCode, and canEqual exists. You should either write all of these or none of these (in the latter case, lombok generates them). +4 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them). diff --git a/usage_examples/DataExample_post.jpage b/usage_examples/DataExample_post.jpage index 29e7e328..e53e999c 100644 --- a/usage_examples/DataExample_post.jpage +++ b/usage_examples/DataExample_post.jpage @@ -42,6 +42,10 @@ public class DataExample { return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")"; } + protected boolean canEqual(Object other) { + return other instanceof DataExample; + } + @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof DataExample)) return false; @@ -90,6 +94,10 @@ public class DataExample { return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")"; } + protected boolean canEqual(Object other) { + return other instanceof Exercise; + } + @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Exercise)) return false; diff --git a/usage_examples/EqualsAndHashCodeExample_post.jpage b/usage_examples/EqualsAndHashCodeExample_post.jpage index aa6a44ba..f58e045b 100644 --- a/usage_examples/EqualsAndHashCodeExample_post.jpage +++ b/usage_examples/EqualsAndHashCodeExample_post.jpage @@ -33,7 +33,7 @@ public class EqualsAndHashCodeExample { return result; } - public boolean canEqual(Object other) { + protected boolean canEqual(Object other) { return other instanceof EqualsAndHashCodeExample; } @@ -65,7 +65,7 @@ public class EqualsAndHashCodeExample { return result; } - public boolean canEqual(Object other) { + protected boolean canEqual(Object other) { return other instanceof Square; } } diff --git a/website/features/EqualsAndHashCode.html b/website/features/EqualsAndHashCode.html index 2ef88676..6b2507c6 100644 --- a/website/features/EqualsAndHashCode.html +++ b/website/features/EqualsAndHashCode.html @@ -65,9 +65,8 @@ return false. This is analogous to <code>java.lang.Double</code>'s equals method, and is in fact required to ensure that comparing an object to an exact copy of itself returns <code>true</code> for equality. </p><p> - If there is <em>any</em> method named either <code>hashCode</code>, <code>equals</code> or <code>canEqual</code>, regardless of - parameters or return type, no methods will be generated, and a warning is emitted instead. These 3 methods need to be in sync with - each other, which lombok cannot guarantee unless it generates all the methods, hence you always get a warning if one <em>or</em> more + If there is <em>any</em> method named either <code>hashCode</code> or <code>equals</code>, regardless of return type, no methods will be generated, and a warning is emitted instead. These 2 methods need to be in sync with + each other, which lombok cannot guarantee unless it generates all the methods, hence you always get a warning if one <em>or</em> both of the methods already exist. </p><p> Attempting to exclude fields that don't exist or would have been excluded anyway (because they are static or transient) results in warnings on the named fields. |