All field accesses done by generated lombok methods are now qualified with 'this.'

Addresses issue #48
This commit is contained in:
Reinier Zwitserloot committed 2010-07-18 00:34:22 +02:00
1 parent 51b031c732
commit 8869e97594
6 files changed
+35 -29

No files matched your search

@@ -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);
@@ -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;
@@ -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);
@@ -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<Getter> {
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));
@@ -94,7 +94,12 @@ public class HandleSynchronized implements JavacAnnotationHandler<Synchronized>
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.<JCStatement>of(maker.Synchronized(lockNode, method.body)));
@@ -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<ToString> {
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.<JCExpression>nil(), hcMethod, List.<JCExpression>of(maker.Ident(field.name)));
} else expr = maker.Ident(field.name);
expr = maker.Apply(List.<JCExpression>nil(), hcMethod, List.<JCExpression>of(thisX));
} else expr = thisX;
if (first) {
current = maker.Binary(JCTree.PLUS, current, expr);