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 | |
parent | dc641cc3696e63c46c9693ccdfd296f173afec53 (diff) | |
download | lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.tar.gz lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.tar.bz2 lombok-7e79316a1f28d1eb1ef8569119b97a70387fd6c6.zip |
Improve toString generation for enums. Fixes #1916
-rw-r--r-- | doc/changelog.markdown | 1 | ||||
-rw-r--r-- | src/core/lombok/core/LombokNode.java | 3 | ||||
-rw-r--r-- | src/core/lombok/eclipse/EclipseNode.java | 7 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleToString.java | 69 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacNode.java | 8 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleToString.java | 21 | ||||
-rw-r--r-- | test/transform/resource/after-delombok/DataOnEnum.java | 2 | ||||
-rw-r--r-- | test/transform/resource/after-delombok/ToStringEnum.java | 28 | ||||
-rw-r--r-- | test/transform/resource/after-ecj/DataOnEnum.java | 2 | ||||
-rw-r--r-- | test/transform/resource/after-ecj/ToStringEnum.java | 41 | ||||
-rw-r--r-- | test/transform/resource/before/ToStringEnum.java | 17 |
11 files changed, 168 insertions, 31 deletions
diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 78588c36..4a19ed73 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -5,6 +5,7 @@ Lombok Changelog * BUGFIX: Since version 1.18.4, the delombok ant task didn't work and errored with a `NoClassDefFoundError`. [Issue #1932](https://github.com/rzwitserloot/lombok/issues/1932) * FEATURE: The `@FieldNameConstants` feature now allows you to write the inner type by hand and add whatever you like to it; lombok will add the constants to this class. See the updated [FieldNameConstants feature](https://projectlombok.org/features/experimental/FieldNameConstants) page. * FEATURE: There is now a `lombok.config` key to configure `@ToString`'s call super behavior; it's just like `@EqualsAndHashCode` which has had it for a while now. [Issue #1918](https://github.com/rzwitserloot/lombok/issues/1918) +* ENHANCEMENT: The toString generation of enums now contains the name of the enum constant. [Issue #1916](https://github.com/rzwitserloot/lombok/issues/1916) ### v1.18.4 (October 30th, 2018) * PLATFORM: Support for Eclipse Photon. [Issue #1831](https://github.com/rzwitserloot/lombok/issues/1831) diff --git a/src/core/lombok/core/LombokNode.java b/src/core/lombok/core/LombokNode.java index 5a0842bc..ae8fd325 100644 --- a/src/core/lombok/core/LombokNode.java +++ b/src/core/lombok/core/LombokNode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -279,6 +279,7 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A, public abstract boolean isStatic(); public abstract boolean isTransient(); public abstract boolean isEnumMember(); + public abstract boolean isEnumType(); public abstract int countMethodParameters(); diff --git a/src/core/lombok/eclipse/EclipseNode.java b/src/core/lombok/eclipse/EclipseNode.java index 1738c770..a0580c51 100644 --- a/src/core/lombok/eclipse/EclipseNode.java +++ b/src/core/lombok/eclipse/EclipseNode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2012 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -250,6 +250,11 @@ public class EclipseNode extends lombok.core.LombokNode<EclipseAST, EclipseNode, return ((FieldDeclaration) node).getKind() == 3; } + @Override public boolean isEnumType() { + if (getKind() != Kind.TYPE) return false; + return (((TypeDeclaration) node).modifiers & ClassFileConstants.AccEnum) != 0; + } + @Override public int countMethodParameters() { if (getKind() != Kind.METHOD) return 0; diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java index 303e5bea..bb52feb5 100644 --- a/src/core/lombok/eclipse/handlers/HandleToString.java +++ b/src/core/lombok/eclipse/handlers/HandleToString.java @@ -21,7 +21,7 @@ */ package lombok.eclipse.handlers; -import static lombok.core.handlers.HandlerUtil.*; +import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.eclipse.handlers.EclipseHandlerUtil.*; import java.util.Arrays; @@ -31,18 +31,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import lombok.AccessLevel; -import lombok.ConfigurationKeys; -import lombok.ToString; -import lombok.core.AST.Kind; -import lombok.core.AnnotationValues; -import lombok.core.configuration.CallSuperType; -import lombok.core.handlers.InclusionExclusionUtils; -import lombok.core.handlers.InclusionExclusionUtils.Included; -import lombok.eclipse.Eclipse; -import lombok.eclipse.EclipseAnnotationHandler; -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.BinaryExpression; @@ -59,12 +47,26 @@ import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.Statement; import org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.eclipse.jdt.internal.compiler.ast.SuperReference; +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; import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.mangosdk.spi.ProviderFor; +import lombok.AccessLevel; +import lombok.ConfigurationKeys; +import lombok.ToString; +import lombok.core.AST.Kind; +import lombok.core.AnnotationValues; +import lombok.core.configuration.CallSuperType; +import lombok.core.handlers.HandlerUtil.FieldAccess; +import lombok.core.handlers.InclusionExclusionUtils; +import lombok.core.handlers.InclusionExclusionUtils.Included; +import lombok.eclipse.Eclipse; +import lombok.eclipse.EclipseAnnotationHandler; +import lombok.eclipse.EclipseNode; + /** * Handles the {@code ToString} annotation for eclipse. */ @@ -165,6 +167,8 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> { boolean includeNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { String typeName = getTypeName(type); + boolean isEnum = type.isEnumType(); + char[] suffix = ")".toCharArray(); String infixS = ", "; char[] infix = infixS.toCharArray(); @@ -172,31 +176,54 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> { long p = (long)pS << 32 | pE; final int PLUS = OperatorIds.PLUS; - char[] prefix; + String prefix; if (callSuper) { - prefix = (typeName + "(super=").toCharArray(); + prefix = "(super="; } else if (members.isEmpty()) { - prefix = (typeName + "()").toCharArray(); + prefix = isEnum ? "" : "()"; } else if (includeNames) { Included<EclipseNode, ToString.Include> firstMember = members.iterator().next(); String name = firstMember.getInc() == null ? "" : firstMember.getInc().name(); if (name.isEmpty()) name = firstMember.getNode().getName(); - prefix = (typeName + "(" + name + "=").toCharArray(); + prefix = "(" + name + "="; } else { - prefix = (typeName + "(").toCharArray(); + prefix = "("; } boolean first = true; - Expression current = new StringLiteral(prefix, pS, pE, 0); - setGeneratedBy(current, source); + Expression current; + if (!isEnum) { + current = new StringLiteral((typeName + prefix).toCharArray(), pS, pE, 0); + setGeneratedBy(current, source); + } else { + current = new StringLiteral((typeName + ".").toCharArray(), pS, pE, 0); + setGeneratedBy(current, source); + + MessageSend thisName = new MessageSend(); + thisName.sourceStart = pS; thisName.sourceEnd = pE; + setGeneratedBy(thisName, source); + thisName.receiver = new ThisReference(pS, pE); + setGeneratedBy(thisName.receiver, source); + thisName.selector = "name".toCharArray(); + current = new BinaryExpression(current, thisName, PLUS); + setGeneratedBy(current, source); + + if (!prefix.isEmpty()) { + StringLiteral px = new StringLiteral(prefix.toCharArray(), pS, pE, 0); + setGeneratedBy(px, source); + current = new BinaryExpression(current, px, PLUS); + current.sourceStart = pS; current.sourceEnd = pE; + setGeneratedBy(current, source); + } + } if (callSuper) { MessageSend callToSuper = new MessageSend(); callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; setGeneratedBy(callToSuper, source); callToSuper.receiver = new SuperReference(pS, pE); - setGeneratedBy(callToSuper, source); + setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "toString".toCharArray(); current = new BinaryExpression(current, callToSuper, PLUS); setGeneratedBy(current, source); diff --git a/src/core/lombok/javac/JavacNode.java b/src/core/lombok/javac/JavacNode.java index 3963c892..f119f1f9 100644 --- a/src/core/lombok/javac/JavacNode.java +++ b/src/core/lombok/javac/JavacNode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2017 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -317,6 +317,12 @@ public class JavacNode extends lombok.core.LombokNode<JavacAST, JavacNode, JCTre return mods != null && (Flags.ENUM & mods.flags) != 0; } + @Override public boolean isEnumType() { + if (getKind() != Kind.TYPE) return false; + JCModifiers mods = getModifiers(); + return mods != null && (Flags.ENUM & mods.flags) != 0; + } + @Override public boolean isTransient() { if (getKind() != Kind.FIELD) return false; JCModifiers mods = getModifiers(); 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(), diff --git a/test/transform/resource/after-delombok/DataOnEnum.java b/test/transform/resource/after-delombok/DataOnEnum.java index 2486646b..6b5779e8 100644 --- a/test/transform/resource/after-delombok/DataOnEnum.java +++ b/test/transform/resource/after-delombok/DataOnEnum.java @@ -8,7 +8,7 @@ public enum DataOnEnum { @java.lang.Override @java.lang.SuppressWarnings("all") public java.lang.String toString() { - return "DataOnEnum(someField=" + this.getSomeField() + ")"; + return "DataOnEnum." + this.name() + "(someField=" + this.getSomeField() + ")"; } @java.lang.SuppressWarnings("all") private DataOnEnum(final String someField) { diff --git a/test/transform/resource/after-delombok/ToStringEnum.java b/test/transform/resource/after-delombok/ToStringEnum.java new file mode 100644 index 00000000..9d852e19 --- /dev/null +++ b/test/transform/resource/after-delombok/ToStringEnum.java @@ -0,0 +1,28 @@ +enum ToStringEnum1 { + CONSTANT; + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "ToStringEnum1." + this.name(); + } +} +enum ToStringEnum2 { + CONSTANT; + int x; + String name; + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "ToStringEnum2." + this.name() + "(x=" + this.x + ", name=" + this.name + ")"; + } +} +class ToStringEnum3 { + enum MemberEnum { + CONSTANT; + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "ToStringEnum3.MemberEnum." + this.name(); + } + } +}
\ No newline at end of file diff --git a/test/transform/resource/after-ecj/DataOnEnum.java b/test/transform/resource/after-ecj/DataOnEnum.java index 0f07088e..31eb12ec 100644 --- a/test/transform/resource/after-ecj/DataOnEnum.java +++ b/test/transform/resource/after-ecj/DataOnEnum.java @@ -7,7 +7,7 @@ public @lombok.Getter @lombok.ToString @lombok.RequiredArgsConstructor enum Data return this.someField; } public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { - return (("DataOnEnum(someField=" + this.getSomeField()) + ")"); + return (((("DataOnEnum." + this.name()) + "(someField=") + this.getSomeField()) + ")"); } private @java.lang.SuppressWarnings("all") DataOnEnum(final String someField) { super(); diff --git a/test/transform/resource/after-ecj/ToStringEnum.java b/test/transform/resource/after-ecj/ToStringEnum.java new file mode 100644 index 00000000..d4ab5a93 --- /dev/null +++ b/test/transform/resource/after-ecj/ToStringEnum.java @@ -0,0 +1,41 @@ +import lombok.ToString; +@ToString enum ToStringEnum1 { + CONSTANT(), + <clinit>() { + } + ToStringEnum1() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return ("ToStringEnum1." + this.name()); + } +} +@ToString enum ToStringEnum2 { + CONSTANT(), + int x; + String name; + <clinit>() { + } + ToStringEnum2() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return (((((("ToStringEnum2." + this.name()) + "(x=") + this.x) + ", name=") + this.name) + ")"); + } +} +class ToStringEnum3 { + @ToString enum MemberEnum { + CONSTANT(), + <clinit>() { + } + MemberEnum() { + super(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return ("ToStringEnum3.MemberEnum." + this.name()); + } + } + ToStringEnum3() { + super(); + } +}
\ No newline at end of file diff --git a/test/transform/resource/before/ToStringEnum.java b/test/transform/resource/before/ToStringEnum.java new file mode 100644 index 00000000..8b8ad576 --- /dev/null +++ b/test/transform/resource/before/ToStringEnum.java @@ -0,0 +1,17 @@ +import lombok.ToString; +@ToString +enum ToStringEnum1 { + CONSTANT; +} +@ToString +enum ToStringEnum2 { + CONSTANT(); + int x; + String name; +} +class ToStringEnum3 { + @ToString + enum MemberEnum { + CONSTANT; + } +}
\ No newline at end of file |