From 8869e9759478403d362f919fde6b79075e7950d6 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 18 Jul 2010 00:24:56 +0200 Subject: All field accesses done by generated lombok methods are now qualified with 'this.' Addresses issue #48 --- .../eclipse/handlers/HandleEqualsAndHashCode.java | 23 +++++++--------------- src/core/lombok/eclipse/handlers/HandleGetter.java | 13 +++++++----- .../lombok/eclipse/handlers/HandleToString.java | 10 ++++++---- src/core/lombok/javac/handlers/HandleGetter.java | 4 +++- .../lombok/javac/handlers/HandleSynchronized.java | 7 ++++++- src/core/lombok/javac/handlers/HandleToString.java | 7 +++++-- 6 files changed, 35 insertions(+), 29 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 { 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 { 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 { } 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); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 5af05c7c..16185e9c 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -39,6 +39,7 @@ import com.sun.tools.javac.tree.JCTree.JCAnnotation; 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.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCTypeParameter; @@ -159,7 +160,8 @@ public class HandleGetter implements JavacAnnotationHandler { private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker) { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); - JCStatement returnStatement = treeMaker.Return(treeMaker.Ident(fieldNode.getName())); + JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldNode.name); + JCStatement returnStatement = treeMaker.Return(thisX); JCBlock methodBody = treeMaker.Block(0, List.of(returnStatement)); Name methodName = field.toName(toGetterName(fieldNode)); diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java index 3602e74b..2f900eb8 100644 --- a/src/core/lombok/javac/handlers/HandleSynchronized.java +++ b/src/core/lombok/javac/handlers/HandleSynchronized.java @@ -94,7 +94,12 @@ public class HandleSynchronized implements JavacAnnotationHandler if (method.body == null) return false; - JCExpression lockNode = maker.Ident(methodNode.toName(lockName)); + JCExpression lockNode; + if (isStatic) { + lockNode = chainDots(maker, methodNode, methodNode.up().getName(), lockName); + } else { + lockNode = maker.Select(maker.Ident(methodNode.toName("this")), methodNode.toName(lockName)); + } method.body = maker.Block(0, List.of(maker.Synchronized(lockNode, method.body))); diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index dd3df620..cdba7558 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -40,6 +40,7 @@ import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree; 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.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.JCTree.JCModifiers; @@ -202,14 +203,16 @@ public class HandleToString implements JavacAnnotationHandler { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); JCExpression expr; + JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name); + if (field.vartype instanceof JCArrayTypeTree) { boolean multiDim = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCArrayTypeTree; boolean primitiveArray = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCPrimitiveTypeTree; boolean useDeepTS = multiDim || !primitiveArray; JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepTS ? "deepToString" : "toString"); - expr = maker.Apply(List.nil(), hcMethod, List.of(maker.Ident(field.name))); - } else expr = maker.Ident(field.name); + expr = maker.Apply(List.nil(), hcMethod, List.of(thisX)); + } else expr = thisX; if (first) { current = maker.Binary(JCTree.PLUS, current, expr); -- cgit