diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-11-08 15:51:08 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-11-08 15:51:08 +0100 |
commit | db19327cb4b9f5a08665b85d4cb953118ce69c13 (patch) | |
tree | 4271b44e0d4c2737d54744ad7e8f0222d5c7b3d2 /src | |
parent | a685d0828c3cda044073054c203d17bcf6ab1096 (diff) | |
download | lombok-db19327cb4b9f5a08665b85d4cb953118ce69c13.tar.gz lombok-db19327cb4b9f5a08665b85d4cb953118ce69c13.tar.bz2 lombok-db19327cb4b9f5a08665b85d4cb953118ce69c13.zip |
[fixes #1628] Lombok now marks the `result` field used in the generated hashCode method as final if it isn’t modified. This doesn’t change its behavior whatsoever, but some linters and especially eclipse save actions (specifically: ‘mark local variables final if possible’) cause issues when they try to mess with generated code. Of course, now any save action with ‘remove useless modifiers’ would cause an issue but those don’t (yet…) exist.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 13 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java | 6 |
2 files changed, 15 insertions, 4 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 6945e5d9..84e5185d 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -227,7 +227,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH public MethodDeclaration createHashCode(EclipseNode type, Collection<Included<EclipseNode, EqualsAndHashCode.Include>> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { int pS = source.sourceStart, pE = source.sourceEnd; - long p = (long)pS << 32 | pE; + long p = (long) pS << 32 | pE; MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source); @@ -246,7 +246,14 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH List<Statement> statements = new ArrayList<Statement>(); - final boolean isEmpty = members.isEmpty(); + boolean isEmpty = true; + for (Included<EclipseNode, EqualsAndHashCode.Include> member : members) { + TypeReference fType = getFieldType(member.getNode(), fieldAccess); + if (fType.getLastToken() != null) { + isEmpty = false; + break; + } + } /* final int PRIME = X; */ { /* Without members, PRIME isn't used, as that would trigger a 'local variable not used' warning. */ @@ -282,6 +289,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH resultDecl.initialization = init; resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; + if (isEmpty) resultDecl.modifiers |= Modifier.FINAL; setGeneratedBy(resultDecl.type, source); statements.add(resultDecl); } @@ -303,6 +311,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH setGeneratedBy(intForBool, source); statements.add(createResultCalculation(source, intForBool)); } else if (Arrays.equals(TypeConstants.LONG, token)) { + /* (int)(ref >>> 32 ^ ref) */ statements.add(createLocalDeclaration(source, dollarFieldName, TypeReference.baseTypeReference(TypeIds.T_long, 0), fieldAccessor)); SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy1, source); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index aa0fe633..cb12bd4e 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -210,8 +210,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas Name resultName = typeNode.toName(RESULT_NAME); long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext()); + boolean isEmpty = members.isEmpty(); + /* final int PRIME = X; */ { - if (!members.isEmpty()) { + if (!isEmpty) { statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode()))); } } @@ -227,7 +229,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas /* ... 1; */ init = maker.Literal(1); } - statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(CTC_INT), init)); + statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0), resultName, maker.TypeIdent(CTC_INT), init)); } for (Included<JavacNode, EqualsAndHashCode.Include> member : members) { |