aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-07-28 16:47:24 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-07-28 16:47:24 +0200
commite744f1829adf95f3b54fa54d813eb26c4c8e8c76 (patch)
tree2e0f441ec68a34016d4fa573bbc6e4c4ad948dab /src/lombok/javac
parent817eae66a61fc9334828a43b81d929ca1dd363cf (diff)
downloadlombok-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/javac')
-rw-r--r--src/lombok/javac/handlers/HandleToString.java47
1 files changed, 35 insertions, 12 deletions
diff --git a/src/lombok/javac/handlers/HandleToString.java b/src/lombok/javac/handlers/HandleToString.java
index 4b989c68..49f0d15c 100644
--- a/src/lombok/javac/handlers/HandleToString.java
+++ b/src/lombok/javac/handlers/HandleToString.java
@@ -152,38 +152,61 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
JCExpression returnType = chainDots(maker, typeNode, "java", "lang", "String");
- JCExpression current = maker.Literal(((JCClassDecl) typeNode.get()).name.toString() + "(");
boolean first = true;
+ String typeName = ((JCClassDecl) typeNode.get()).name.toString();
+ String infix = ", ";
+ String suffix = ")";
+ String prefix;
+ if ( callSuper ) {
+ prefix = typeName + "(super=";
+ } else if ( fields.isEmpty() ) {
+ prefix = typeName + "()";
+ } else if ( includeFieldNames ) {
+ prefix = typeName + "(" + ((JCVariableDecl)fields.iterator().next().get()).name.toString() + "=";
+ } else {
+ prefix = typeName + "(";
+ }
+
+ JCExpression current = maker.Literal(prefix);
+
if ( callSuper ) {
- current = maker.Binary(JCTree.PLUS, current, maker.Literal("super=["));
JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(),
maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")),
List.<JCExpression>nil());
current = maker.Binary(JCTree.PLUS, current, callToSuper);
- current = maker.Binary(JCTree.PLUS, current, maker.Literal("]"));
first = false;
}
for ( Node fieldNode : fields ) {
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
- if ( !first ) current = maker.Binary(JCTree.PLUS, current, maker.Literal(", "));
- first = false;
- if ( includeFieldNames ) {
- current = maker.Binary(JCTree.PLUS, current, maker.Literal(fieldNode.getName() + "="));
- }
+ JCExpression expr;
+
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");
- current = maker.Binary(JCTree.PLUS, current, maker.Apply(
- List.<JCExpression>nil(), hcMethod, List.<JCExpression>of(maker.Ident(field.name))));
- } else current = maker.Binary(JCTree.PLUS, current, maker.Ident(field.name));
+ expr = maker.Apply(List.<JCExpression>nil(), hcMethod, List.<JCExpression>of(maker.Ident(field.name)));
+ } else expr = maker.Ident(field.name);
+
+ if ( first ) {
+ current = maker.Binary(JCTree.PLUS, current, expr);
+ first = false;
+ continue;
+ }
+
+ if ( includeFieldNames ) {
+ current = maker.Binary(JCTree.PLUS, current, maker.Literal(infix + fieldNode.getName() + "="));
+ } else {
+ current = maker.Binary(JCTree.PLUS, current, maker.Literal(infix));
+ }
+
+ current = maker.Binary(JCTree.PLUS, current, expr);
}
- current = maker.Binary(JCTree.PLUS, current, maker.Literal(")"));
+ if ( !first ) current = maker.Binary(JCTree.PLUS, current, maker.Literal(suffix));
JCStatement returnStatement = maker.Return(current);