diff options
author | Roel Spilker <Roels@topdesk.com> | 2019-07-08 22:00:03 +0200 |
---|---|---|
committer | Roel Spilker <Roels@topdesk.com> | 2019-07-08 23:17:38 +0200 |
commit | 2ccd3f5f5d089f5525f6b219df35a1200596f0a8 (patch) | |
tree | 76f11afe3648b22520a65a115058dd7fd4fe61fe /src/core/lombok/javac | |
parent | 11065b564f3fc1cee2c540a33b7ed1b3774816e2 (diff) | |
download | lombok-2ccd3f5f5d089f5525f6b219df35a1200596f0a8.tar.gz lombok-2ccd3f5f5d089f5525f6b219df35a1200596f0a8.tar.bz2 lombok-2ccd3f5f5d089f5525f6b219df35a1200596f0a8.zip |
Fixes #2165: Generated equals fails on annotated array type
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index cb12bd4e..e4d7fa7f 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -25,6 +25,7 @@ import static lombok.core.handlers.HandlerUtil.handleFlagUsage; 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; @@ -234,7 +235,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas 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); @@ -279,9 +280,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas 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"); @@ -430,7 +432,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas 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) { @@ -450,9 +452,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas 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"); @@ -522,4 +525,30 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas public JCStatement returnBool(JavacTreeMaker maker, boolean bool) { return maker.Return(maker.Literal(CTC_BOOLEAN, bool ? 1 : 0)); } + + private boolean jcAnnotatedTypeInit; + 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(JCExpression o) { + if (o == null) return false; + if (!jcAnnotatedTypeInit) { + try { + jcAnnotatedTypeClass = Class.forName("com.sun.tools.javac.tree.JCTree$JCAnnotatedType", false, o.getClass().getClassLoader()); + jcAnnotatedTypeUnderlyingTypeField = jcAnnotatedTypeClass.getDeclaredField("underlyingType"); + } + catch (Exception ignore) {} + jcAnnotatedTypeInit = true; + } + return jcAnnotatedTypeClass == o.getClass(); + } } |