aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2020-09-25 00:13:50 +0200
committerRoel Spilker <r.spilker@gmail.com>2020-09-25 00:13:50 +0200
commit2ba65d105ddfce54690a64f93cbc5c55480ef364 (patch)
treec79158768703c8cc77ba9b031f30bc2d7d40db7f /src/core
parentee6c0ca7f2274b24571afdc8586eff9052f5eda1 (diff)
downloadlombok-2ba65d105ddfce54690a64f93cbc5c55480ef364.tar.gz
lombok-2ba65d105ddfce54690a64f93cbc5c55480ef364.tar.bz2
lombok-2ba65d105ddfce54690a64f93cbc5c55480ef364.zip
Cache hashCode:
- Fix bug where 0 would be returned once - Fix Eclipse position error - Don't initialize field with default value 0
Diffstat (limited to 'src/core')
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java44
-rw-r--r--src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java22
2 files changed, 43 insertions, 23 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index 2f0ce227..deb19c00 100755
--- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -246,12 +246,11 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
}
private void createHashCodeCacheField(EclipseNode typeNode, ASTNode source) {
- FieldDeclaration hashCodeCacheDecl = new FieldDeclaration(HASH_CODE_CACHE_NAME_ARR, 0, -1);
+ FieldDeclaration hashCodeCacheDecl = new FieldDeclaration(HASH_CODE_CACHE_NAME_ARR, 0, 0);
hashCodeCacheDecl.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccTransient;
hashCodeCacheDecl.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
hashCodeCacheDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
hashCodeCacheDecl.declarationSourceEnd = -1;
- hashCodeCacheDecl.initialization = makeIntLiteral("0".toCharArray(), source);
injectFieldAndMarkGenerated(typeNode, hashCodeCacheDecl);
setGeneratedBy(hashCodeCacheDecl, source);
setGeneratedBy(hashCodeCacheDecl.type, source);
@@ -345,7 +344,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;
+ if (isEmpty && !cacheHashCode) resultDecl.modifiers |= Modifier.FINAL;
setGeneratedBy(resultDecl.type, source);
statements.add(resultDecl);
}
@@ -440,24 +439,43 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
}
}
- /* this.$hashCodeCache = result != 0 ? result : Integer.MIN_VALUE; */ {
+ /*
+ * if (result == 0) result = Integer.MIN_VALUE;
+ * this.$hashCodeCache = result;
+ *
+ */ {
if (cacheHashCode) {
- FieldReference hashCodeCacheRef = new FieldReference(HASH_CODE_CACHE_NAME_ARR, p);
- hashCodeCacheRef.receiver = new ThisReference(pS, pE);
- setGeneratedBy(hashCodeCacheRef, source);
- setGeneratedBy(hashCodeCacheRef.receiver, source);
SingleNameReference resultRef = new SingleNameReference(RESULT, p);
setGeneratedBy(resultRef, source);
- EqualExpression resultNotZero = new EqualExpression(resultRef, makeIntLiteral("0".toCharArray(), source), OperatorIds.NOT_EQUAL);
- setGeneratedBy(resultNotZero, source);
+
+ EqualExpression resultIsZero = new EqualExpression(resultRef, makeIntLiteral("0".toCharArray(), source), OperatorIds.EQUAL_EQUAL);
+ setGeneratedBy(resultIsZero, source);
+
+ resultRef = new SingleNameReference(RESULT, p);
+ setGeneratedBy(resultRef, source);
+
FieldReference integerMinValue = new FieldReference("MIN_VALUE".toCharArray(), p);
integerMinValue.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_INTEGER);
setGeneratedBy(integerMinValue, source);
+
+ Assignment newResult = new Assignment(resultRef, integerMinValue, pE);
+ newResult.sourceStart = pS; newResult.statementEnd = newResult.sourceEnd = pE;
+ setGeneratedBy(newResult, source);
+
+ IfStatement ifStatement = new IfStatement(resultIsZero, newResult, pS, pE);
+ setGeneratedBy(ifStatement, source);
+ statements.add(ifStatement);
+
+
+ FieldReference hashCodeCacheRef = new FieldReference(HASH_CODE_CACHE_NAME_ARR, p);
+ hashCodeCacheRef.receiver = new ThisReference(pS, pE);
+ setGeneratedBy(hashCodeCacheRef, source);
+ setGeneratedBy(hashCodeCacheRef.receiver, source);
+
resultRef = new SingleNameReference(RESULT, p);
setGeneratedBy(resultRef, source);
- ConditionalExpression notZeroOrIntegerMin = new ConditionalExpression(resultNotZero, resultRef, integerMinValue);
- setGeneratedBy(notZeroOrIntegerMin, source);
- Assignment cacheResult = new Assignment(hashCodeCacheRef, notZeroOrIntegerMin, pE);
+
+ Assignment cacheResult = new Assignment(hashCodeCacheRef, resultRef, pE);
cacheResult.sourceStart = pS; cacheResult.statementEnd = cacheResult.sourceEnd = pE;
setGeneratedBy(cacheResult, source);
statements.add(cacheResult);
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
index ea1820f8..9c5b8762 100644
--- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -40,7 +40,6 @@ import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.JCTree.JCBinary;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-import com.sun.tools.javac.tree.JCTree.JCConditional;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
@@ -221,7 +220,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
private void createHashCodeCacheField(JavacNode typeNode, JCTree source) {
JavacTreeMaker maker = typeNode.getTreeMaker();
JCModifiers mods = maker.Modifiers(Flags.PRIVATE | Flags.TRANSIENT);
- JCVariableDecl hashCodeCacheField = maker.VarDef(mods, typeNode.toName(HASH_CODE_CACHE_NAME), maker.TypeIdent(CTC_INT), maker.Literal(CTC_INT, 0));
+ JCVariableDecl hashCodeCacheField = maker.VarDef(mods, typeNode.toName(HASH_CODE_CACHE_NAME), maker.TypeIdent(CTC_INT), null);
injectFieldAndMarkGenerated(typeNode, hashCodeCacheField);
recursiveSetGeneratedBy(hashCodeCacheField, source, typeNode.getContext());
}
@@ -273,7 +272,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
/* ... 1; */
init = maker.Literal(1);
}
- statements.append(maker.VarDef(maker.Modifiers(isEmpty ? finalFlag : 0), resultName, maker.TypeIdent(CTC_INT), init));
+ statements.append(maker.VarDef(maker.Modifiers(isEmpty && !cacheHashCode ? finalFlag : 0), resultName, maker.TypeIdent(CTC_INT), init));
}
for (Included<JavacNode, EqualsAndHashCode.Include> member : members) {
@@ -345,14 +344,17 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
}
}
- /* this.$hashCodeCache = result != 0 ? result : Integer.MIN_VALUE; */ {
+ /*
+ * if (result == 0) result = Integer.MIN_VALUE;
+ * this.$hashCodeCache = result;
+ *
+ */ {
if (cacheHashCode) {
- JCIdent receiver = maker.Ident(typeNode.toName("this"));
- JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME));
- JCExpression resultNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(resultName), maker.Literal(CTC_INT, 0));
- JCExpression integerMinValue = genJavaLangTypeRef(typeNode, "Integer", "MIN_VALUE");
- JCConditional notZeroOrIntegerMin = maker.Conditional(resultNotZero, maker.Ident(resultName), integerMinValue);
- statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, notZeroOrIntegerMin)));
+ statements.append(maker.If(maker.Binary(CTC_EQUAL, maker.Ident(resultName), maker.Literal(CTC_INT, 0)),
+ maker.Exec(maker.Assign(maker.Ident(resultName), genJavaLangTypeRef(typeNode, "Integer", "MIN_VALUE"))), null));
+
+ JCFieldAccess cacheHashCodeFieldAccess = maker.Select(maker.Ident(typeNode.toName("this")), typeNode.toName(HASH_CODE_CACHE_NAME));
+ statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, maker.Ident(resultName))));
}
}