aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lombok/EqualsAndHashCode.java69
-rw-r--r--src/lombok/eclipse/handlers/HandleData.java336
-rw-r--r--src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java531
-rw-r--r--src/lombok/javac/handlers/HandleData.java251
-rw-r--r--src/lombok/javac/handlers/HandleEqualsAndHashCode.java429
5 files changed, 1034 insertions, 582 deletions
diff --git a/src/lombok/EqualsAndHashCode.java b/src/lombok/EqualsAndHashCode.java
new file mode 100644
index 00000000..715dbd7a
--- /dev/null
+++ b/src/lombok/EqualsAndHashCode.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Generates implementations for the <code>equals</code> and <code>hashCode</code> methods inherited by all objects.
+ * <p>
+ * If either method already exists, then <code>&#64;EqualsAndHashCode</code> will not generate that particular method.
+ * If they all exist, <code>&#64;EqualsAndHashCode</code> generates no methods, and emits a warning instead to highlight
+ * that its doing nothing at all. The parameter list and return type are not relevant when deciding to skip generation of
+ * a method; any method named <code>hashCode</code> will make <code>&#64;EqualsAndHashCode</code> not generate that method,
+ * for example.
+ * <p>
+ * All fields that are non-static and non-transient are used in the equality check and hashCode generation. You can exclude
+ * more fields by specifying them in the <code>exclude</code> parameter.
+ * <p>
+ * Normally, auto-generating <code>hashCode</code> and <code>equals</code> implementations in a subclass is a bad idea, as
+ * the superclass also defines fields, for which equality checks/hashcodes won't be auto-generated. Therefore, a warning
+ * is emitted when you try. Instead, you can set the <code>callSuper</code> parameter to <em>true</em> which will call
+ * <code>super.equals</code> and <code>super.hashCode</code>. Doing this with <code>java.lang.Object</code> as superclass is
+ * pointless, so, conversely, setting this flag when <em>NOT</em> extending something (other than Object) will also generate
+ * a warning. Be aware that not all implementations of <code>equals</code> correctly handle being called from a subclass!
+ * Fortunately, lombok-generated <code>equals</code> implementations do correctly handle it.
+ * <p>
+ * Array fields are handled by way of {@link java.util.Arrays#deepEquals(Object[], Object[])} where necessary, as well
+ * as <code>deepHashCode</code>. The downside is that arrays with circular references (arrays that contain themselves,
+ * possibly indirectly) results in calls to <code>hashCode</code> and <code>equals</code> throwing a
+ * {@link java.lang.StackOverflowError}. However, the implementations for java's own {@link java.util.ArrayList} suffer
+ * from the same flaw.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.SOURCE)
+public @interface EqualsAndHashCode {
+ /**
+ * Any fields listed here will not be taken into account in the generated
+ * <code>equals</code> and <code>hashCode</code> implementations.
+ */
+ String[] exclude() default {};
+
+ /**
+ * Call on the superclass's implementations of <code>equals</code> and <code>hashCode</code> before calculating
+ * for the fields in this class.
+ */
+ boolean callSuper() default false;
+}
diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java
index 749418eb..f1623e39 100644
--- a/src/lombok/eclipse/handlers/HandleData.java
+++ b/src/lombok/eclipse/handlers/HandleData.java
@@ -26,12 +26,8 @@ import static lombok.eclipse.handlers.PKG.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
import lombok.AccessLevel;
import lombok.Data;
@@ -47,44 +43,23 @@ import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.Assignment;
-import org.eclipse.jdt.internal.compiler.ast.BinaryExpression;
-import org.eclipse.jdt.internal.compiler.ast.CastExpression;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
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.IntLiteral;
-import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
-import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.NameReference;
-import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
-import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
-import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
-import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
-import org.eclipse.jdt.internal.compiler.ast.Reference;
import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
-import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
-import org.eclipse.jdt.internal.compiler.ast.UnaryExpression;
-import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
-import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
-import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.mangosdk.spi.ProviderFor;
/**
@@ -107,14 +82,12 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
return false;
}
- List<Node> nodesForEquality = new ArrayList<Node>();
List<Node> nodesForConstructor = new ArrayList<Node>();
for ( Node child : typeNode.down() ) {
if ( child.getKind() != Kind.FIELD ) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
//Skip static fields.
if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue;
- if ( (fieldDecl.modifiers & ClassFileConstants.AccTransient) == 0 ) nodesForEquality.add(child);
boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0;
if ( isFinal && fieldDecl.initialization == null ) nodesForConstructor.add(child);
new HandleGetter().generateGetterForField(child, annotationNode.get());
@@ -122,16 +95,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
}
new HandleToString().generateToStringForType(typeNode, annotationNode);
-
- if ( methodExists("equals", typeNode) == MemberExistsResult.NOT_EXISTS ) {
- MethodDeclaration equals = createEquals(typeNode, nodesForEquality, ast);
- injectMethod(typeNode, equals);
- }
-
- if ( methodExists("hashCode", typeNode) == MemberExistsResult.NOT_EXISTS ) {
- MethodDeclaration hashCode = createHashCode(typeNode, nodesForEquality, ast);
- injectMethod(typeNode, hashCode);
- }
+ new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
//Careful: Generate the public static constructor (if there is one) LAST, so that any attempt to
//'find callers' on the annotation node will find callers of the constructor, which is by far the
@@ -232,302 +196,4 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
constructor.statements = new Statement[] { new ReturnStatement(statement, (int)(p >> 32), (int)p) };
return constructor;
}
-
- private static final Set<String> BUILT_IN_TYPES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
- "byte", "short", "int", "long", "char", "boolean", "double", "float")));
-
- private MethodDeclaration createEquals(Node type, Collection<Node> fields, ASTNode pos) {
- MethodDeclaration method = new MethodDeclaration(
- ((CompilationUnitDeclaration) type.top().get()).compilationResult);
-
- method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
- method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
- method.annotations = new Annotation[] {
- new MarkerAnnotation(new QualifiedTypeReference(TypeConstants.JAVA_LANG_OVERRIDE, new long[] { 0, 0, 0}), 0)
- };
- method.selector = "equals".toCharArray();
- method.thrownExceptions = null;
- method.typeParameters = null;
- method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
- method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart;
- method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = pos.sourceEnd;
- method.arguments = new Argument[] {
- new Argument(new char[] { 'o' }, 0,
- new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }), 0)
- };
-
- List<Statement> statements = new ArrayList<Statement>();
-
- /* if ( o == this ) return true; */ {
- EqualExpression otherEqualsThis = new EqualExpression(
- new SingleNameReference(new char[] { 'o' }, 0),
- new ThisReference(0, 0), OperatorIds.EQUAL_EQUAL);
-
- ReturnStatement returnTrue = new ReturnStatement(new TrueLiteral(0, 0), 0, 0);
- IfStatement ifOtherEqualsThis = new IfStatement(otherEqualsThis, returnTrue, 0, 0);
- statements.add(ifOtherEqualsThis);
- }
-
- /* if ( o == null ) return false; */ {
- EqualExpression otherEqualsNull = new EqualExpression(
- new SingleNameReference(new char[] { 'o' }, 0),
- new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
-
- ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- IfStatement ifOtherEqualsNull = new IfStatement(otherEqualsNull, returnFalse, 0, 0);
- statements.add(ifOtherEqualsNull);
- }
-
- /* if ( o.getClass() != getClass() ) return false; */ {
- MessageSend otherGetClass = new MessageSend();
- otherGetClass.receiver = new SingleNameReference(new char[] { 'o' }, 0);
- otherGetClass.selector = "getClass".toCharArray();
- MessageSend thisGetClass = new MessageSend();
- thisGetClass.receiver = new ThisReference(0, 0);
- thisGetClass.selector = "getClass".toCharArray();
- EqualExpression classesNotEqual = new EqualExpression(otherGetClass, thisGetClass, OperatorIds.NOT_EQUAL);
- ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- IfStatement ifClassesNotEqual = new IfStatement(classesNotEqual, returnFalse, 0, 0);
- statements.add(ifClassesNotEqual);
- }
-
- char[] otherN = "other".toCharArray();
-
-
- TypeDeclaration typeDecl = (TypeDeclaration)type.get();
- /* MyType<?> other = (MyType<?>) o; */ {
- if ( !fields.isEmpty() ) {
- LocalDeclaration other = new LocalDeclaration(otherN, 0, 0);
- char[] typeName = typeDecl.name;
- Expression targetType;
- if ( typeDecl.typeParameters == null || typeDecl.typeParameters.length == 0 ) {
- targetType = new SingleNameReference(((TypeDeclaration)type.get()).name, 0);
- other.type = new SingleTypeReference(typeName, 0);
- } else {
- TypeReference[] typeArgs = new TypeReference[typeDecl.typeParameters.length];
- for ( int i = 0 ; i < typeArgs.length ; i++ ) typeArgs[i] = new Wildcard(Wildcard.UNBOUND);
- targetType = new ParameterizedSingleTypeReference(typeName, typeArgs, 0, 0);
- other.type = new ParameterizedSingleTypeReference(typeName, copyTypes(typeArgs), 0, 0);
- }
- other.initialization = new CastExpression(
- new SingleNameReference(new char[] { 'o' }, 0),
- targetType);
- statements.add(other);
- }
- }
-
- for ( Node field : fields ) {
- FieldDeclaration f = (FieldDeclaration) field.get();
- char[] token = f.type.getLastToken();
- if ( f.type.dimensions() == 0 && token != null ) {
- if ( Arrays.equals(TypeConstants.FLOAT, token) ) {
- statements.add(generateCompareFloatOrDouble(otherN, "Float".toCharArray(), f.name));
- } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) {
- statements.add(generateCompareFloatOrDouble(otherN, "Double".toCharArray(), f.name));
- } else if ( BUILT_IN_TYPES.contains(new String(token)) ) {
- EqualExpression fieldsNotEqual = new EqualExpression(
- new SingleNameReference(f.name, 0),
- generateQualifiedNameRef(otherN, f.name),
- OperatorIds.NOT_EQUAL);
- ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- statements.add(new IfStatement(fieldsNotEqual, returnStatement, 0, 0));
- } else /* objects */ {
- EqualExpression fieldIsNull = new EqualExpression(
- new SingleNameReference(f.name, 0),
- new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
- EqualExpression otherFieldIsntNull = new EqualExpression(
- generateQualifiedNameRef(otherN, f.name),
- new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
- MessageSend equalsCall = new MessageSend();
- equalsCall.receiver = new SingleNameReference(f.name, 0);
- equalsCall.selector = "equals".toCharArray();
- equalsCall.arguments = new Expression[] { generateQualifiedNameRef(otherN, f.name) };
- UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT);
- ConditionalExpression fullEquals = new ConditionalExpression(fieldIsNull, otherFieldIsntNull, fieldsNotEqual);
- ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- statements.add(new IfStatement(fullEquals, returnStatement, 0, 0));
- }
- } else if ( f.type.dimensions() > 0 && token != null ) {
- MessageSend arraysEqualCall = new MessageSend();
- arraysEqualCall.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
- if ( f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token)) ) {
- arraysEqualCall.selector = "deepEquals".toCharArray();
- } else {
- arraysEqualCall.selector = "equals".toCharArray();
- }
- arraysEqualCall.arguments = new Expression[] {
- new SingleNameReference(f.name, 0),
- generateQualifiedNameRef(otherN, f.name) };
- UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT);
- ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- statements.add(new IfStatement(arraysNotEqual, returnStatement, 0, 0));
- }
- }
-
- /* return true; */ {
- statements.add(new ReturnStatement(new TrueLiteral(0, 0), 0, 0));
- }
- method.statements = statements.toArray(new Statement[statements.size()]);
- return method;
- }
-
- private IfStatement generateCompareFloatOrDouble(char[] otherN, char[] floatOrDouble, char[] fieldName) {
- /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false */
- MessageSend floatCompare = new MessageSend();
- floatCompare.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble);
- floatCompare.selector = "compare".toCharArray();
- floatCompare.arguments = new Expression[] {
- new SingleNameReference(fieldName, 0),
- generateQualifiedNameRef(otherN, fieldName)
- };
- EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, new IntLiteral(new char[] {'0'}, 0, 0), OperatorIds.NOT_EQUAL);
- ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
- return new IfStatement(ifFloatCompareIsNot0, returnFalse, 0, 0);
- }
-
- private Reference generateFieldReference(char[] fieldName) {
- FieldReference thisX = new FieldReference(("this." + new String(fieldName)).toCharArray(), 0);
- thisX.receiver = new ThisReference(0, 0);
- thisX.token = fieldName;
- return thisX;
- }
-
- private NameReference generateQualifiedNameRef(char[]... varNames) {
- if ( varNames.length > 1 )
- return new QualifiedNameReference(varNames, new long[varNames.length], 0, 0);
- else return new SingleNameReference(varNames[0], 0);
- }
-
- private MethodDeclaration createHashCode(Node type, Collection<Node> fields, ASTNode pos) {
- MethodDeclaration method = new MethodDeclaration(
- ((CompilationUnitDeclaration) type.top().get()).compilationResult);
-
- method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
- method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0);
- method.annotations = new Annotation[] {
- new MarkerAnnotation(new QualifiedTypeReference(TypeConstants.JAVA_LANG_OVERRIDE, new long[] { 0, 0, 0}), 0)
- };
- method.selector = "hashCode".toCharArray();
- method.thrownExceptions = null;
- method.typeParameters = null;
- method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
- method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart;
- method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = pos.sourceEnd;
- method.arguments = null;
-
- List<Statement> statements = new ArrayList<Statement>();
- List<Expression> intoResult = new ArrayList<Expression>();
-
- final char[] PRIME = "PRIME".toCharArray();
- final char[] RESULT = "result".toCharArray();
- final boolean isEmpty = fields.isEmpty();
-
- /* final int PRIME = 31; */ {
- if ( !isEmpty ) { /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */
- LocalDeclaration primeDecl = new LocalDeclaration(PRIME, 0 ,0);
- primeDecl.modifiers = Modifier.FINAL;
- primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
- primeDecl.initialization = new IntLiteral("31".toCharArray(), 0, 0);
- statements.add(primeDecl);
- }
- }
-
- /* int result = 1; */ {
- LocalDeclaration resultDecl = new LocalDeclaration(RESULT, 0, 0);
- resultDecl.initialization = new IntLiteral("1".toCharArray(), 0, 0);
- resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
- statements.add(resultDecl);
- }
-
- int tempCounter = 0;
- for ( Node field : fields ) {
- FieldDeclaration f = (FieldDeclaration) field.get();
- char[] token = f.type.getLastToken();
- if ( f.type.dimensions() == 0 && token != null ) {
- if ( Arrays.equals(TypeConstants.FLOAT, token) ) {
- /* Float.floatToIntBits(fieldName) */
- MessageSend floatToIntBits = new MessageSend();
- floatToIntBits.receiver = generateQualifiedNameRef(TypeConstants.JAVA_LANG_FLOAT);
- floatToIntBits.selector = "floatToIntBits".toCharArray();
- floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name) };
- intoResult.add(floatToIntBits);
- } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) {
- /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */
- MessageSend doubleToLongBits = new MessageSend();
- doubleToLongBits.receiver = generateQualifiedNameRef(TypeConstants.JAVA_LANG_DOUBLE);
- doubleToLongBits.selector = "doubleToLongBits".toCharArray();
- doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name) };
- final char[] tempName = ("temp" + ++tempCounter).toCharArray();
- LocalDeclaration tempVar = new LocalDeclaration(tempName, 0, 0);
- tempVar.initialization = doubleToLongBits;
- tempVar.type = TypeReference.baseTypeReference(TypeIds.T_long, 0);
- tempVar.modifiers = Modifier.FINAL;
- statements.add(tempVar);
- intoResult.add(longToIntForHashCode(
- new SingleNameReference(tempName, 0), new SingleNameReference(tempName, 0)));
- } else if ( Arrays.equals(TypeConstants.BOOLEAN, token) ) {
- /* booleanField ? 1231 : 1237 */
- intoResult.add(new ConditionalExpression(
- generateFieldReference(f.name),
- new IntLiteral("1231".toCharArray(), 0, 0),
- new IntLiteral("1237".toCharArray(), 0 ,0)));
- } else if ( Arrays.equals(TypeConstants.LONG, token) ) {
- intoResult.add(longToIntForHashCode(generateFieldReference(f.name), generateFieldReference(f.name)));
- } else if ( BUILT_IN_TYPES.contains(new String(token)) ) {
- intoResult.add(generateFieldReference(f.name));
- } else /* objects */ {
- /* this.fieldName == null ? 0 : this.fieldName.hashCode() */
- MessageSend hashCodeCall = new MessageSend();
- hashCodeCall.receiver = generateFieldReference(f.name);
- hashCodeCall.selector = "hashCode".toCharArray();
- EqualExpression objIsNull = new EqualExpression(
- generateFieldReference(f.name),
- new NullLiteral(0, 0),
- OperatorIds.EQUAL_EQUAL);
- ConditionalExpression nullOrHashCode = new ConditionalExpression(
- objIsNull,
- new IntLiteral("0".toCharArray(), 0, 0),
- hashCodeCall);
- intoResult.add(nullOrHashCode);
- }
- } else if ( f.type.dimensions() > 0 && token != null ) {
- /* Arrays.deepHashCode(array) //just hashCode for simple arrays */
- MessageSend arraysHashCodeCall = new MessageSend();
- arraysHashCodeCall.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
- if ( f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token)) ) {
- arraysHashCodeCall.selector = "deepHashCode".toCharArray();
- } else {
- arraysHashCodeCall.selector = "hashCode".toCharArray();
- }
- arraysHashCodeCall.arguments = new Expression[] { generateFieldReference(f.name) };
- intoResult.add(arraysHashCodeCall);
- }
- }
-
- /* fold each intoResult entry into:
- result = result * PRIME + (item); */ {
- for ( Expression ex : intoResult ) {
- BinaryExpression multiplyByPrime = new BinaryExpression(new SingleNameReference(RESULT, 0),
- new SingleNameReference(PRIME, 0), OperatorIds.MULTIPLY);
- BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS);
- statements.add(new Assignment(new SingleNameReference(RESULT, 0), addItem, 0));
- }
- }
-
- /* return result; */ {
- statements.add(new ReturnStatement(new SingleNameReference(RESULT, 0), 0, 0));
- }
- method.statements = statements.toArray(new Statement[statements.size()]);
- return method;
- }
-
- /** Give 2 clones! */
- private Expression longToIntForHashCode(Reference ref1, Reference ref2) {
- /* (int)(ref >>> 32 ^ ref) */
- BinaryExpression higherBits = new BinaryExpression(
- ref1, new IntLiteral("32".toCharArray(), 0, 0),
- OperatorIds.UNSIGNED_RIGHT_SHIFT);
- BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR);
- return new CastExpression(xorParts, TypeReference.baseTypeReference(TypeIds.T_int, 0));
- }
}
diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
new file mode 100644
index 00000000..d8e1562e
--- /dev/null
+++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -0,0 +1,531 @@
+/*
+ * Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.eclipse.handlers;
+
+import static lombok.eclipse.handlers.PKG.*;
+
+import static lombok.eclipse.Eclipse.copyTypes;
+
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
+import org.eclipse.jdt.internal.compiler.ast.Argument;
+import org.eclipse.jdt.internal.compiler.ast.Assignment;
+import org.eclipse.jdt.internal.compiler.ast.BinaryExpression;
+import org.eclipse.jdt.internal.compiler.ast.CastExpression;
+import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
+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.IntLiteral;
+import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
+import org.eclipse.jdt.internal.compiler.ast.MessageSend;
+import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.NameReference;
+import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
+import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
+import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.Reference;
+import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
+import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
+import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.Statement;
+import org.eclipse.jdt.internal.compiler.ast.SuperReference;
+import org.eclipse.jdt.internal.compiler.ast.ThisReference;
+import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.TypeReference;
+import org.eclipse.jdt.internal.compiler.ast.UnaryExpression;
+import org.eclipse.jdt.internal.compiler.ast.Wildcard;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
+import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
+import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
+import org.mangosdk.spi.ProviderFor;
+
+import lombok.AccessLevel;
+import lombok.EqualsAndHashCode;
+import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
+import lombok.eclipse.Eclipse;
+import lombok.eclipse.EclipseAnnotationHandler;
+import lombok.eclipse.EclipseAST.Node;
+
+/**
+ * Handles the <code>EqualsAndHashCode</code> annotation for eclipse.
+ */
+@ProviderFor(EclipseAnnotationHandler.class)
+public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsAndHashCode> {
+ private static final Set<String> BUILT_IN_TYPES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+ "byte", "short", "int", "long", "char", "boolean", "double", "float")));
+
+ private void checkForBogusExcludes(Node type, AnnotationValues<EqualsAndHashCode> annotation) {
+ List<String> list = Arrays.asList(annotation.getInstance().exclude());
+ boolean[] matched = new boolean[list.size()];
+
+ for ( Node child : type.down() ) {
+ if ( list.isEmpty() ) break;
+ if ( child.getKind() != Kind.FIELD ) continue;
+ if ( (((FieldDeclaration)child.get()).modifiers & ClassFileConstants.AccStatic) != 0 ) continue;
+ if ( (((FieldDeclaration)child.get()).modifiers & ClassFileConstants.AccTransient) != 0 ) continue;
+ int idx = list.indexOf(child.getName());
+ if ( idx > -1 ) matched[idx] = true;
+ }
+
+ for ( int i = 0 ; i < list.size() ; i++ ) {
+ if ( !matched[i] ) {
+ annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i);
+ }
+ }
+ }
+
+ public void generateEqualsAndHashCodeForType(Node typeNode, Node errorNode) {
+ for ( Node child : typeNode.down() ) {
+ if ( child.getKind() == Kind.ANNOTATION ) {
+ if ( Eclipse.annotationTypeMatches(EqualsAndHashCode.class, child) ) {
+ //The annotation will make it happen, so we can skip it.
+ return;
+ }
+ }
+ }
+
+ boolean callSuper = false;
+ try {
+ callSuper = ((Boolean)EqualsAndHashCode.class.getMethod("callSuper").getDefaultValue()).booleanValue();
+ } catch ( Exception ignore ) {}
+ generateMethods(typeNode, errorNode, Collections.<String>emptyList(), callSuper, false);
+ }
+
+ @Override public boolean handle(AnnotationValues<EqualsAndHashCode> annotation, Annotation ast, Node annotationNode) {
+ EqualsAndHashCode ann = annotation.getInstance();
+ List<String> excludes = Arrays.asList(ann.exclude());
+ Node typeNode = annotationNode.up();
+
+ checkForBogusExcludes(typeNode, annotation);
+
+ return generateMethods(typeNode, annotationNode, excludes, ann.callSuper(), true);
+ }
+
+ public boolean generateMethods(Node typeNode, Node errorNode, List<String> excludes,
+ boolean callSuper, boolean whineIfExists) {
+ TypeDeclaration typeDecl = null;
+
+ if ( typeNode.get() instanceof TypeDeclaration ) typeDecl = (TypeDeclaration) typeNode.get();
+ int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
+ boolean notAClass = (modifiers &
+ (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
+
+ if ( typeDecl == null || notAClass ) {
+ errorNode.addError("@EqualsAndHashCode is only supported on a class.");
+ return false;
+ }
+
+ boolean isDirectDescendentOfObject = true;
+
+ if ( typeDecl.superclass != null ) {
+ String p = typeDecl.superclass.toString();
+ isDirectDescendentOfObject = p.equals("Object") || p.equals("java.lang.Object");
+ }
+
+ if ( isDirectDescendentOfObject && callSuper ) {
+ errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless.");
+ return true;
+ }
+
+ if ( !isDirectDescendentOfObject && !callSuper ) {
+ errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object.");
+ }
+
+ List<Node> nodesForEquality = new ArrayList<Node>();
+ for ( Node child : typeNode.down() ) {
+ if ( child.getKind() != Kind.FIELD ) continue;
+ FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
+ //Skip static fields.
+ if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue;
+ //Skip transient fields.
+ if ( (fieldDecl.modifiers & ClassFileConstants.AccTransient) != 0 ) continue;
+ //Skip excluded fields.
+ if ( excludes.contains(new String(fieldDecl.name)) ) continue;
+ nodesForEquality.add(child);
+ }
+
+ switch ( methodExists("hashCode", typeNode) ) {
+ case NOT_EXISTS:
+ MethodDeclaration hashCode = createHashCode(typeNode, nodesForEquality, callSuper, errorNode.get());
+ injectMethod(typeNode, hashCode);
+ break;
+ case EXISTS_BY_LOMBOK:
+ break;
+ default:
+ case EXISTS_BY_USER:
+ if ( whineIfExists ) {
+ errorNode.addWarning("Not generating hashCode(): A method with that name already exists");
+ }
+ break;
+ }
+
+ switch ( methodExists("equals", typeNode) ) {
+ case NOT_EXISTS:
+ MethodDeclaration equals = createEquals(typeNode, nodesForEquality, callSuper, errorNode.get());
+ injectMethod(typeNode, equals);
+ break;
+ case EXISTS_BY_LOMBOK:
+ break;
+ default:
+ case EXISTS_BY_USER:
+ if ( whineIfExists ) {
+ errorNode.addWarning("Not generating equals(Object other): A method with that name already exists");
+ }
+ break;
+ }
+
+ return true;
+ }
+
+ private MethodDeclaration createHashCode(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) {
+ MethodDeclaration method = new MethodDeclaration(
+ ((CompilationUnitDeclaration) type.top().get()).compilationResult);
+
+ method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
+ method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0);
+ method.annotations = new Annotation[] {
+ new MarkerAnnotation(new QualifiedTypeReference(TypeConstants.JAVA_LANG_OVERRIDE, new long[] { 0, 0, 0}), 0)
+ };
+ method.selector = "hashCode".toCharArray();
+ method.thrownExceptions = null;
+ method.typeParameters = null;
+ method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart;
+ method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = pos.sourceEnd;
+ method.arguments = null;
+
+ List<Statement> statements = new ArrayList<Statement>();
+ List<Expression> intoResult = new ArrayList<Expression>();
+
+ final char[] PRIME = "PRIME".toCharArray();
+ final char[] RESULT = "result".toCharArray();
+ final boolean isEmpty = fields.isEmpty();
+
+ /* final int PRIME = 31; */ {
+ /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */
+ if ( !isEmpty || callSuper ) {
+ LocalDeclaration primeDecl = new LocalDeclaration(PRIME, 0 ,0);
+ primeDecl.modifiers = Modifier.FINAL;
+ primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
+ primeDecl.initialization = new IntLiteral("31".toCharArray(), 0, 0);
+ statements.add(primeDecl);
+ }
+ }
+
+ /* int result = 1; */ {
+ LocalDeclaration resultDecl = new LocalDeclaration(RESULT, 0, 0);
+ resultDecl.initialization = new IntLiteral("1".toCharArray(), 0, 0);
+ resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
+ statements.add(resultDecl);
+ }
+
+ if ( callSuper ) {
+ MessageSend callToSuper = new MessageSend();
+ callToSuper.receiver = new SuperReference(0, 0);
+ callToSuper.selector = "hashCode".toCharArray();
+ intoResult.add(callToSuper);
+ }
+
+ int tempCounter = 0;
+ for ( Node field : fields ) {
+ FieldDeclaration f = (FieldDeclaration) field.get();
+ char[] token = f.type.getLastToken();
+ if ( f.type.dimensions() == 0 && token != null ) {
+ if ( Arrays.equals(TypeConstants.FLOAT, token) ) {
+ /* Float.floatToIntBits(fieldName) */
+ MessageSend floatToIntBits = new MessageSend();
+ floatToIntBits.receiver = generateQualifiedNameRef(TypeConstants.JAVA_LANG_FLOAT);
+ floatToIntBits.selector = "floatToIntBits".toCharArray();
+ floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name) };
+ intoResult.add(floatToIntBits);
+ } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) {
+ /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */
+ MessageSend doubleToLongBits = new MessageSend();
+ doubleToLongBits.receiver = generateQualifiedNameRef(TypeConstants.JAVA_LANG_DOUBLE);
+ doubleToLongBits.selector = "doubleToLongBits".toCharArray();
+ doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name) };
+ final char[] tempName = ("temp" + ++tempCounter).toCharArray();
+ LocalDeclaration tempVar = new LocalDeclaration(tempName, 0, 0);
+ tempVar.initialization = doubleToLongBits;
+ tempVar.type = TypeReference.baseTypeReference(TypeIds.T_long, 0);
+ tempVar.modifiers = Modifier.FINAL;
+ statements.add(tempVar);
+ intoResult.add(longToIntForHashCode(
+ new SingleNameReference(tempName, 0), new SingleNameReference(tempName, 0)));
+ } else if ( Arrays.equals(TypeConstants.BOOLEAN, token) ) {
+ /* booleanField ? 1231 : 1237 */
+ intoResult.add(new ConditionalExpression(
+ generateFieldReference(f.name),
+ new IntLiteral("1231".toCharArray(), 0, 0),
+ new IntLiteral("1237".toCharArray(), 0 ,0)));
+ } else if ( Arrays.equals(TypeConstants.LONG, token) ) {
+ intoResult.add(longToIntForHashCode(generateFieldReference(f.name), generateFieldReference(f.name)));
+ } else if ( BUILT_IN_TYPES.contains(new String(token)) ) {
+ intoResult.add(generateFieldReference(f.name));
+ } else /* objects */ {
+ /* this.fieldName == null ? 0 : this.fieldName.hashCode() */
+ MessageSend hashCodeCall = new MessageSend();
+ hashCodeCall.receiver = generateFieldReference(f.name);
+ hashCodeCall.selector = "hashCode".toCharArray();
+ EqualExpression objIsNull = new EqualExpression(
+ generateFieldReference(f.name),
+ new NullLiteral(0, 0),
+ OperatorIds.EQUAL_EQUAL);
+ ConditionalExpression nullOrHashCode = new ConditionalExpression(
+ objIsNull,
+ new IntLiteral("0".toCharArray(), 0, 0),
+ hashCodeCall);
+ intoResult.add(nullOrHashCode);
+ }
+ } else if ( f.type.dimensions() > 0 && token != null ) {
+ /* Arrays.deepHashCode(array) //just hashCode for simple arrays */
+ MessageSend arraysHashCodeCall = new MessageSend();
+ arraysHashCodeCall.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
+ if ( f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token)) ) {
+ arraysHashCodeCall.selector = "deepHashCode".toCharArray();
+ } else {
+ arraysHashCodeCall.selector = "hashCode".toCharArray();
+ }
+ arraysHashCodeCall.arguments = new Expression[] { generateFieldReference(f.name) };
+ intoResult.add(arraysHashCodeCall);
+ }
+ }
+
+ /* fold each intoResult entry into:
+ result = result * PRIME + (item); */ {
+ for ( Expression ex : intoResult ) {
+ BinaryExpression multiplyByPrime = new BinaryExpression(new SingleNameReference(RESULT, 0),
+ new SingleNameReference(PRIME, 0), OperatorIds.MULTIPLY);
+ BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS);
+ statements.add(new Assignment(new SingleNameReference(RESULT, 0), addItem, 0));
+ }
+ }
+
+ /* return result; */ {
+ statements.add(new ReturnStatement(new SingleNameReference(RESULT, 0), 0, 0));
+ }
+ method.statements = statements.toArray(new Statement[statements.size()]);
+ return method;
+ }
+
+ private MethodDeclaration createEquals(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) {
+ MethodDeclaration method = new MethodDeclaration(
+ ((CompilationUnitDeclaration) type.top().get()).compilationResult);
+
+ method.modifiers = PKG.toModifier(AccessLevel.PUBLIC);
+ method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
+ method.annotations = new Annotation[] {
+ new MarkerAnnotation(new QualifiedTypeReference(TypeConstants.JAVA_LANG_OVERRIDE, new long[] { 0, 0, 0}), 0)
+ };
+ method.selector = "equals".toCharArray();
+ method.thrownExceptions = null;
+ method.typeParameters = null;
+ method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
+ method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart;
+ method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = pos.sourceEnd;
+ method.arguments = new Argument[] {
+ new Argument(new char[] { 'o' }, 0,
+ new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }), 0)
+ };
+
+ List<Statement> statements = new ArrayList<Statement>();
+
+ /* if ( o == this ) return true; */ {
+ EqualExpression otherEqualsThis = new EqualExpression(
+ new SingleNameReference(new char[] { 'o' }, 0),
+ new ThisReference(0, 0), OperatorIds.EQUAL_EQUAL);
+
+ ReturnStatement returnTrue = new ReturnStatement(new TrueLiteral(0, 0), 0, 0);
+ IfStatement ifOtherEqualsThis = new IfStatement(otherEqualsThis, returnTrue, 0, 0);
+ statements.add(ifOtherEqualsThis);
+ }
+
+ /* if ( o == null ) return false; */ {
+ EqualExpression otherEqualsNull = new EqualExpression(
+ new SingleNameReference(new char[] { 'o' }, 0),
+ new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
+
+ ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ IfStatement ifOtherEqualsNull = new IfStatement(otherEqualsNull, returnFalse, 0, 0);
+ statements.add(ifOtherEqualsNull);
+ }
+
+ /* if ( o.getClass() != getClass() ) return false; */ {
+ MessageSend otherGetClass = new MessageSend();
+ otherGetClass.receiver = new SingleNameReference(new char[] { 'o' }, 0);
+ otherGetClass.selector = "getClass".toCharArray();
+ MessageSend thisGetClass = new MessageSend();
+ thisGetClass.receiver = new ThisReference(0, 0);
+ thisGetClass.selector = "getClass".toCharArray();
+ EqualExpression classesNotEqual = new EqualExpression(otherGetClass, thisGetClass, OperatorIds.NOT_EQUAL);
+ ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ IfStatement ifClassesNotEqual = new IfStatement(classesNotEqual, returnFalse, 0, 0);
+ statements.add(ifClassesNotEqual);
+ }
+
+ char[] otherN = "other".toCharArray();
+
+ /* if ( !super.equals(o) ) return false; */
+ if ( callSuper ) {
+ MessageSend callToSuper = new MessageSend();
+ callToSuper.receiver = new SuperReference(0, 0);
+ callToSuper.selector = "equals".toCharArray();
+ callToSuper.arguments = new Expression[] {new SingleNameReference(new char[] { 'o' }, 0) };
+ Expression superNotEqual = new UnaryExpression(callToSuper, OperatorIds.NOT);
+ ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ IfStatement ifSuperEquals = new IfStatement(superNotEqual, returnFalse, 0, 0);
+ statements.add(ifSuperEquals);
+ }
+
+ TypeDeclaration typeDecl = (TypeDeclaration)type.get();
+ /* MyType<?> other = (MyType<?>) o; */ {
+ if ( !fields.isEmpty() ) {
+ LocalDeclaration other = new LocalDeclaration(otherN, 0, 0);
+ char[] typeName = typeDecl.name;
+ Expression targetType;
+ if ( typeDecl.typeParameters == null || typeDecl.typeParameters.length == 0 ) {
+ targetType = new SingleNameReference(((TypeDeclaration)type.get()).name, 0);
+ other.type = new SingleTypeReference(typeName, 0);
+ } else {
+ TypeReference[] typeArgs = new TypeReference[typeDecl.typeParameters.length];
+ for ( int i = 0 ; i < typeArgs.length ; i++ ) typeArgs[i] = new Wildcard(Wildcard.UNBOUND);
+ targetType = new ParameterizedSingleTypeReference(typeName, typeArgs, 0, 0);
+ other.type = new ParameterizedSingleTypeReference(typeName, copyTypes(typeArgs), 0, 0);
+ }
+ other.initialization = new CastExpression(
+ new SingleNameReference(new char[] { 'o' }, 0),
+ targetType);
+ statements.add(other);
+ }
+ }
+
+ for ( Node field : fields ) {
+ FieldDeclaration f = (FieldDeclaration) field.get();
+ char[] token = f.type.getLastToken();
+ if ( f.type.dimensions() == 0 && token != null ) {
+ if ( Arrays.equals(TypeConstants.FLOAT, token) ) {
+ statements.add(generateCompareFloatOrDouble(otherN, "Float".toCharArray(), f.name));
+ } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) {
+ statements.add(generateCompareFloatOrDouble(otherN, "Double".toCharArray(), f.name));
+ } else if ( BUILT_IN_TYPES.contains(new String(token)) ) {
+ EqualExpression fieldsNotEqual = new EqualExpression(
+ new SingleNameReference(f.name, 0),
+ generateQualifiedNameRef(otherN, f.name),
+ OperatorIds.NOT_EQUAL);
+ ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ statements.add(new IfStatement(fieldsNotEqual, returnStatement, 0, 0));
+ } else /* objects */ {
+ EqualExpression fieldIsNull = new EqualExpression(
+ new SingleNameReference(f.name, 0),
+ new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
+ EqualExpression otherFieldIsntNull = new EqualExpression(
+ generateQualifiedNameRef(otherN, f.name),
+ new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
+ MessageSend equalsCall = new MessageSend();
+ equalsCall.receiver = new SingleNameReference(f.name, 0);
+ equalsCall.selector = "equals".toCharArray();
+ equalsCall.arguments = new Expression[] { generateQualifiedNameRef(otherN, f.name) };
+ UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT);
+ ConditionalExpression fullEquals = new ConditionalExpression(fieldIsNull, otherFieldIsntNull, fieldsNotEqual);
+ ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ statements.add(new IfStatement(fullEquals, returnStatement, 0, 0));
+ }
+ } else if ( f.type.dimensions() > 0 && token != null ) {
+ MessageSend arraysEqualCall = new MessageSend();
+ arraysEqualCall.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
+ if ( f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token)) ) {
+ arraysEqualCall.selector = "deepEquals".toCharArray();
+ } else {
+ arraysEqualCall.selector = "equals".toCharArray();
+ }
+ arraysEqualCall.arguments = new Expression[] {
+ new SingleNameReference(f.name, 0),
+ generateQualifiedNameRef(otherN, f.name) };
+ UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT);
+ ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ statements.add(new IfStatement(arraysNotEqual, returnStatement, 0, 0));
+ }
+ }
+
+ /* return true; */ {
+ statements.add(new ReturnStatement(new TrueLiteral(0, 0), 0, 0));
+ }
+ method.statements = statements.toArray(new Statement[statements.size()]);
+ return method;
+ }
+
+ private IfStatement generateCompareFloatOrDouble(char[] otherN, char[] floatOrDouble, char[] fieldName) {
+ /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false */
+ MessageSend floatCompare = new MessageSend();
+ floatCompare.receiver = generateQualifiedNameRef(TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble);
+ floatCompare.selector = "compare".toCharArray();
+ floatCompare.arguments = new Expression[] {
+ new SingleNameReference(fieldName, 0),
+ generateQualifiedNameRef(otherN, fieldName)
+ };
+ EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, new IntLiteral(new char[] {'0'}, 0, 0), OperatorIds.NOT_EQUAL);
+ ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(0, 0), 0, 0);
+ return new IfStatement(ifFloatCompareIsNot0, returnFalse, 0, 0);
+ }
+
+ /** Give 2 clones! */
+ private Expression longToIntForHashCode(Reference ref1, Reference ref2) {
+ /* (int)(ref >>> 32 ^ ref) */
+ BinaryExpression higherBits = new BinaryExpression(
+ ref1, new IntLiteral("32".toCharArray(), 0, 0),
+ OperatorIds.UNSIGNED_RIGHT_SHIFT);
+ BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR);
+ return new CastExpression(xorParts, TypeReference.baseTypeReference(TypeIds.T_int, 0));
+ }
+
+ private Reference generateFieldReference(char[] fieldName) {
+ FieldReference thisX = new FieldReference(("this." + new String(fieldName)).toCharArray(), 0);
+ thisX.receiver = new ThisReference(0, 0);
+ thisX.token = fieldName;
+ return thisX;
+ }
+
+ private NameReference generateQualifiedNameRef(char[]... varNames) {
+ if ( varNames.length > 1 )
+ return new QualifiedNameReference(varNames, new long[varNames.length], 0, 0);
+ else return new SingleNameReference(varNames[0], 0);
+ }
+}
diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java
index f4d1c754..003b8808 100644
--- a/src/lombok/javac/handlers/HandleData.java
+++ b/src/lombok/javac/handlers/HandleData.java
@@ -21,27 +21,23 @@
*/
package lombok.javac.handlers;
-import java.lang.reflect.Modifier;
-
import static lombok.javac.handlers.PKG.*;
+import java.lang.reflect.Modifier;
+
import lombok.Data;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacAST.Node;
+import lombok.javac.handlers.PKG.MemberExistsResult;
import org.mangosdk.spi.ProviderFor;
-import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Flags;
-import com.sun.tools.javac.code.TypeTags;
-import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
-import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.JCTree.JCAssign;
-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.JCExpression;
@@ -49,14 +45,12 @@ 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.JCModifiers;
-import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;
import com.sun.tools.javac.tree.JCTree.JCReturn;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
-import com.sun.tools.javac.util.Name;
/**
* Handles the <code>lombok.Data</code> annotation for javac.
@@ -91,16 +85,7 @@ public class HandleData implements JavacAnnotationHandler<Data> {
}
new HandleToString().generateToStringForType(typeNode, annotationNode);
-
- if ( methodExists("equals", typeNode) == MemberExistsResult.NOT_EXISTS ) {
- JCMethodDecl method = createEquals(typeNode, nodesForEquality);
- injectMethod(typeNode, method);
- }
-
- if ( methodExists("hashCode", typeNode) == MemberExistsResult.NOT_EXISTS ) {
- JCMethodDecl method = createHashCode(typeNode, nodesForEquality);
- injectMethod(typeNode, method);
- }
+ new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
String staticConstructorName = annotation.getInstance().staticConstructor();
@@ -117,234 +102,6 @@ public class HandleData implements JavacAnnotationHandler<Data> {
return true;
}
- private JCMethodDecl createHashCode(Node typeNode, List<Node> fields) {
- TreeMaker maker = typeNode.getTreeMaker();
-
- JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
- JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
- JCExpression returnType = maker.TypeIdent(TypeTags.INT);
- List<JCStatement> statements = List.nil();
-
- Name primeName = typeNode.toName("PRIME");
- Name resultName = typeNode.toName("result");
- /* final int PRIME = 31; */ {
- if ( !fields.isEmpty() ) {
- statements = statements.append(
- maker.VarDef(maker.Modifiers(Flags.FINAL), primeName, maker.TypeIdent(TypeTags.INT), maker.Literal(31)));
- }
- }
-
- /* int result = 1; */ {
- statements = statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(TypeTags.INT), maker.Literal(1)));
- }
-
- List<JCExpression> intoResult = List.nil();
-
- int tempCounter = 0;
- for ( Node fieldNode : fields ) {
- JCVariableDecl field = (JCVariableDecl) fieldNode.get();
- JCExpression fType = field.vartype;
- JCExpression thisDotField = maker.Select(maker.Ident(typeNode.toName("this")), field.name);
- JCExpression thisDotFieldClone = maker.Select(maker.Ident(typeNode.toName("this")), field.name);
- if ( fType instanceof JCPrimitiveTypeTree ) {
- switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) {
- case BOOLEAN:
- /* this.fieldName ? 1231 : 1237 */
- intoResult = intoResult.append(maker.Conditional(thisDotField, maker.Literal(1231), maker.Literal(1237)));
- break;
- case LONG:
- intoResult = intoResult.append(longToIntForHashCode(maker, thisDotField, thisDotFieldClone));
- break;
- case FLOAT:
- /* Float.floatToIntBits(this.fieldName) */
- intoResult = intoResult.append(maker.Apply(
- List.<JCExpression>nil(),
- chainDots(maker, typeNode, "java", "lang", "Float", "floatToIntBits"),
- List.of(thisDotField)));
- break;
- case DOUBLE:
- /* longToIntForHashCode(Double.doubleToLongBits(this.fieldName)) */
- Name tempVar = typeNode.toName("temp" + (++tempCounter));
- JCExpression init = maker.Apply(
- List.<JCExpression>nil(),
- chainDots(maker, typeNode, "java", "lang", "Double", "doubleToLongBits"),
- List.of(thisDotField));
- statements = statements.append(
- maker.VarDef(maker.Modifiers(Flags.FINAL), tempVar, maker.TypeIdent(TypeTags.LONG), init));
- intoResult = intoResult.append(longToIntForHashCode(maker, maker.Ident(tempVar), maker.Ident(tempVar)));
- break;
- default:
- case BYTE:
- case SHORT:
- case INT:
- case CHAR:
- /* just the field */
- intoResult = intoResult.append(thisDotField);
- break;
- }
- } else if ( fType instanceof JCArrayTypeTree ) {
- /* 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 useDeepHC = multiDim || !primitiveArray;
-
- JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode");
- intoResult = intoResult.append(
- maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(thisDotField)));
- } else /* objects */ {
- /* this.fieldName == null ? 0 : this.fieldName.hashCode() */
- JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(thisDotField, typeNode.toName("hashCode")),
- List.<JCExpression>nil());
- JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, thisDotField, maker.Literal(TypeTags.BOT, null));
- intoResult = intoResult.append(
- maker.Conditional(thisEqualsNull, maker.Literal(0), hcCall));
- }
- }
-
- /* fold each intoResult entry into:
- result = result * PRIME + (item); */
- for ( JCExpression expr : intoResult ) {
- JCExpression mult = maker.Binary(JCTree.MUL, maker.Ident(resultName), maker.Ident(primeName));
- JCExpression add = maker.Binary(JCTree.PLUS, mult, expr);
- statements = statements.append(maker.Exec(maker.Assign(maker.Ident(resultName), add)));
- }
-
- /* return result; */ {
- statements = statements.append(maker.Return(maker.Ident(resultName)));
- }
-
- JCBlock body = maker.Block(0, statements);
- return maker.MethodDef(mods, typeNode.toName("hashCode"), returnType,
- List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
- }
-
- /** The 2 references must be clones of each other. */
- private JCExpression longToIntForHashCode(TreeMaker maker, JCExpression ref1, JCExpression ref2) {
- /* (int)(ref >>> 32 ^ ref) */
- JCExpression shift = maker.Binary(JCTree.USR, ref1, maker.Literal(32));
- JCExpression xorBits = maker.Binary(JCTree.BITXOR, shift, ref2);
- return maker.TypeCast(maker.TypeIdent(TypeTags.INT), xorBits);
- }
-
- private JCMethodDecl createEquals(Node typeNode, List<Node> fields) {
- TreeMaker maker = typeNode.getTreeMaker();
- JCClassDecl type = (JCClassDecl) typeNode.get();
-
- Name oName = typeNode.toName("o");
- Name otherName = typeNode.toName("other");
- Name thisName = typeNode.toName("this");
-
- JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
- JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
- JCExpression objectType = maker.Type(typeNode.getSymbolTable().objectType);
- JCExpression returnType = maker.TypeIdent(TypeTags.BOOLEAN);
-
- List<JCStatement> statements = List.nil();
- List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(0), oName, objectType, null));
-
- /* if ( o == this ) return true; */ {
- statements = statements.append(
- maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Ident(thisName)), returnBool(maker, true), null));
- }
-
- /* if ( o == null ) return false; */ {
- statements = statements.append(
- maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Literal(TypeTags.BOT, null)), returnBool(maker, false), null));
- }
-
- /* if ( o.getClass() != this.getClass() ) return false; */ {
- Name getClass = typeNode.toName("getClass");
- List<JCExpression> exprNil = List.nil();
- JCExpression oGetClass = maker.Apply(exprNil, maker.Select(maker.Ident(oName), getClass), exprNil);
- JCExpression thisGetClass = maker.Apply(exprNil, maker.Select(maker.Ident(thisName), getClass), exprNil);
- statements = statements.append(
- maker.If(maker.Binary(JCTree.NE, oGetClass, thisGetClass), returnBool(maker, false), null));
- }
-
- /* MyType<?> other = (MyType<?>) o; */ {
- final JCExpression selfType1, selfType2;
- List<JCExpression> wildcards1 = List.nil();
- List<JCExpression> wildcards2 = List.nil();
- for ( int i = 0 ; i < type.typarams.length() ; i++ ) {
- wildcards1 = wildcards1.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
- wildcards2 = wildcards2.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
- }
-
- if ( type.typarams.isEmpty() ) {
- selfType1 = maker.Ident(type.name);
- selfType2 = maker.Ident(type.name);
- } else {
- selfType1 = maker.TypeApply(maker.Ident(type.name), wildcards1);
- selfType2 = maker.TypeApply(maker.Ident(type.name), wildcards2);
- }
-
- statements = statements.append(
- maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, selfType1, maker.TypeCast(selfType2, maker.Ident(oName))));
- }
-
- for ( Node fieldNode : fields ) {
- JCVariableDecl field = (JCVariableDecl) fieldNode.get();
- JCExpression fType = field.vartype;
- JCExpression thisDotField = maker.Select(maker.Ident(thisName), field.name);
- JCExpression otherDotField = maker.Select(maker.Ident(otherName), field.name);
- if ( fType instanceof JCPrimitiveTypeTree ) {
- switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) {
- case FLOAT:
- /* if ( Float.compare(this.fieldName, other.fieldName) != 0 ) return false; */
- statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, false));
- break;
- case DOUBLE:
- /* if ( Double(this.fieldName, other.fieldName) != 0 ) return false; */
- statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, true));
- break;
- default:
- /* if ( this.fieldName != other.fieldName ) return false; */
- statements = statements.append(
- maker.If(maker.Binary(JCTree.NE, thisDotField, otherDotField), returnBool(maker, false), null));
- break;
- }
- } else if ( fType instanceof JCArrayTypeTree ) {
- /* 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 useDeepEquals = multiDim || !primitiveArray;
-
- JCExpression eqMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepEquals ? "deepEquals" : "equals");
- List<JCExpression> args = List.of(thisDotField, otherDotField);
- statements = statements.append(maker.If(maker.Unary(JCTree.NOT,
- maker.Apply(List.<JCExpression>nil(), eqMethod, args)), returnBool(maker, false), null));
- } else /* objects */ {
- /* if ( this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName) ) return false; */
- JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, thisDotField, maker.Literal(TypeTags.BOT, null));
- JCExpression otherNotEqualsNull = maker.Binary(JCTree.NE, otherDotField, maker.Literal(TypeTags.BOT, null));
- JCExpression thisEqualsThat = maker.Apply(
- List.<JCExpression>nil(), maker.Select(thisDotField, typeNode.toName("equals")), List.of(otherDotField));
- JCExpression fieldsAreNotEqual = maker.Conditional(thisEqualsNull, otherNotEqualsNull, maker.Unary(JCTree.NOT, thisEqualsThat));
- statements = statements.append(maker.If(fieldsAreNotEqual, returnBool(maker, false), null));
- }
- }
-
- /* return true; */ {
- statements = statements.append(returnBool(maker, true));
- }
-
- JCBlock body = maker.Block(0, statements);
- return maker.MethodDef(mods, typeNode.toName("equals"), returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null);
- }
-
- private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, TreeMaker maker, Node node, boolean isDouble) {
- /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false; */
- JCExpression clazz = chainDots(maker, node, "java", "lang", isDouble ? "Double" : "Float");
- List<JCExpression> args = List.of(thisDotField, otherDotField);
- JCBinary compareCallEquals0 = maker.Binary(JCTree.NE, maker.Apply(
- List.<JCExpression>nil(), maker.Select(clazz, node.toName("compare")), args), maker.Literal(0));
- return maker.If(compareCallEquals0, returnBool(maker, false), null);
- }
-
- private JCStatement returnBool(TreeMaker maker, boolean bool) {
- return maker.Return(maker.Literal(TypeTags.BOOLEAN, bool ? 1 : 0));
- }
-
private JCMethodDecl createConstructor(boolean isPublic, Node typeNode, List<Node> fields) {
TreeMaker maker = typeNode.getTreeMaker();
JCClassDecl type = (JCClassDecl) typeNode.get();
diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java
new file mode 100644
index 00000000..1a1158eb
--- /dev/null
+++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -0,0 +1,429 @@
+/*
+ * Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.javac.handlers;
+
+import static lombok.javac.handlers.PKG.*;
+
+import lombok.EqualsAndHashCode;
+import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
+import lombok.javac.Javac;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacAST.Node;
+
+import org.mangosdk.spi.ProviderFor;
+
+import com.sun.tools.javac.code.BoundKind;
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.code.TypeTags;
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.TreeMaker;
+import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+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.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
+import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
+import com.sun.tools.javac.tree.JCTree.JCModifiers;
+import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;
+import com.sun.tools.javac.tree.JCTree.JCStatement;
+import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
+import com.sun.tools.javac.tree.JCTree.JCUnary;
+import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Name;
+
+/**
+ * Handles the <code>lombok.EqualsAndHashCode</code> annotation for javac.
+ */
+@ProviderFor(JavacAnnotationHandler.class)
+public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAndHashCode> {
+ private void checkForBogusExcludes(Node type, AnnotationValues<EqualsAndHashCode> annotation) {
+ List<String> list = List.from(annotation.getInstance().exclude());
+ boolean[] matched = new boolean[list.size()];
+
+ for ( Node child : type.down() ) {
+ if ( list.isEmpty() ) break;
+ if ( child.getKind() != Kind.FIELD ) continue;
+ if ( (((JCVariableDecl)child.get()).mods.flags & Flags.STATIC) != 0 ) continue;
+ if ( (((JCVariableDecl)child.get()).mods.flags & Flags.TRANSIENT) != 0 ) continue;
+ int idx = list.indexOf(child.getName());
+ if ( idx > -1 ) matched[idx] = true;
+ }
+
+ for ( int i = 0 ; i < list.size() ; i++ ) {
+ if ( !matched[i] ) {
+ annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i);
+ }
+ }
+ }
+
+ @Override public boolean handle(AnnotationValues<EqualsAndHashCode> annotation, JCAnnotation ast, Node annotationNode) {
+ EqualsAndHashCode ann = annotation.getInstance();
+ List<String> excludes = List.from(ann.exclude());
+ Node typeNode = annotationNode.up();
+
+ checkForBogusExcludes(typeNode, annotation);
+
+ return generateMethods(typeNode, annotationNode, excludes, ann.callSuper(), true);
+ }
+
+ public void generateEqualsAndHashCodeForType(Node typeNode, Node errorNode) {
+ for ( Node child : typeNode.down() ) {
+ if ( child.getKind() == Kind.ANNOTATION ) {
+ if ( Javac.annotationTypeMatches(EqualsAndHashCode.class, child) ) {
+ //The annotation will make it happen, so we can skip it.
+ return;
+ }
+ }
+ }
+
+ boolean callSuper = false;
+ try {
+ callSuper = ((Boolean)EqualsAndHashCode.class.getMethod("callSuper").getDefaultValue()).booleanValue();
+ } catch ( Exception ignore ) {}
+ generateMethods(typeNode, errorNode, List.<String>nil(), callSuper, false);
+ }
+
+ private boolean generateMethods(Node typeNode, Node errorNode, List<String> excludes,
+ boolean callSuper, boolean whineIfExists) {
+ boolean notAClass = true;
+ if ( typeNode.get() instanceof JCClassDecl ) {
+ long flags = ((JCClassDecl)typeNode.get()).mods.flags;
+ notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
+ }
+
+ if ( notAClass ) {
+ errorNode.addError("@EqualsAndHashCode is only supported on a class.");
+ return false;
+ }
+
+ boolean isDirectDescendentOfObject = true;
+
+ JCTree extending = ((JCClassDecl)typeNode.get()).extending;
+ if ( extending != null ) {
+ String p = extending.toString();
+ isDirectDescendentOfObject = p.equals("Object") || p.equals("java.lang.Object");
+ }
+
+ if ( isDirectDescendentOfObject && callSuper ) {
+ errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless.");
+ return true;
+ }
+
+ if ( !isDirectDescendentOfObject && !callSuper ) {
+ errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object.");
+ }
+
+ List<Node> nodesForEquality = List.nil();
+ for ( Node child : typeNode.down() ) {
+ if ( child.getKind() != Kind.FIELD ) continue;
+ JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
+ //Skip static fields.
+ if ( (fieldDecl.mods.flags & Flags.STATIC) != 0 ) continue;
+ //Skip transient fields.
+ if ( (fieldDecl.mods.flags & Flags.TRANSIENT) != 0 ) continue;
+ //Skip excluded fields.
+ if ( excludes.contains(fieldDecl.name.toString()) ) continue;
+ nodesForEquality = nodesForEquality.append(child);
+ }
+
+ switch ( methodExists("hashCode", typeNode) ) {
+ case NOT_EXISTS:
+ JCMethodDecl method = createHashCode(typeNode, nodesForEquality, callSuper);
+ injectMethod(typeNode, method);
+ break;
+ case EXISTS_BY_LOMBOK:
+ break;
+ default:
+ case EXISTS_BY_USER:
+ if ( whineIfExists ) {
+ errorNode.addWarning("Not generating hashCode(): A method with that name already exists");
+ }
+ break;
+ }
+
+ switch ( methodExists("equals", typeNode) ) {
+ case NOT_EXISTS:
+ JCMethodDecl method = createEquals(typeNode, nodesForEquality, callSuper);
+ injectMethod(typeNode, method);
+ break;
+ case EXISTS_BY_LOMBOK:
+ break;
+ default:
+ case EXISTS_BY_USER:
+ if ( whineIfExists ) {
+ errorNode.addWarning("Not generating equals(Object other): A method with that name already exists");
+ }
+ break;
+ }
+
+ return true;
+ }
+
+ private JCMethodDecl createHashCode(Node typeNode, List<Node> fields, boolean callSuper) {
+ TreeMaker maker = typeNode.getTreeMaker();
+
+ JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
+ JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
+ JCExpression returnType = maker.TypeIdent(TypeTags.INT);
+ List<JCStatement> statements = List.nil();
+
+ Name primeName = typeNode.toName("PRIME");
+ Name resultName = typeNode.toName("result");
+ /* final int PRIME = 31; */ {
+ if ( !fields.isEmpty() || callSuper ) {
+ statements = statements.append(
+ maker.VarDef(maker.Modifiers(Flags.FINAL), primeName, maker.TypeIdent(TypeTags.INT), maker.Literal(31)));
+ }
+ }
+
+ /* int result = 1; */ {
+ statements = statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(TypeTags.INT), maker.Literal(1)));
+ }
+
+ List<JCExpression> intoResult = List.nil();
+
+ if ( callSuper ) {
+ JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(),
+ maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")),
+ List.<JCExpression>nil());
+ intoResult = intoResult.append(callToSuper);
+ }
+
+ int tempCounter = 0;
+ for ( Node fieldNode : fields ) {
+ JCVariableDecl field = (JCVariableDecl) fieldNode.get();
+ JCExpression fType = field.vartype;
+ JCExpression thisDotField = maker.Select(maker.Ident(typeNode.toName("this")), field.name);
+ JCExpression thisDotFieldClone = maker.Select(maker.Ident(typeNode.toName("this")), field.name);
+ if ( fType instanceof JCPrimitiveTypeTree ) {
+ switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) {
+ case BOOLEAN:
+ /* this.fieldName ? 1231 : 1237 */
+ intoResult = intoResult.append(maker.Conditional(thisDotField, maker.Literal(1231), maker.Literal(1237)));
+ break;
+ case LONG:
+ intoResult = intoResult.append(longToIntForHashCode(maker, thisDotField, thisDotFieldClone));
+ break;
+ case FLOAT:
+ /* Float.floatToIntBits(this.fieldName) */
+ intoResult = intoResult.append(maker.Apply(
+ List.<JCExpression>nil(),
+ chainDots(maker, typeNode, "java", "lang", "Float", "floatToIntBits"),
+ List.of(thisDotField)));
+ break;
+ case DOUBLE:
+ /* longToIntForHashCode(Double.doubleToLongBits(this.fieldName)) */
+ Name tempVar = typeNode.toName("temp" + (++tempCounter));
+ JCExpression init = maker.Apply(
+ List.<JCExpression>nil(),
+ chainDots(maker, typeNode, "java", "lang", "Double", "doubleToLongBits"),
+ List.of(thisDotField));
+ statements = statements.append(
+ maker.VarDef(maker.Modifiers(Flags.FINAL), tempVar, maker.TypeIdent(TypeTags.LONG), init));
+ intoResult = intoResult.append(longToIntForHashCode(maker, maker.Ident(tempVar), maker.Ident(tempVar)));
+ break;
+ default:
+ case BYTE:
+ case SHORT:
+ case INT:
+ case CHAR:
+ /* just the field */
+ intoResult = intoResult.append(thisDotField);
+ break;
+ }
+ } else if ( fType instanceof JCArrayTypeTree ) {
+ /* 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 useDeepHC = multiDim || !primitiveArray;
+
+ JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode");
+ intoResult = intoResult.append(
+ maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(thisDotField)));
+ } else /* objects */ {
+ /* this.fieldName == null ? 0 : this.fieldName.hashCode() */
+ JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(thisDotField, typeNode.toName("hashCode")),
+ List.<JCExpression>nil());
+ JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, thisDotField, maker.Literal(TypeTags.BOT, null));
+ intoResult = intoResult.append(
+ maker.Conditional(thisEqualsNull, maker.Literal(0), hcCall));
+ }
+ }
+
+ /* fold each intoResult entry into:
+ result = result * PRIME + (item); */
+ for ( JCExpression expr : intoResult ) {
+ JCExpression mult = maker.Binary(JCTree.MUL, maker.Ident(resultName), maker.Ident(primeName));
+ JCExpression add = maker.Binary(JCTree.PLUS, mult, expr);
+ statements = statements.append(maker.Exec(maker.Assign(maker.Ident(resultName), add)));
+ }
+
+ /* return result; */ {
+ statements = statements.append(maker.Return(maker.Ident(resultName)));
+ }
+
+ JCBlock body = maker.Block(0, statements);
+ return maker.MethodDef(mods, typeNode.toName("hashCode"), returnType,
+ List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
+ }
+
+ /** The 2 references must be clones of each other. */
+ private JCExpression longToIntForHashCode(TreeMaker maker, JCExpression ref1, JCExpression ref2) {
+ /* (int)(ref >>> 32 ^ ref) */
+ JCExpression shift = maker.Binary(JCTree.USR, ref1, maker.Literal(32));
+ JCExpression xorBits = maker.Binary(JCTree.BITXOR, shift, ref2);
+ return maker.TypeCast(maker.TypeIdent(TypeTags.INT), xorBits);
+ }
+
+ private JCMethodDecl createEquals(Node typeNode, List<Node> fields, boolean callSuper) {
+ TreeMaker maker = typeNode.getTreeMaker();
+ JCClassDecl type = (JCClassDecl) typeNode.get();
+
+ Name oName = typeNode.toName("o");
+ Name otherName = typeNode.toName("other");
+ Name thisName = typeNode.toName("this");
+
+ JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
+ JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
+ JCExpression objectType = maker.Type(typeNode.getSymbolTable().objectType);
+ JCExpression returnType = maker.TypeIdent(TypeTags.BOOLEAN);
+
+ List<JCStatement> statements = List.nil();
+ List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(0), oName, objectType, null));
+
+ /* if ( o == this ) return true; */ {
+ statements = statements.append(
+ maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Ident(thisName)), returnBool(maker, true), null));
+ }
+
+ /* if ( o == null ) return false; */ {
+ statements = statements.append(
+ maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName), maker.Literal(TypeTags.BOT, null)), returnBool(maker, false), null));
+ }
+
+ /* if ( o.getClass() != this.getClass() ) return false; */ {
+ Name getClass = typeNode.toName("getClass");
+ List<JCExpression> exprNil = List.nil();
+ JCExpression oGetClass = maker.Apply(exprNil, maker.Select(maker.Ident(oName), getClass), exprNil);
+ JCExpression thisGetClass = maker.Apply(exprNil, maker.Select(maker.Ident(thisName), getClass), exprNil);
+ statements = statements.append(
+ maker.If(maker.Binary(JCTree.NE, oGetClass, thisGetClass), returnBool(maker, false), null));
+ }
+
+ /* if ( !super.equals(o) ) return false; */
+ if ( callSuper ) {
+ JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(),
+ maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("equals")),
+ List.<JCExpression>of(maker.Ident(oName)));
+ JCUnary superNotEqual = maker.Unary(JCTree.NOT, callToSuper);
+ statements = statements.append(maker.If(superNotEqual, returnBool(maker, false), null));
+ }
+
+ /* MyType<?> other = (MyType<?>) o; */ {
+ final JCExpression selfType1, selfType2;
+ List<JCExpression> wildcards1 = List.nil();
+ List<JCExpression> wildcards2 = List.nil();
+ for ( int i = 0 ; i < type.typarams.length() ; i++ ) {
+ wildcards1 = wildcards1.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
+ wildcards2 = wildcards2.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
+ }
+
+ if ( type.typarams.isEmpty() ) {
+ selfType1 = maker.Ident(type.name);
+ selfType2 = maker.Ident(type.name);
+ } else {
+ selfType1 = maker.TypeApply(maker.Ident(type.name), wildcards1);
+ selfType2 = maker.TypeApply(maker.Ident(type.name), wildcards2);
+ }
+
+ statements = statements.append(
+ maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, selfType1, maker.TypeCast(selfType2, maker.Ident(oName))));
+ }
+
+ for ( Node fieldNode : fields ) {
+ JCVariableDecl field = (JCVariableDecl) fieldNode.get();
+ JCExpression fType = field.vartype;
+ JCExpression thisDotField = maker.Select(maker.Ident(thisName), field.name);
+ JCExpression otherDotField = maker.Select(maker.Ident(otherName), field.name);
+ if ( fType instanceof JCPrimitiveTypeTree ) {
+ switch ( ((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind() ) {
+ case FLOAT:
+ /* if ( Float.compare(this.fieldName, other.fieldName) != 0 ) return false; */
+ statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, false));
+ break;
+ case DOUBLE:
+ /* if ( Double(this.fieldName, other.fieldName) != 0 ) return false; */
+ statements = statements.append(generateCompareFloatOrDouble(thisDotField, otherDotField, maker, typeNode, true));
+ break;
+ default:
+ /* if ( this.fieldName != other.fieldName ) return false; */
+ statements = statements.append(
+ maker.If(maker.Binary(JCTree.NE, thisDotField, otherDotField), returnBool(maker, false), null));
+ break;
+ }
+ } else if ( fType instanceof JCArrayTypeTree ) {
+ /* 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 useDeepEquals = multiDim || !primitiveArray;
+
+ JCExpression eqMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepEquals ? "deepEquals" : "equals");
+ List<JCExpression> args = List.of(thisDotField, otherDotField);
+ statements = statements.append(maker.If(maker.Unary(JCTree.NOT,
+ maker.Apply(List.<JCExpression>nil(), eqMethod, args)), returnBool(maker, false), null));
+ } else /* objects */ {
+ /* if ( this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName) ) return false; */
+ JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, thisDotField, maker.Literal(TypeTags.BOT, null));
+ JCExpression otherNotEqualsNull = maker.Binary(JCTree.NE, otherDotField, maker.Literal(TypeTags.BOT, null));
+ JCExpression thisEqualsThat = maker.Apply(
+ List.<JCExpression>nil(), maker.Select(thisDotField, typeNode.toName("equals")), List.of(otherDotField));
+ JCExpression fieldsAreNotEqual = maker.Conditional(thisEqualsNull, otherNotEqualsNull, maker.Unary(JCTree.NOT, thisEqualsThat));
+ statements = statements.append(maker.If(fieldsAreNotEqual, returnBool(maker, false), null));
+ }
+ }
+
+ /* return true; */ {
+ statements = statements.append(returnBool(maker, true));
+ }
+
+ JCBlock body = maker.Block(0, statements);
+ return maker.MethodDef(mods, typeNode.toName("equals"), returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null);
+ }
+
+ private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, TreeMaker maker, Node node, boolean isDouble) {
+ /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false; */
+ JCExpression clazz = chainDots(maker, node, "java", "lang", isDouble ? "Double" : "Float");
+ List<JCExpression> args = List.of(thisDotField, otherDotField);
+ JCBinary compareCallEquals0 = maker.Binary(JCTree.NE, maker.Apply(
+ List.<JCExpression>nil(), maker.Select(clazz, node.toName("compare")), args), maker.Literal(0));
+ return maker.If(compareCallEquals0, returnBool(maker, false), null);
+ }
+
+ private JCStatement returnBool(TreeMaker maker, boolean bool) {
+ return maker.Return(maker.Literal(TypeTags.BOOLEAN, bool ? 1 : 0));
+ }
+
+}