mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 04:16:19 +00:00
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.
This commit is contained in:
1 parent
817eae66a6
commit
e744f1829a
2 files changed
+59
-22
No files matched your search
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user