Skip to content

Commit

Permalink
Fixes #2165: Generated equals fails on annotated array type
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel Spilker authored and Roel Spilker committed Jul 8, 2019
1 parent 11065b5 commit 6ed3b7c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/changelog.markdown
Expand Up @@ -5,6 +5,7 @@ Lombok Changelog
* ENHANCEMENT: Thanks to Mark Haynes, the `staticConstructor` will now also be generated if a (private) constructor already exists. [Issue #2100](https://github.com/rzwitserloot/lombok/issues/2100)
* ENHANCEMENT: `val` is now capable of decoding the type of convoluted expressions (particularly if the right hand side involves lambdas and conditional (ternary) expressions). [Pull Request #2109](https://github.com/rzwitserloot/lombok/pull/2109) with thanks to Alexander Bulgakov.
* BUGFIX: Delombok would turn something like `List<byte[]>...` in a method parameter to `List<byte...>...` [Issue #2140](https://github.com/rzwitserloot/lombok/issues/2140)
* BUGFIX: Javac would generate the wrong equals and hashCode if a type-use annotation was put on an array type field [Issue #2165](https://github.com/rzwitserloot/lombok/issues/2165)

### v1.18.8 (May 7th, 2019)
* FEATURE: You can now configure `@FieldNameConstants` to `CONSTANT_CASE` the generated constants, using a `lombok.config` option. See the [FieldNameConstants documentation](https://projectlombok.org/features/experimental/FieldNameConstants). [Issue #2092](https://github.com/rzwitserloot/lombok/issues/2092).
Expand Down
43 changes: 37 additions & 6 deletions src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
Expand Up @@ -25,6 +25,7 @@
import static lombok.javac.Javac.*;
import static lombok.javac.handlers.JavacHandlerUtil.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -234,7 +235,7 @@ public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List<Included<J

for (Included<JavacNode, EqualsAndHashCode.Include> member : members) {
JavacNode memberNode = member.getNode();
JCExpression fType = getFieldType(memberNode, fieldAccess);
JCExpression fType = unnotate(getFieldType(memberNode, fieldAccess));
boolean isMethod = memberNode.getKind() == Kind.METHOD;

JCExpression fieldAccessor = isMethod ? createMethodAccessor(maker, memberNode) : createFieldAccessor(maker, memberNode, fieldAccess);
Expand Down Expand Up @@ -279,9 +280,10 @@ public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List<Included<J
break;
}
} else if (fType instanceof JCArrayTypeTree) {
JCArrayTypeTree array = (JCArrayTypeTree) fType;
/* java.util.Arrays.deepHashCode(this.fieldName) //use just hashCode() for primitive arrays. */
boolean multiDim = ((JCArrayTypeTree) fType).elemtype instanceof JCArrayTypeTree;
boolean primitiveArray = ((JCArrayTypeTree) fType).elemtype instanceof JCPrimitiveTypeTree;
boolean multiDim = unnotate(array.elemtype) instanceof JCArrayTypeTree;
boolean primitiveArray = unnotate(array.elemtype) instanceof JCPrimitiveTypeTree;
boolean useDeepHC = multiDim || !primitiveArray;

JCExpression hcMethod = chainDots(typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode");
Expand Down Expand Up @@ -430,7 +432,7 @@ public JCMethodDecl createEquals(JavacNode typeNode, java.util.List<Included<Jav
JavacNode memberNode = member.getNode();
boolean isMethod = memberNode.getKind() == Kind.METHOD;

JCExpression fType = getFieldType(memberNode, fieldAccess);
JCExpression fType = unnotate(getFieldType(memberNode, fieldAccess));
JCExpression thisFieldAccessor = isMethod ? createMethodAccessor(maker, memberNode) : createFieldAccessor(maker, memberNode, fieldAccess);
JCExpression otherFieldAccessor = isMethod ? createMethodAccessor(maker, memberNode, maker.Ident(otherName)) : createFieldAccessor(maker, memberNode, fieldAccess, maker.Ident(otherName));
if (fType instanceof JCPrimitiveTypeTree) {
Expand All @@ -450,9 +452,10 @@ public JCMethodDecl createEquals(JavacNode typeNode, java.util.List<Included<Jav
break;
}
} else if (fType instanceof JCArrayTypeTree) {
JCArrayTypeTree array = (JCArrayTypeTree) fType;
/* if (!java.util.Arrays.deepEquals(this.fieldName, other.fieldName)) return false; //use equals for primitive arrays. */
boolean multiDim = ((JCArrayTypeTree) fType).elemtype instanceof JCArrayTypeTree;
boolean primitiveArray = ((JCArrayTypeTree) fType).elemtype instanceof JCPrimitiveTypeTree;
boolean multiDim = unnotate(array.elemtype) instanceof JCArrayTypeTree;
boolean primitiveArray = unnotate(array.elemtype) instanceof JCPrimitiveTypeTree;
boolean useDeepEquals = multiDim || !primitiveArray;

JCExpression eqMethod = chainDots(typeNode, "java", "util", "Arrays", useDeepEquals ? "deepEquals" : "equals");
Expand Down Expand Up @@ -522,4 +525,32 @@ public JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExp
public JCStatement returnBool(JavacTreeMaker maker, boolean bool) {
return maker.Return(maker.Literal(CTC_BOOLEAN, bool ? 1 : 0));
}

private boolean jcAnnotatedTypeInit = false;
private Class<?> jcAnnotatedTypeClass = null;
private Field jcAnnotatedTypeUnderlyingTypeField = null;

private JCExpression unnotate(JCExpression type) {
if (!isJcAnnotatedType(type)) return type;
if (jcAnnotatedTypeUnderlyingTypeField == null) return type;
try {
return (JCExpression) jcAnnotatedTypeUnderlyingTypeField.get(type);
} catch (Exception ignore) {}
return type;
}

private boolean isJcAnnotatedType(Object o) {
if (o == null) return false;
if (jcAnnotatedTypeInit) return jcAnnotatedTypeClass == o.getClass();
Class<?> c = o.getClass();
if (c.getSimpleName().equals("JCAnnotatedType")) {
jcAnnotatedTypeClass = c;
try {
jcAnnotatedTypeUnderlyingTypeField = c.getDeclaredField("underlyingType");
} catch (Exception ignore) {}
jcAnnotatedTypeInit = true;
return true;
}
return false;
}
}
@@ -0,0 +1,51 @@
import java.lang.annotation.*;

class EqualsAndHashCodeAnnotated {
@Annotated
int primitive;
@Annotated
Object object;
int @Annotated [] primitiveValues;
int @Annotated [] @Annotated [] morePrimitiveValues;
Integer @Annotated [] objectValues;
Integer @Annotated [] @Annotated [] moreObjectValues;
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.SOURCE)
@interface Annotated {
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeAnnotated)) return false;
final EqualsAndHashCodeAnnotated other = (EqualsAndHashCodeAnnotated) o;
if (!other.canEqual((java.lang.Object) this)) return false;
if (this.primitive != other.primitive) return false;
final java.lang.Object this$object = this.object;
final java.lang.Object other$object = other.object;
if (this$object == null ? other$object != null : !this$object.equals(other$object)) return false;
if (!java.util.Arrays.equals(this.primitiveValues, other.primitiveValues)) return false;
if (!java.util.Arrays.deepEquals(this.morePrimitiveValues, other.morePrimitiveValues)) return false;
if (!java.util.Arrays.deepEquals(this.objectValues, other.objectValues)) return false;
if (!java.util.Arrays.deepEquals(this.moreObjectValues, other.moreObjectValues)) return false;
return true;
}
@java.lang.SuppressWarnings("all")
protected boolean canEqual(final java.lang.Object other) {
return other instanceof EqualsAndHashCodeAnnotated;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.primitive;
final java.lang.Object $object = this.object;
result = result * PRIME + ($object == null ? 43 : $object.hashCode());
result = result * PRIME + java.util.Arrays.hashCode(this.primitiveValues);
result = result * PRIME + java.util.Arrays.deepHashCode(this.morePrimitiveValues);
result = result * PRIME + java.util.Arrays.deepHashCode(this.objectValues);
result = result * PRIME + java.util.Arrays.deepHashCode(this.moreObjectValues);
return result;
}
}
19 changes: 19 additions & 0 deletions test/transform/resource/before/EqualsAndHashCodeAnnotated.java
@@ -0,0 +1,19 @@
//version 8
import java.lang.annotation.*;

@lombok.EqualsAndHashCode
class EqualsAndHashCodeAnnotated {
@Annotated int primitive;
@Annotated Object object;

int @Annotated [] primitiveValues;
int @Annotated [] @Annotated [] morePrimitiveValues;

Integer @Annotated [] objectValues;
Integer @Annotated [] @Annotated [] moreObjectValues;

@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.SOURCE)
@interface Annotated {
}
}

0 comments on commit 6ed3b7c

Please sign in to comment.