Finish implementation, add tests

This commit is contained in:
Andre Brait authored and Roel Spilker committed 2020-09-24 22:25:15 +02:00
1 parent 4e0ffbc329
commit d00ab552e1
5 files changed
+50 -32

No files matched your search

@@ -62,6 +62,7 @@ import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression;
import org.eclipse.jdt.internal.compiler.ast.IntLiteral;
@@ -305,8 +306,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
/* if ($hashCodeCache != 0) return $hashCodeCache; */ {
if (cacheHashCode) {
SingleNameReference hashCodeCacheRef = new SingleNameReference(HASH_CODE_CACHE_NAME_ARR, p);
FieldReference hashCodeCacheRef = new FieldReference(HASH_CODE_CACHE_NAME_ARR, p);
hashCodeCacheRef.receiver = new ThisReference(pS, pE);
setGeneratedBy(hashCodeCacheRef, source);
setGeneratedBy(hashCodeCacheRef.receiver, source);
EqualExpression cacheNotZero = new EqualExpression(hashCodeCacheRef, makeIntLiteral("0".toCharArray(), source), OperatorIds.NOT_EQUAL);
setGeneratedBy(cacheNotZero, source);
ReturnStatement returnCache = new ReturnStatement(hashCodeCacheRef, pS, pS);
@@ -448,8 +451,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
/* $hashCodeCache = result; */ {
if (cacheHashCode) {
SingleNameReference hashCodeCacheRef = new SingleNameReference(HASH_CODE_CACHE_NAME_ARR, p);
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);
Assignment cacheResult = new Assignment(hashCodeCacheRef, resultRef, pE);
@@ -42,6 +42,8 @@ import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
@@ -244,11 +246,12 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
boolean isEmpty = members.isEmpty();
/* if ($hashCodeCache != 0) return $hashCodeCache; */ {
/* if (this.$hashCodeCache != 0) return this.$hashCodeCache; */ {
if (cacheHashCode) {
Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME);
JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, maker.Ident(cacheHashCodeName), maker.Literal(CTC_INT, 0));
statements.append(maker.If(cacheNotZero, maker.Return(maker.Ident(cacheHashCodeName)), null));
JCIdent receiver = maker.Ident(typeNode.toName("this"));
JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME));
JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, cacheHashCodeFieldAccess, maker.Literal(CTC_INT, 0));
statements.append(maker.If(cacheNotZero, maker.Return(cacheHashCodeFieldAccess), null));
}
}
@@ -341,10 +344,11 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
}
}
/* $hashCodeCache = result; */ {
/* this.$hashCodeCache = result; */ {
if (cacheHashCode) {
Name cacheHashCodeName = typeNode.toName(HASH_CODE_CACHE_NAME);
statements.append(maker.Exec(maker.Assign(maker.Ident(cacheHashCodeName), maker.Ident(resultName))));
JCIdent receiver = maker.Ident(typeNode.toName("this"));
JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME));
statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, maker.Ident(resultName))));
}
}
@@ -42,12 +42,13 @@ class EqualsAndHashCode {
}
}
final class EqualsAndHashCode2 {
@java.lang.SuppressWarnings("all")
private transient int $hashCodeCache = 0;
int x;
long y;
float f;
double d;
boolean b;
private transient int $hashCodeCache = 0;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
@@ -64,7 +65,7 @@ final class EqualsAndHashCode2 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
if ($hashCodeCache != 0) return $hashCodeCache;
if (this.$hashCodeCache != 0) return this.$hashCodeCache;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.x;
@@ -74,11 +75,12 @@ final class EqualsAndHashCode2 {
final long $d = java.lang.Double.doubleToLongBits(this.d);
result = result * PRIME + (int) ($d >>> 32 ^ $d);
result = result * PRIME + (this.b ? 79 : 97);
$hashCodeCache = result;
this.$hashCodeCache = result;
return result;
}
}
final class EqualsAndHashCode3 extends EqualsAndHashCode {
@java.lang.SuppressWarnings("all")
private transient int $hashCodeCache = 0;
@java.lang.Override
@java.lang.SuppressWarnings("all")
@@ -96,9 +98,9 @@ final class EqualsAndHashCode3 extends EqualsAndHashCode {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
if ($hashCodeCache != 0) return $hashCodeCache;
if (this.$hashCodeCache != 0) return this.$hashCodeCache;
final int result = 1;
$hashCodeCache = result;
this.$hashCodeCache = result;
return result;
}
}
@@ -125,13 +127,14 @@ class EqualsAndHashCode4 extends EqualsAndHashCode {
}
}
class EqualsAndHashCode5 extends EqualsAndHashCode {
@java.lang.SuppressWarnings("all")
private transient int $hashCodeCache = 0;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCode5)) return false;
final EqualsAndHashCode5 other = (EqualsAndHashCode4) o;
final EqualsAndHashCode5 other = (EqualsAndHashCode5) o;
if (!other.canEqual((java.lang.Object) this)) return false;
if (!super.equals(o)) return false;
return true;
@@ -143,9 +146,9 @@ class EqualsAndHashCode5 extends EqualsAndHashCode {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
if ($hashCodeCache != 0) return $hashCodeCache;
if (this.$hashCodeCache != 0) return this.$hashCodeCache;
final int result = super.hashCode();
$hashCodeCache = result;
this.$hashCodeCache = result;
return result;
}
}
@@ -48,12 +48,12 @@
}
}
final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode2 {
private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0;
int x;
long y;
float f;
double d;
boolean b;
private transient int $hashCodeCache = 0;
EqualsAndHashCode2() {
super();
}
@@ -76,8 +76,8 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt
return true;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
if ($hashCodeCache != 0)
return $hashCodeCache;
if ((this.$hashCodeCache != 0))
return this.$hashCodeCache;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.x);
@@ -87,11 +87,12 @@ final @lombok.EqualsAndHashCode(cacheStrategy = lombok.EqualsAndHashCode.CacheSt
final long $d = java.lang.Double.doubleToLongBits(this.d);
result = ((result * PRIME) + (int) ($d ^ ($d >>> 32)));
result = ((result * PRIME) + (this.b ? 79 : 97));
$hashCodeCache = result;
this.$hashCodeCache = result;
return result;
}
}
final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode {
final @lombok.EqualsAndHashCode(callSuper = false,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode3 extends EqualsAndHashCode {
private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0;
EqualsAndHashCode3() {
super();
}
@@ -109,11 +110,14 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn
return (other instanceof EqualsAndHashCode3);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
if ((this.$hashCodeCache != 0))
return this.$hashCodeCache;
final int result = 1;
this.$hashCodeCache = result;
return result;
}
}
@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode {
@lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode4 extends EqualsAndHashCode {
EqualsAndHashCode4() {
super();
}
@@ -137,10 +141,10 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn
return result;
}
}
@lombok.EqualsAndHashCode(callSuper=true, cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode {
private transient int $hashCodeCache = 0;
final @lombok.EqualsAndHashCode(callSuper = true,cacheStrategy = lombok.EqualsAndHashCode.CacheStrategy.LAZY) class EqualsAndHashCode5 extends EqualsAndHashCode {
private transient @java.lang.SuppressWarnings("all") int $hashCodeCache = 0;
EqualsAndHashCode5() {
super();
super();
}
public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) {
if ((o == this))
@@ -149,19 +153,19 @@ final @lombok.EqualsAndHashCode(callSuper=false, cacheStrategy = lombok.EqualsAn
return false;
final EqualsAndHashCode5 other = (EqualsAndHashCode5) o;
if ((! other.canEqual((java.lang.Object) this)))
return false;
return false;
if ((! super.equals(o)))
return false;
return true;
return false;
return true;
}
protected @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) {
return (other instanceof EqualsAndHashCode5);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
if ($hashCodeCache != 0)
return $hashCodeCache;
if ((this.$hashCodeCache != 0))
return this.$hashCodeCache;
final int result = super.hashCode();
$hashCodeCache = result;
this.$hashCodeCache = result;
return result;
}
}
@@ -0,0 +1,2 @@
1 Not caching the result of hashCode: Annotated type is not final.
23 Not caching the result of hashCode: Annotated type is not final.