diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-07-28 16:47:24 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-07-28 16:47:24 +0200 |
commit | e744f1829adf95f3b54fa54d813eb26c4c8e8c76 (patch) | |
tree | 2e0f441ec68a34016d4fa573bbc6e4c4ad948dab /src/lombok/eclipse/handlers | |
parent | 817eae66a61fc9334828a43b81d929ca1dd363cf (diff) | |
download | lombok-e744f1829adf95f3b54fa54d813eb26c4c8e8c76.tar.gz lombok-e744f1829adf95f3b54fa54d813eb26c4c8e8c76.tar.bz2 lombok-e744f1829adf95f3b54fa54d813eb26c4c8e8c76.zip |
Changed the way toString is generated to reduce the number of superfluous 'plus' nodes (e.g. concatenating the infix ", " and a field name literal such as "width=" into ", width=".
Also removed the [] brackets from the supercall, as, if you're chaining to another lombok-generated toString, those are superfluous - lombok's toString includes parentheses already.
Diffstat (limited to 'src/lombok/eclipse/handlers')
-rw-r--r-- | src/lombok/eclipse/handlers/HandleToString.java | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/src/lombok/eclipse/handlers/HandleToString.java b/src/lombok/eclipse/handlers/HandleToString.java index 4cafebb5..4b5204b5 100644 --- a/src/lombok/eclipse/handlers/HandleToString.java +++ b/src/lombok/eclipse/handlers/HandleToString.java @@ -161,31 +161,37 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> { private MethodDeclaration createToString(Node type, Collection<Node> fields, boolean includeFieldNames, boolean callSuper, ASTNode pos) { char[] rawTypeName = ((TypeDeclaration)type.get()).name; String typeName = rawTypeName == null ? "" : new String(rawTypeName); - char[] prefix = (typeName + "(").toCharArray(); char[] suffix = ")".toCharArray(); - char[] infix = ", ".toCharArray(); + String infixS = ", "; + char[] infix = infixS.toCharArray(); long p = (long)pos.sourceStart << 32 | pos.sourceEnd; final int PLUS = OperatorIds.PLUS; + char[] prefix; + + if ( callSuper ) { + prefix = (typeName + "(super=").toCharArray(); + } else if ( fields.isEmpty() ) { + prefix = (typeName + "()").toCharArray(); + } else if ( includeFieldNames ) { + prefix = (typeName + "(" + new String(((FieldDeclaration)fields.iterator().next().get()).name) + "=").toCharArray(); + } else { + prefix = (typeName + "(").toCharArray(); + } + boolean first = true; Expression current = new StringLiteral(prefix, 0, 0, 0); if ( callSuper ) { MessageSend callToSuper = new MessageSend(); callToSuper.receiver = new SuperReference(0, 0); callToSuper.selector = "toString".toCharArray(); - current = new BinaryExpression(current, new StringLiteral("super=[".toCharArray(), 0, 0, 0), PLUS); current = new BinaryExpression(current, callToSuper, PLUS); - current = new BinaryExpression(current, new StringLiteral(new char[] { ']' }, 0, 0, 0), PLUS); first = false; } for ( Node field : fields ) { FieldDeclaration f = (FieldDeclaration)field.get(); if ( f.name == null || f.type == null ) continue; - if ( !first ) { - current = new BinaryExpression(current, new StringLiteral(infix, 0, 0, 0), PLUS); - } - else first = false; Expression ex; if ( f.type.dimensions() > 0 ) { @@ -200,13 +206,21 @@ public class HandleToString implements EclipseAnnotationHandler<ToString> { ex = arrayToString; } else ex = new SingleNameReference(f.name, p); + if ( first ) { + current = new BinaryExpression(current, ex, PLUS); + first = false; + continue; + } + if ( includeFieldNames ) { - char[] namePlusEqualsSign = (new String(f.name) + "=").toCharArray(); + char[] namePlusEqualsSign = (infixS + new String(f.name) + "=").toCharArray(); current = new BinaryExpression(current, new StringLiteral(namePlusEqualsSign, 0, 0, 0), PLUS); + } else { + current = new BinaryExpression(current, new StringLiteral(infix, 0, 0, 0), PLUS); } current = new BinaryExpression(current, ex, PLUS); } - current = new BinaryExpression(current, new StringLiteral(suffix, 0, 0, 0), PLUS); + if ( !first ) current = new BinaryExpression(current, new StringLiteral(suffix, 0, 0, 0), PLUS); ReturnStatement returnStatement = new ReturnStatement(current, (int)(p >> 32), (int)p); |