diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-18 00:24:56 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-18 00:34:22 +0200 |
commit | 8869e9759478403d362f919fde6b79075e7950d6 (patch) | |
tree | acb6d06a9f46f5a0dc054137ba63f13240fb2727 /src/core/lombok/eclipse/handlers | |
parent | 51b031c732a86cb28b35c9b6c9b31916d3846c88 (diff) | |
download | lombok-8869e9759478403d362f919fde6b79075e7950d6.tar.gz lombok-8869e9759478403d362f919fde6b79075e7950d6.tar.bz2 lombok-8869e9759478403d362f919fde6b79075e7950d6.zip |
All field accesses done by generated lombok methods are now qualified with 'this.'
Addresses issue #48
Diffstat (limited to 'src/core/lombok/eclipse/handlers')
3 files changed, 21 insertions, 25 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 2c636916..ef0bc9be 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -561,15 +561,14 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA for (EclipseNode 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, source)); } else if (Arrays.equals(TypeConstants.DOUBLE, token)) { statements.add(generateCompareFloatOrDouble(otherN, "Double".toCharArray(), f.name, source)); } else if (BUILT_IN_TYPES.contains(new String(token))) { - NameReference fieldRef = new SingleNameReference(f.name, p); - Eclipse.setGeneratedBy(fieldRef, source); - EqualExpression fieldsNotEqual = new EqualExpression(fieldRef, + EqualExpression fieldsNotEqual = new EqualExpression(generateFieldReference(f.name, source), generateQualifiedNameRef(source, otherN, f.name), OperatorIds.NOT_EQUAL); Eclipse.setGeneratedBy(fieldsNotEqual, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); @@ -580,11 +579,9 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA Eclipse.setGeneratedBy(ifStatement, source); statements.add(ifStatement); } else /* objects */ { - 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); + EqualExpression fieldIsNull = new EqualExpression(generateFieldReference(f.name, source), nullLiteral, OperatorIds.EQUAL_EQUAL); nullLiteral = new NullLiteral(pS, pE); Eclipse.setGeneratedBy(nullLiteral, source); EqualExpression otherFieldIsntNull = new EqualExpression( @@ -593,8 +590,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA 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.receiver = generateFieldReference(f.name, source); equalsCall.selector = "equals".toCharArray(); equalsCall.arguments = new Expression[] { generateQualifiedNameRef(source, otherN, f.name) }; UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT); @@ -621,9 +617,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA } else { arraysEqualCall.selector = "equals".toCharArray(); } - NameReference fieldNameRef = new SingleNameReference(f.name, p); - Eclipse.setGeneratedBy(fieldNameRef, source); - arraysEqualCall.arguments = new Expression[] { fieldNameRef, generateQualifiedNameRef(source, otherN, f.name) }; + arraysEqualCall.arguments = new Expression[] { generateFieldReference(f.name, source), generateQualifiedNameRef(source, otherN, f.name) }; UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT); arraysNotEqual.sourceStart = pS; arraysNotEqual.sourceEnd = pE; Eclipse.setGeneratedBy(arraysNotEqual, source); @@ -650,16 +644,13 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA 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.sourceStart = pS; floatCompare.sourceEnd = pE; Eclipse.setGeneratedBy(floatCompare, source); floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); floatCompare.selector = "compare".toCharArray(); - NameReference fieldNameRef = new SingleNameReference(fieldName, p); - Eclipse.setGeneratedBy(fieldNameRef, source); - floatCompare.arguments = new Expression[] {fieldNameRef, generateQualifiedNameRef(source, otherN, fieldName)}; + floatCompare.arguments = new Expression[] {generateFieldReference(fieldName, source), 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); @@ -696,7 +687,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA 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); + FieldReference thisX = new FieldReference(fieldName, p); Eclipse.setGeneratedBy(thisX, source); thisX.receiver = new ThisReference(pS, pE); Eclipse.setGeneratedBy(thisX.receiver, source); diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 43197ae4..9a676eb5 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -34,12 +34,12 @@ import lombok.eclipse.EclipseNode; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; -import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; +import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; -import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.Statement; +import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; @@ -176,9 +176,12 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> { method.thrownExceptions = null; method.typeParameters = null; method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; - Expression fieldExpression = new SingleNameReference(field.name, ((long)field.declarationSourceStart << 32) | field.declarationSourceEnd); - Eclipse.setGeneratedBy(fieldExpression, source); - Statement returnStatement = new ReturnStatement(fieldExpression, field.sourceStart, field.sourceEnd); + long p = ((long)field.declarationSourceStart << 32) | field.declarationSourceEnd; + FieldReference thisX = new FieldReference(field.name, p); + Eclipse.setGeneratedBy(thisX, source); + thisX.receiver = new ThisReference(source.sourceStart, source.sourceEnd); + Eclipse.setGeneratedBy(thisX.receiver, source); + Statement returnStatement = new ReturnStatement(thisX, field.sourceStart, field.sourceEnd); Eclipse.setGeneratedBy(returnStatement, source); method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java index d5a4c398..5b87e32b 100644 --- a/src/core/lombok/eclipse/handlers/HandleToString.java +++ b/src/core/lombok/eclipse/handlers/HandleToString.java @@ -220,12 +220,17 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> { FieldDeclaration f = (FieldDeclaration)field.get(); if (f.name == null || f.type == null) continue; + FieldReference thisX = new FieldReference(f.name, p); + Eclipse.setGeneratedBy(thisX, source); + thisX.receiver = new ThisReference(source.sourceStart, source.sourceEnd); + Eclipse.setGeneratedBy(thisX.receiver, source); + Expression ex; if (f.type.dimensions() > 0) { MessageSend arrayToString = new MessageSend(); arrayToString.sourceStart = pS; arrayToString.sourceEnd = pE; arrayToString.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); - arrayToString.arguments = new Expression[] { new SingleNameReference(f.name, p) }; + arrayToString.arguments = new Expression[] { thisX }; Eclipse.setGeneratedBy(arrayToString.arguments[0], source); if (f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(f.type.getLastToken()))) { arrayToString.selector = "deepToString".toCharArray(); @@ -234,9 +239,6 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> { } ex = arrayToString; } else { - FieldReference thisX = new FieldReference(f.name, p); - thisX.receiver = new ThisReference(source.sourceStart, source.sourceEnd); - Eclipse.setGeneratedBy(thisX.receiver, source); ex = thisX; } Eclipse.setGeneratedBy(ex, source); |