From e4efca6804f184f115e469faf74a608d3604604e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerardo=20Mart=C3=ADn=20Chaves?= Date: Thu, 22 Nov 2012 11:56:25 -0200 Subject: [PATCH 1/2] Update version to 1.1.11.f1 --- org.jiayun.commons4e/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.jiayun.commons4e/META-INF/MANIFEST.MF b/org.jiayun.commons4e/META-INF/MANIFEST.MF index 2aae049..cf2c6b7 100644 --- a/org.jiayun.commons4e/META-INF/MANIFEST.MF +++ b/org.jiayun.commons4e/META-INF/MANIFEST.MF @@ -1,7 +1,7 @@ Manifest-Version: 1.0 Bundle-Name: Commons4E Bundle-SymbolicName: org.jiayun.commons4e; singleton=true -Bundle-Version: 1.1.11 +Bundle-Version: 1.1.11.f1 Bundle-ClassPath: commons4e.jar Bundle-Activator: org.jiayun.commons4e.Commons4ePlugin Bundle-Vendor: JiaYun From d4f139eb60aebe1f9e03ffdd395955768b907aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerardo=20Mart=C3=ADn=20Chaves?= Date: Thu, 22 Nov 2012 11:56:56 -0200 Subject: [PATCH 2/2] Fix using instanceOf on equals generation. By equals contract the equals method must be symmetric. This means that a.equals(b) must be true if and only if b.equals(a) is true. Using instanceOf doesn't grant this. --- .../generators/EqualsHashCodeGenerator.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java index 56f4aa5..27a10c5 100644 --- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java +++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java @@ -176,16 +176,39 @@ private String createEqualsMethod(final IType objectClass, content.append("@Override\n"); } content.append("public boolean equals(final Object other) {\n"); + content.append("if (other == null) return false;\n"); if (compareReferences) { content.append("if (this == other) return true;"); } - content.append("if ( !(other instanceof "); - content.append(objectClass.getElementName()); - content.append(") ) return false;\n"); - content.append(objectClass.getElementName()); - content.append(" castOther = ("); - content.append(objectClass.getElementName()); - content.append(") other;\n"); + // by definition a.equals(b) == b.equals(a) (symmetric), using instanceOf can't assure this + // example: + // class A { + // int field1; + // public A(int x) { field1 = x }; + // } + // class B extends A { + // int field2; + // public A(int x, int y) { super(x); field2 = y }; + // } + // ... + // A a = new A(1) + // B b = new B(1, 2); + // a.equals(b); // true + // b.equals(a); // false + // + // from jdk javadoc for equals: + // (equals) It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. + +// content.append("if ( !(other instanceof "); +// content.append(objectClass.getElementName()); +// content.append(") ) return false;\n"); + content.append("if (!(other.getClass() == this.getClass())) return false;\n"); + + content.append(objectClass.getElementName()); + content.append(" castOther = ("); + content.append(objectClass.getElementName()); + content.append(") other;\n"); + content.append("return new EqualsBuilder()"); if (appendSuper) { content.append(".appendSuper(super.equals(other))");