diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-23 07:44:53 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-23 07:44:53 +0200 |
commit | 35691e83edffdadd5ef438793eec9c968e8bfd35 (patch) | |
tree | 203470348ad9204cbba9b47aa80521093ec45c96 /src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | |
parent | 21717cec11d5a8abdc3eba280290a65103bbeaf7 (diff) | |
download | lombok-35691e83edffdadd5ef438793eec9c968e8bfd35.tar.gz lombok-35691e83edffdadd5ef438793eec9c968e8bfd35.tar.bz2 lombok-35691e83edffdadd5ef438793eec9c968e8bfd35.zip |
Massive change to the eclipse handlers: They now set the 'generatedBy' flag which we can use to patch eclipse in specific places to ignore generated nodes.
Diffstat (limited to 'src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java')
-rw-r--r-- | src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 365 |
1 files changed, 263 insertions, 102 deletions
diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 46792236..209c095c 100644 --- a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -234,22 +234,24 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA return true; } - private MethodDeclaration createHashCode(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) { - int pS = pos.sourceStart, pE = pos.sourceEnd; + private MethodDeclaration createHashCode(Node type, Collection<Node> fields, boolean callSuper, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); + Eclipse.setGeneratedBy(method, source); method.modifiers = PKG.toModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0); - method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, p)}; + Eclipse.setGeneratedBy(method.returnType, source); + method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source)}; 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.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; + method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; method.arguments = null; List<Statement> statements = new ArrayList<Statement>(); @@ -263,23 +265,34 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */ if ( !isEmpty || callSuper ) { LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE); + Eclipse.setGeneratedBy(primeDecl, source); primeDecl.modifiers |= Modifier.FINAL; primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); + primeDecl.type.sourceStart = pS; primeDecl.type.sourceEnd = pE; + Eclipse.setGeneratedBy(primeDecl.type, source); primeDecl.initialization = new IntLiteral("31".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(primeDecl.initialization, source); statements.add(primeDecl); } } /* int result = 1; */ { LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); + Eclipse.setGeneratedBy(resultDecl, source); resultDecl.initialization = new IntLiteral("1".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(resultDecl.initialization, source); resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); + resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; + Eclipse.setGeneratedBy(resultDecl.type, source); statements.add(resultDecl); } if ( callSuper ) { MessageSend callToSuper = new MessageSend(); + Eclipse.setGeneratedBy(callToSuper, source); + callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; callToSuper.receiver = new SuperReference(pS, pE); + Eclipse.setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "hashCode".toCharArray(); intoResult.add(callToSuper); } @@ -292,59 +305,79 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA if ( Arrays.equals(TypeConstants.FLOAT, token) ) { /* Float.floatToIntBits(fieldName) */ MessageSend floatToIntBits = new MessageSend(); - floatToIntBits.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA_LANG_FLOAT); + floatToIntBits.sourceStart = pS; floatToIntBits.sourceEnd = pE; + Eclipse.setGeneratedBy(floatToIntBits, source); + floatToIntBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_FLOAT); floatToIntBits.selector = "floatToIntBits".toCharArray(); - floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name, p) }; + floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name, source) }; intoResult.add(floatToIntBits); } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) { /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */ MessageSend doubleToLongBits = new MessageSend(); - doubleToLongBits.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA_LANG_DOUBLE); + doubleToLongBits.sourceStart = pS; doubleToLongBits.sourceEnd = pE; + Eclipse.setGeneratedBy(doubleToLongBits, source); + doubleToLongBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_DOUBLE); doubleToLongBits.selector = "doubleToLongBits".toCharArray(); - doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name, p) }; + doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name, source) }; final char[] tempName = ("temp" + ++tempCounter).toCharArray(); LocalDeclaration tempVar = new LocalDeclaration(tempName, pS, pE); + Eclipse.setGeneratedBy(tempVar, source); tempVar.initialization = doubleToLongBits; tempVar.type = TypeReference.baseTypeReference(TypeIds.T_long, 0); + tempVar.type.sourceStart = pS; tempVar.type.sourceEnd = pE; + Eclipse.setGeneratedBy(tempVar.type, source); tempVar.modifiers = Modifier.FINAL; statements.add(tempVar); - intoResult.add(longToIntForHashCode( - new SingleNameReference(tempName, p), new SingleNameReference(tempName, p), p)); + SingleNameReference copy1 = new SingleNameReference(tempName, p); + Eclipse.setGeneratedBy(copy1, source); + SingleNameReference copy2 = new SingleNameReference(tempName, p); + Eclipse.setGeneratedBy(copy2, source); + intoResult.add(longToIntForHashCode(copy1, copy2, source)); } else if ( Arrays.equals(TypeConstants.BOOLEAN, token) ) { /* booleanField ? 1231 : 1237 */ - intoResult.add(new ConditionalExpression( - generateFieldReference(f.name, p), - new IntLiteral("1231".toCharArray(), pS, pE), - new IntLiteral("1237".toCharArray(), pS, pE))); + IntLiteral int1231 = new IntLiteral("1231".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int1231, source); + IntLiteral int1237 = new IntLiteral("1237".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int1237, source); + ConditionalExpression int1231or1237 = new ConditionalExpression( + generateFieldReference(f.name, source), int1231, int1237); + Eclipse.setGeneratedBy(int1231or1237, source); + intoResult.add(int1231or1237); } else if ( Arrays.equals(TypeConstants.LONG, token) ) { - intoResult.add(longToIntForHashCode(generateFieldReference(f.name, p), generateFieldReference(f.name, p), p)); + intoResult.add(longToIntForHashCode(generateFieldReference(f.name, source), generateFieldReference(f.name, source), source)); } else if ( BUILT_IN_TYPES.contains(new String(token)) ) { - intoResult.add(generateFieldReference(f.name, p)); + intoResult.add(generateFieldReference(f.name, source)); } else /* objects */ { /* this.fieldName == null ? 0 : this.fieldName.hashCode() */ MessageSend hashCodeCall = new MessageSend(); - hashCodeCall.receiver = generateFieldReference(f.name, p); + hashCodeCall.sourceStart = pS; hashCodeCall.sourceEnd = pE; + Eclipse.setGeneratedBy(hashCodeCall, source); + hashCodeCall.receiver = generateFieldReference(f.name, source); hashCodeCall.selector = "hashCode".toCharArray(); + NullLiteral nullLiteral = new NullLiteral(pS, pE); + Eclipse.setGeneratedBy(nullLiteral, source); EqualExpression objIsNull = new EqualExpression( - generateFieldReference(f.name, p), - new NullLiteral(pS, pE), - OperatorIds.EQUAL_EQUAL); - ConditionalExpression nullOrHashCode = new ConditionalExpression( - objIsNull, - new IntLiteral("0".toCharArray(), pS, pE), - hashCodeCall); + generateFieldReference(f.name, source), nullLiteral, OperatorIds.EQUAL_EQUAL); + Eclipse.setGeneratedBy(objIsNull, source); + IntLiteral int0 = new IntLiteral("0".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int0, source); + ConditionalExpression nullOrHashCode = new ConditionalExpression(objIsNull, int0, hashCodeCall); + nullOrHashCode.sourceStart = pS; nullOrHashCode.sourceEnd = pE; + Eclipse.setGeneratedBy(nullOrHashCode, source); 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(p, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); + arraysHashCodeCall.sourceStart = pS; arraysHashCodeCall.sourceEnd = pE; + Eclipse.setGeneratedBy(arraysHashCodeCall, source); + arraysHashCodeCall.receiver = generateQualifiedNameRef(source, 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, p) }; + arraysHashCodeCall.arguments = new Expression[] { generateFieldReference(f.name, source) }; intoResult.add(arraysHashCodeCall); } } @@ -352,73 +385,117 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* fold each intoResult entry into: result = result * PRIME + (item); */ { for ( Expression ex : intoResult ) { - BinaryExpression multiplyByPrime = new BinaryExpression(new SingleNameReference(RESULT, p), - new SingleNameReference(PRIME, 0), OperatorIds.MULTIPLY); + SingleNameReference resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + SingleNameReference primeRef = new SingleNameReference(PRIME, p); + Eclipse.setGeneratedBy(primeRef, source); + BinaryExpression multiplyByPrime = new BinaryExpression(resultRef, primeRef, OperatorIds.MULTIPLY); + multiplyByPrime.sourceStart = pS; multiplyByPrime.sourceEnd = pE; + Eclipse.setGeneratedBy(multiplyByPrime, source); BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS); - statements.add(new Assignment(new SingleNameReference(RESULT, p), addItem, pE)); + addItem.sourceStart = pS; addItem.sourceEnd = pE; + Eclipse.setGeneratedBy(addItem, source); + resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + Assignment assignment = new Assignment(resultRef, addItem, pE); + assignment.sourceStart = pS; assignment.sourceEnd = pE; + Eclipse.setGeneratedBy(assignment, source); + statements.add(assignment); } } /* return result; */ { - statements.add(new ReturnStatement(new SingleNameReference(RESULT, p), pS, pE)); + SingleNameReference resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + ReturnStatement returnStatement = new ReturnStatement(resultRef, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + statements.add(returnStatement); } method.statements = statements.toArray(new Statement[statements.size()]); return method; } - private MethodDeclaration createEquals(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) { - int pS = pos.sourceStart; int pE = pos.sourceEnd; + private MethodDeclaration createEquals(Node type, Collection<Node> fields, boolean callSuper, ASTNode source) { + int pS = source.sourceStart; int pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); - + Eclipse.setGeneratedBy(method, source); method.modifiers = PKG.toModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0); - method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, p)}; + method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; + Eclipse.setGeneratedBy(method.returnType, source); + method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source)}; 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[] { p, p, p }), Modifier.FINAL) - }; + method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; + method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; + TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p }); + Eclipse.setGeneratedBy(objectRef, source); + method.arguments = new Argument[] {new Argument(new char[] { 'o' }, 0, objectRef, Modifier.FINAL)}; + method.arguments[0].sourceStart = pS; method.arguments[0].sourceEnd = pE; + Eclipse.setGeneratedBy(method.arguments[0], source); List<Statement> statements = new ArrayList<Statement>(); /* if ( o == this ) return true; */ { - EqualExpression otherEqualsThis = new EqualExpression( - new SingleNameReference(new char[] { 'o' }, p), - new ThisReference(pS, pE), OperatorIds.EQUAL_EQUAL); + SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); + Eclipse.setGeneratedBy(oRef, source); + ThisReference thisRef = new ThisReference(pS, pE); + Eclipse.setGeneratedBy(thisRef, source); + EqualExpression otherEqualsThis = new EqualExpression(oRef, thisRef, OperatorIds.EQUAL_EQUAL); + Eclipse.setGeneratedBy(otherEqualsThis, source); - ReturnStatement returnTrue = new ReturnStatement(new TrueLiteral(pS, pE), pS, pE); + TrueLiteral trueLiteral = new TrueLiteral(pS, pE); + Eclipse.setGeneratedBy(trueLiteral, source); + ReturnStatement returnTrue = new ReturnStatement(trueLiteral, pS, pE); + Eclipse.setGeneratedBy(returnTrue, source); IfStatement ifOtherEqualsThis = new IfStatement(otherEqualsThis, returnTrue, pS, pE); + Eclipse.setGeneratedBy(ifOtherEqualsThis, source); statements.add(ifOtherEqualsThis); } /* if ( o == null ) return false; */ { - EqualExpression otherEqualsNull = new EqualExpression( - new SingleNameReference(new char[] { 'o' }, p), - new NullLiteral(pS, pE), OperatorIds.EQUAL_EQUAL); + SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); + Eclipse.setGeneratedBy(oRef, source); + NullLiteral nullLiteral = new NullLiteral(pS, pE); + Eclipse.setGeneratedBy(nullLiteral, source); + EqualExpression otherEqualsNull = new EqualExpression(oRef, nullLiteral, OperatorIds.EQUAL_EQUAL); + Eclipse.setGeneratedBy(otherEqualsNull, source); - ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnFalse, source); IfStatement ifOtherEqualsNull = new IfStatement(otherEqualsNull, returnFalse, pS, pE); + Eclipse.setGeneratedBy(ifOtherEqualsNull, source); statements.add(ifOtherEqualsNull); } /* if ( o.getClass() != getClass() ) return false; */ { MessageSend otherGetClass = new MessageSend(); + otherGetClass.sourceStart = pS; otherGetClass.sourceEnd = pE; + Eclipse.setGeneratedBy(otherGetClass, source); otherGetClass.receiver = new SingleNameReference(new char[] { 'o' }, p); + Eclipse.setGeneratedBy(otherGetClass.receiver, source); otherGetClass.selector = "getClass".toCharArray(); MessageSend thisGetClass = new MessageSend(); + thisGetClass.sourceStart = pS; thisGetClass.sourceEnd = pE; + Eclipse.setGeneratedBy(thisGetClass, source); thisGetClass.receiver = new ThisReference(pS, pE); + Eclipse.setGeneratedBy(thisGetClass.receiver, source); thisGetClass.selector = "getClass".toCharArray(); EqualExpression classesNotEqual = new EqualExpression(otherGetClass, thisGetClass, OperatorIds.NOT_EQUAL); - ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); + Eclipse.setGeneratedBy(classesNotEqual, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnFalse, source); IfStatement ifClassesNotEqual = new IfStatement(classesNotEqual, returnFalse, pS, pE); + Eclipse.setGeneratedBy(ifClassesNotEqual, source); statements.add(ifClassesNotEqual); } @@ -427,12 +504,22 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* if ( !super.equals(o) ) return false; */ if ( callSuper ) { MessageSend callToSuper = new MessageSend(); + callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; + Eclipse.setGeneratedBy(callToSuper, source); callToSuper.receiver = new SuperReference(pS, pE); + Eclipse.setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "equals".toCharArray(); - callToSuper.arguments = new Expression[] {new SingleNameReference(new char[] { 'o' }, p) }; + SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); + Eclipse.setGeneratedBy(oRef, source); + callToSuper.arguments = new Expression[] {oRef}; Expression superNotEqual = new UnaryExpression(callToSuper, OperatorIds.NOT); - ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); + Eclipse.setGeneratedBy(superNotEqual, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnFalse, source); IfStatement ifSuperEquals = new IfStatement(superNotEqual, returnFalse, pS, pE); + Eclipse.setGeneratedBy(ifSuperEquals, source); statements.add(ifSuperEquals); } @@ -440,20 +527,30 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* MyType<?> other = (MyType<?>) o; */ { if ( !fields.isEmpty() ) { LocalDeclaration other = new LocalDeclaration(otherN, pS, pE); + Eclipse.setGeneratedBy(other, source); char[] typeName = typeDecl.name; Expression targetType; if ( typeDecl.typeParameters == null || typeDecl.typeParameters.length == 0 ) { targetType = new SingleNameReference(((TypeDeclaration)type.get()).name, p); + Eclipse.setGeneratedBy(targetType, source); other.type = new SingleTypeReference(typeName, p); + Eclipse.setGeneratedBy(other.type, source); } else { TypeReference[] typeArgs = new TypeReference[typeDecl.typeParameters.length]; - for ( int i = 0 ; i < typeArgs.length ; i++ ) typeArgs[i] = new Wildcard(Wildcard.UNBOUND); + for ( int i = 0 ; i < typeArgs.length ; i++ ) { + typeArgs[i] = new Wildcard(Wildcard.UNBOUND); + typeArgs[i].sourceStart = pS; typeArgs[i].sourceEnd = pE; + Eclipse.setGeneratedBy(typeArgs[i], source); + } targetType = new ParameterizedSingleTypeReference(typeName, typeArgs, 0, p); - other.type = new ParameterizedSingleTypeReference(typeName, copyTypes(typeArgs), 0, p); + Eclipse.setGeneratedBy(targetType, source); + other.type = new ParameterizedSingleTypeReference(typeName, copyTypes(typeArgs, source), 0, p); + Eclipse.setGeneratedBy(other.type, source); } - other.initialization = new CastExpression( - new SingleNameReference(new char[] { 'o' }, p), - targetType); + NameReference oRef = new SingleNameReference(new char[] { 'o' }, p); + Eclipse.setGeneratedBy(oRef, source); + other.initialization = new CastExpression(oRef, targetType); + Eclipse.setGeneratedBy(other.initialization, source); statements.add(other); } } @@ -463,92 +560,156 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA 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, p)); + statements.add(generateCompareFloatOrDouble(otherN, "Float".toCharArray(), f.name, source)); } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) { - statements.add(generateCompareFloatOrDouble(otherN, "Double".toCharArray(), f.name, p)); + statements.add(generateCompareFloatOrDouble(otherN, "Double".toCharArray(), f.name, source)); } else if ( BUILT_IN_TYPES.contains(new String(token)) ) { - EqualExpression fieldsNotEqual = new EqualExpression( - new SingleNameReference(f.name, p), - generateQualifiedNameRef(p, otherN, f.name), - OperatorIds.NOT_EQUAL); - ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); - statements.add(new IfStatement(fieldsNotEqual, returnStatement, pS, pE)); + NameReference fieldRef = new SingleNameReference(f.name, p); + Eclipse.setGeneratedBy(fieldRef, source); + EqualExpression fieldsNotEqual = new EqualExpression(fieldRef, + generateQualifiedNameRef(source, otherN, f.name), OperatorIds.NOT_EQUAL); + Eclipse.setGeneratedBy(fieldsNotEqual, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + IfStatement ifStatement = new IfStatement(fieldsNotEqual, returnStatement, pS, pE); + Eclipse.setGeneratedBy(ifStatement, source); + statements.add(ifStatement); } else /* objects */ { - EqualExpression fieldIsNull = new EqualExpression( - new SingleNameReference(f.name, p), - new NullLiteral(pS, pE), OperatorIds.EQUAL_EQUAL); + NameReference fieldNameRef = new SingleNameReference(f.name, p); + Eclipse.setGeneratedBy(fieldNameRef, source); + NullLiteral nullLiteral = new NullLiteral(pS, pE); + Eclipse.setGeneratedBy(nullLiteral, source); + EqualExpression fieldIsNull = new EqualExpression(fieldNameRef, nullLiteral, OperatorIds.EQUAL_EQUAL); + nullLiteral = new NullLiteral(pS, pE); + Eclipse.setGeneratedBy(nullLiteral, source); EqualExpression otherFieldIsntNull = new EqualExpression( - generateQualifiedNameRef(p, otherN, f.name), - new NullLiteral(pS, pE), OperatorIds.NOT_EQUAL); + generateQualifiedNameRef(source, otherN, f.name), + nullLiteral, OperatorIds.NOT_EQUAL); MessageSend equalsCall = new MessageSend(); + equalsCall.sourceStart = pS; equalsCall.sourceEnd = pE; + Eclipse.setGeneratedBy(equalsCall, source); equalsCall.receiver = new SingleNameReference(f.name, p); + Eclipse.setGeneratedBy(equalsCall.receiver, source); equalsCall.selector = "equals".toCharArray(); - equalsCall.arguments = new Expression[] { generateQualifiedNameRef(p, otherN, f.name) }; + equalsCall.arguments = new Expression[] { generateQualifiedNameRef(source, otherN, f.name) }; UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT); + fieldsNotEqual.sourceStart = pS; fieldsNotEqual.sourceEnd = pE; + Eclipse.setGeneratedBy(fieldsNotEqual, source); ConditionalExpression fullEquals = new ConditionalExpression(fieldIsNull, otherFieldIsntNull, fieldsNotEqual); - ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); - statements.add(new IfStatement(fullEquals, returnStatement, pS, pE)); + fullEquals.sourceStart = pS; fullEquals.sourceEnd = pE; + Eclipse.setGeneratedBy(fullEquals, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + IfStatement ifStatement = new IfStatement(fullEquals, returnStatement, pS, pE); + Eclipse.setGeneratedBy(ifStatement, source); + statements.add(ifStatement); } } else if ( f.type.dimensions() > 0 && token != null ) { MessageSend arraysEqualCall = new MessageSend(); - arraysEqualCall.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); + arraysEqualCall.sourceStart = pS; arraysEqualCall.sourceEnd = pE; + Eclipse.setGeneratedBy(arraysEqualCall, source); + arraysEqualCall.receiver = generateQualifiedNameRef(source, 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, p), - generateQualifiedNameRef(p, otherN, f.name) }; + NameReference fieldNameRef = new SingleNameReference(f.name, p); + Eclipse.setGeneratedBy(fieldNameRef, source); + arraysEqualCall.arguments = new Expression[] { fieldNameRef, generateQualifiedNameRef(source, otherN, f.name) }; UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT); - ReturnStatement returnStatement = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); - statements.add(new IfStatement(arraysNotEqual, returnStatement, pS, pE)); + arraysNotEqual.sourceStart = pS; arraysNotEqual.sourceEnd = pE; + Eclipse.setGeneratedBy(arraysNotEqual, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + IfStatement ifStatement = new IfStatement(arraysNotEqual, returnStatement, pS, pE); + Eclipse.setGeneratedBy(ifStatement, source); + statements.add(ifStatement); } } /* return true; */ { - statements.add(new ReturnStatement(new TrueLiteral(pS, pE), pS, pE)); + TrueLiteral trueLiteral = new TrueLiteral(pS, pE); + Eclipse.setGeneratedBy(trueLiteral, source); + ReturnStatement returnStatement = new ReturnStatement(trueLiteral, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + statements.add(returnStatement); } method.statements = statements.toArray(new Statement[statements.size()]); return method; } - private IfStatement generateCompareFloatOrDouble(char[] otherN, char[] floatOrDouble, char[] fieldName, long p) { - int pS = (int)(p >> 32), pE = (int)p; + private IfStatement generateCompareFloatOrDouble(char[] otherN, char[] floatOrDouble, char[] fieldName, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; + long p = (long)pS << 32 | pE; /* if ( Float.compare(fieldName, other.fieldName) != 0 ) return false */ MessageSend floatCompare = new MessageSend(); - floatCompare.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); + floatCompare.sourceStart = pS; floatCompare.sourceEnd = pE; + Eclipse.setGeneratedBy(floatCompare, source); + floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); floatCompare.selector = "compare".toCharArray(); - floatCompare.arguments = new Expression[] { - new SingleNameReference(fieldName, p), - generateQualifiedNameRef(p, otherN, fieldName) - }; - EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, new IntLiteral(new char[] {'0'}, pS, pE), OperatorIds.NOT_EQUAL); - ReturnStatement returnFalse = new ReturnStatement(new FalseLiteral(pS, pE), pS, pE); - return new IfStatement(ifFloatCompareIsNot0, returnFalse, pS, pE); + NameReference fieldNameRef = new SingleNameReference(fieldName, p); + Eclipse.setGeneratedBy(fieldNameRef, source); + floatCompare.arguments = new Expression[] {fieldNameRef, generateQualifiedNameRef(source, otherN, fieldName)}; + IntLiteral int0 = new IntLiteral(new char[] {'0'}, pS, pE); + Eclipse.setGeneratedBy(int0, source); + EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, int0, OperatorIds.NOT_EQUAL); + ifFloatCompareIsNot0.sourceStart = pS; ifFloatCompareIsNot0.sourceEnd = pE; + Eclipse.setGeneratedBy(ifFloatCompareIsNot0, source); + FalseLiteral falseLiteral = new FalseLiteral(pS, pE); + Eclipse.setGeneratedBy(falseLiteral, source); + ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); + Eclipse.setGeneratedBy(returnFalse, source); + IfStatement ifStatement = new IfStatement(ifFloatCompareIsNot0, returnFalse, pS, pE); + Eclipse.setGeneratedBy(ifStatement, source); + return ifStatement; } /** Give 2 clones! */ - private Expression longToIntForHashCode(Reference ref1, Reference ref2, long p) { - int pS = (int)(p >> 32), pE = (int)p; + private Expression longToIntForHashCode(Reference ref1, Reference ref2, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; /* (int)(ref >>> 32 ^ ref) */ - BinaryExpression higherBits = new BinaryExpression( - ref1, new IntLiteral("32".toCharArray(), pS, pE), - OperatorIds.UNSIGNED_RIGHT_SHIFT); + IntLiteral int32 = new IntLiteral("32".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int32, source); + BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT); + Eclipse.setGeneratedBy(higherBits, source); BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR); - return new CastExpression(xorParts, TypeReference.baseTypeReference(TypeIds.T_int, 0)); + Eclipse.setGeneratedBy(xorParts, source); + TypeReference intRef = TypeReference.baseTypeReference(TypeIds.T_int, 0); + intRef.sourceStart = pS; intRef.sourceEnd = pE; + Eclipse.setGeneratedBy(intRef, source); + CastExpression expr = new CastExpression(xorParts, intRef); + expr.sourceStart = pS; expr.sourceEnd = pE; + Eclipse.setGeneratedBy(expr, source); + return expr; } - private Reference generateFieldReference(char[] fieldName, long p) { + private Reference generateFieldReference(char[] fieldName, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; + long p = (long)pS << 32 | pE; FieldReference thisX = new FieldReference(("this." + new String(fieldName)).toCharArray(), p); - thisX.receiver = new ThisReference((int)(p >> 32), (int)p); + Eclipse.setGeneratedBy(thisX, source); + thisX.receiver = new ThisReference(pS, pE); + Eclipse.setGeneratedBy(thisX.receiver, source); thisX.token = fieldName; return thisX; } - private NameReference generateQualifiedNameRef(long p, char[]... varNames) { - if ( varNames.length > 1 ) - return new QualifiedNameReference(varNames, new long[varNames.length], (int)(p >> 32), (int)p); - else return new SingleNameReference(varNames[0], p); + private NameReference generateQualifiedNameRef(ASTNode source, char[]... varNames) { + int pS = source.sourceStart, pE = source.sourceEnd; + long p = (long)pS << 32 | pE; + + NameReference ref; + + if ( varNames.length > 1 ) ref = new QualifiedNameReference(varNames, new long[varNames.length], pS, pE); + else ref = new SingleNameReference(varNames[0], p); + Eclipse.setGeneratedBy(ref, source); + return ref; } } |