diff options
author | Roel Spilker <r.spilker@gmail.com> | 2018-11-06 00:27:02 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2018-11-06 00:37:07 +0100 |
commit | 7e79316a1f28d1eb1ef8569119b97a70387fd6c6 (patch) | |
tree | a4251e266a99984b569f70bbb2e8baee6b55b177 /src/core/lombok/javac/handlers/HandleToString.java | |
parent | dc641cc3696e63c46c9693ccdfd296f173afec53 (diff) | |
download | lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.tar.gz lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.tar.bz2 lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.zip |
Improve toString generation for enums. Fixes #1916
Diffstat (limited to 'src/core/lombok/javac/handlers/HandleToString.java')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleToString.java | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index eb6e56f3..3780da79 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -166,23 +166,34 @@ public class HandleToString extends JavacAnnotationHandler<ToString> { boolean first = true; String typeName = getTypeName(typeNode); + boolean isEnum = typeNode.isEnumType(); + String infix = ", "; String suffix = ")"; String prefix; if (callSuper) { - prefix = typeName + "(super="; + prefix = "(super="; } else if (members.isEmpty()) { - prefix = typeName + "()"; + prefix = isEnum ? "" : "()"; } else if (includeNames) { Included<JavacNode, ToString.Include> firstMember = members.iterator().next(); String name = firstMember.getInc() == null ? "" : firstMember.getInc().name(); if (name.isEmpty()) name = firstMember.getNode().getName(); - prefix = typeName + "(" + name + "="; + prefix = "(" + name + "="; + } else { + prefix = "("; + } + + JCExpression current; + if (!isEnum) { + current = maker.Literal(typeName + prefix); } else { - prefix = typeName + "("; + current = maker.Binary(CTC_PLUS, maker.Literal(typeName + "."), maker.Apply(List.<JCExpression>nil(), + maker.Select(maker.Ident(typeNode.toName("this")), typeNode.toName("name")), + List.<JCExpression>nil())); + if (!prefix.isEmpty()) current = maker.Binary(CTC_PLUS, current, maker.Literal(prefix)); } - JCExpression current = maker.Literal(prefix); if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), |