From 7bf68c61cd791439a58ba13386f014e4bae5a143 Mon Sep 17 00:00:00 2001 From: gafertp Date: Fri, 20 Nov 2015 21:26:39 +0100 Subject: [PATCH] Make equals symmetrical if one compares a supertype and a subtype with no unequal properties --- basic/.gitignore | 2 ++ .../jvnet/jaxb2_commons/plugin/equals/EqualsPlugin.java | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/basic/.gitignore b/basic/.gitignore index b83d2226..1b823e3d 100644 --- a/basic/.gitignore +++ b/basic/.gitignore @@ -1 +1,3 @@ /target/ +.idea +*.iml \ No newline at end of file diff --git a/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/equals/EqualsPlugin.java b/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/equals/EqualsPlugin.java index 4dbf44ca..e1595da9 100644 --- a/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/equals/EqualsPlugin.java +++ b/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/equals/EqualsPlugin.java @@ -24,7 +24,6 @@ import com.sun.codemodel.JBlock; import com.sun.codemodel.JCodeModel; -import com.sun.codemodel.JConditional; import com.sun.codemodel.JDefinedClass; import com.sun.codemodel.JExpr; import com.sun.codemodel.JExpression; @@ -183,9 +182,10 @@ protected void processClassOutline(ClassOutline classOutline) { final JVar equalsStrategy = equals.param(EqualsStrategy.class, "strategy"); - final JConditional ifNotInstanceof = body._if(JOp.not(object - ._instanceof(theClass))); - ifNotInstanceof._then()._return(JExpr.FALSE); + JExpression objectIsNull = object.eq(JExpr._null()); + JExpression notTheSameType = JExpr.invoke("getClass").ne(object.invoke("getClass")); + body._if(JOp.cor(objectIsNull, notTheSameType)) + ._then()._return(JExpr.FALSE); // body._if(JExpr._this().eq(object))._then()._return(JExpr.TRUE);