diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-12-31 01:09:07 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-12-31 10:12:24 +0100 |
commit | 49100b43c084f6774d43d93eee98440253965047 (patch) | |
tree | 4e16b1d0f1407bc6cfdcd93d3c569380553fa663 /src | |
parent | 52582dc10ea1b5ea4a1dbb71831a442056354990 (diff) | |
download | lombok-49100b43c084f6774d43d93eee98440253965047.tar.gz lombok-49100b43c084f6774d43d93eee98440253965047.tar.bz2 lombok-49100b43c084f6774d43d93eee98440253965047.zip |
calls to canEqual and equals are now fored to go to the version that has 1 Object as parameter (creating new equals methods by giving them non-object parameterized is a _really_ bad idea, but if someone did do that, obviously lombok shouldn't call those!)
Fixes #165.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 15 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 14 |
2 files changed, 20 insertions, 9 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index d906d85d..adcb8e6a 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -536,7 +536,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA } } - /* if (!other.canEqual(this)) return false; */ { + /* if (!other.canEqual((java.lang.Object) this)) return false; */ { if (needsCanEqual) { MessageSend otherCanEqual = new MessageSend(); otherCanEqual.sourceStart = pS; otherCanEqual.sourceEnd = pE; @@ -547,8 +547,11 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA ThisReference thisReference = new ThisReference(pS, pE); Eclipse.setGeneratedBy(thisReference, source); + CastExpression castThisRef = new CastExpression(thisReference, generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_OBJECT)); + Eclipse.setGeneratedBy(castThisRef, source); + castThisRef.sourceStart = pS; castThisRef.sourceEnd = pE; - otherCanEqual.arguments = new Expression[] {thisReference}; + otherCanEqual.arguments = new Expression[] {castThisRef}; Expression notOtherCanEqual = new UnaryExpression(otherCanEqual, OperatorIds.NOT); Eclipse.setGeneratedBy(notOtherCanEqual, source); @@ -621,7 +624,11 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA Eclipse.setGeneratedBy(equalsCall, source); equalsCall.receiver = createFieldAccessor(field, fieldAccess, source); equalsCall.selector = "equals".toCharArray(); - equalsCall.arguments = new Expression[] { createFieldAccessor(field, fieldAccess, source, otherName) }; + Expression equalsArg = createFieldAccessor(field, fieldAccess, source, otherName); + CastExpression castEqualsArg = new CastExpression(equalsArg, generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_OBJECT)); + Eclipse.setGeneratedBy(castEqualsArg, source); + castEqualsArg.sourceStart = pS; castEqualsArg.sourceEnd = pE; + equalsCall.arguments = new Expression[] { castEqualsArg }; UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT); fieldsNotEqual.sourceStart = pS; fieldsNotEqual.sourceEnd = pE; Eclipse.setGeneratedBy(fieldsNotEqual, source); @@ -673,7 +680,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA private MethodDeclaration createCanEqual(EclipseNode type, ASTNode source) { - /* public boolean canEquals(final java.lang.Object other) { + /* public boolean canEqual(final java.lang.Object other) { * return other instanceof MyType; * } */ diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 165f2041..78ecbd9c 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -199,7 +199,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd case EXISTS_BY_USER: default: break; - } + } } switch (methodExists("hashCode", typeNode)) { case NOT_EXISTS: @@ -381,12 +381,14 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd } } - /* if (!other.canEqual(this)) return false; */ { + /* if (!other.canEqual((java.lang.Object) this)) return false; */ { if (needsCanEqual) { List<JCExpression> exprNil = List.nil(); + JCExpression thisRef = maker.Ident(thisName); + JCExpression castThisRef = maker.TypeCast(chainDots(maker, typeNode, "java", "lang", "Object"), thisRef); JCExpression equalityCheck = maker.Apply(exprNil, maker.Select(maker.Ident(otherName), typeNode.toName("canEqual")), - List.<JCExpression>of(maker.Ident(thisName))); + List.of(castThisRef)); statements.append(maker.If(maker.Unary(Javac.getCTCint(JCTree.class, "NOT"), equalityCheck), returnBool(maker, false), null)); } } @@ -434,9 +436,11 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd /* if (this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName)) return false; */ JCExpression thisEqualsNull = maker.Binary(Javac.getCTCint(JCTree.class, "EQ"), thisFieldAccessor, maker.Literal(Javac.getCTCint(TypeTags.class, "BOT"), null)); JCExpression otherNotEqualsNull = maker.Binary(Javac.getCTCint(JCTree.class, "NE"), otherFieldAccessor, maker.Literal(Javac.getCTCint(TypeTags.class, "BOT"), null)); + JCExpression equalsArg = createFieldAccessor(maker, fieldNode, fieldAccess, maker.Ident(otherName)); + JCExpression castEqualsArg = maker.TypeCast(chainDots(maker, typeNode, "java", "lang", "Object"), equalsArg); JCExpression thisEqualsThat = maker.Apply(List.<JCExpression>nil(), maker.Select(createFieldAccessor(maker, fieldNode, fieldAccess), typeNode.toName("equals")), - List.of(createFieldAccessor(maker, fieldNode, fieldAccess, maker.Ident(otherName)))); + List.of(castEqualsArg)); JCExpression fieldsAreNotEqual = maker.Conditional(thisEqualsNull, otherNotEqualsNull, maker.Unary(Javac.getCTCint(JCTree.class, "NOT"), thisEqualsThat)); statements.append(maker.If(fieldsAreNotEqual, returnBool(maker, false), null)); } @@ -451,7 +455,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd } private JCMethodDecl createCanEqual(JavacNode typeNode) { - /* public boolean canEquals(final java.lang.Object other) { + /* public boolean canEqual(final java.lang.Object other) { * return other instanceof MyType; * } */ |