From b8e85f5dc7044e9a4bd43c41786bdda2a38f50ac Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 4 Jun 2018 23:34:25 +0200 Subject: FieldNameConstants now defaults to having a prefix ‘FIELD_’, which can be configured both on the annotation itself and via a config key. This totally breaks compatibility with the previous lombok release, but, hey, it’s in experimental and it’s been one week. This is better. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/changelog.markdown | 1 + src/core/lombok/ConfigurationKeys.java | 14 ++++++++++++ .../eclipse/handlers/HandleFieldNameConstants.java | 26 +++++++++++++--------- .../lombok/experimental/FieldNameConstants.java | 2 ++ .../javac/handlers/HandleFieldNameConstants.java | 26 +++++++++++++--------- .../after-delombok/FieldNameConstantsBasic.java | 4 ++-- .../FieldNameConstantsConfigKeys.java | 4 ++++ .../after-delombok/FieldNameConstantsWeird.java | 2 ++ .../after-ecj/FieldNameConstantsBasic.java | 4 ++-- .../after-ecj/FieldNameConstantsConfigKeys.java | 9 ++++++++ .../after-ecj/FieldNameConstantsWeird.java | 6 ++++- .../before/FieldNameConstantsConfigKeys.java | 7 ++++++ .../resource/before/FieldNameConstantsWeird.java | 3 +++ .../FieldNameConstantsWeird.java.messages | 2 +- .../FieldNameConstantsWeird.java.messages | 2 +- .../features/experimental/FieldNameConstants.html | 14 ++++++++++-- 16 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 test/transform/resource/after-delombok/FieldNameConstantsConfigKeys.java create mode 100644 test/transform/resource/after-ecj/FieldNameConstantsConfigKeys.java create mode 100644 test/transform/resource/before/FieldNameConstantsConfigKeys.java diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 771ccc55..f1575a53 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -7,6 +7,7 @@ Lombok Changelog * BUGFIX: Using boolean parameters in lombok annotations would fail. [Issue #1709](https://github.com/rzwitserloot/lombok/issues/1709) * BUGFIX: Delombok would give an error message. [Issue #1705](https://github.com/rzwitserloot/lombok/issues/1705) * FEATURE: Google's [Flogger (a.k.a. FluentLogger)](https://google.github.io/flogger/) is now available via `@Flogger`. [Issue #1697](https://github.com/rzwitserloot/lombok/issues/1697) +* FEATURE: `@FieldNameConstants` has been extended to support prefixes and suffixes. By default, the generated constants are prefixed with `FIELD_`. [Docs on @FieldNameConstants](https://projectlombok.org/features/experimental/FieldNameConstants). ### v1.16.22 "Envious Ferret" (May 29th, 2018) * FEATURE: Private no-args constructor for `@Data` and `@Value` to enable deserialization frameworks (like Jackson) to operate out-of-the-box. Use `lombok.noArgsConstructor.extraPrivate = false` to disable this behavior. diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index adca920e..46c3f770 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -521,6 +521,20 @@ public class ConfigurationKeys { */ public static final ConfigurationKey FIELD_NAME_CONSTANTS_FLAG_USAGE = new ConfigurationKey("lombok.fieldNameConstants.flagUsage", "Emit a warning or error if @FieldNameConstants is used.") {}; + /** + * lombok configuration: {@code lombok.fieldNameConstants.prefix} = <String: aJavaIdentifierPrefix> (Default: {@code PREFIX_}). + * + * The names of the constants generated by {@code @FieldNameConstants} will be prefixed with this value. + */ + public static final ConfigurationKey FIELD_NAME_CONSTANTS_PREFIX = new ConfigurationKey("lombok.fieldNameConstants.prefix", "names of constants generated by @FieldNameConstants will be prefixed with this value. (default: 'PREFIX_').") {}; + + /** + * lombok configuration: {@code lombok.fieldNameConstants.suffix} = <String: aJavaIdentifierPrefix> (Default: nothing). + * + * The names of the constants generated by {@code @FieldNameConstants} will be suffixed with this value. + */ + public static final ConfigurationKey FIELD_NAME_CONSTANTS_SUFFIX = new ConfigurationKey("lombok.fieldNameConstants.suffix", "names of constants generated by @FieldNameConstants will be suffixed with this value. (default: nothing).") {}; + // ----- Wither ----- /** diff --git a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java index 754ddf47..c3a28f7f 100644 --- a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java +++ b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java @@ -49,7 +49,7 @@ import org.mangosdk.spi.ProviderFor; @ProviderFor(EclipseAnnotationHandler.class) public class HandleFieldNameConstants extends EclipseAnnotationHandler { - public void generateFieldNameConstantsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level) { + public void generateFieldNameConstantsForType(EclipseNode typeNode, EclipseNode errorNode, AccessLevel level, String prefix, String suffix) { TypeDeclaration typeDecl = null; if (typeNode.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) typeNode.get(); @@ -62,13 +62,13 @@ public class HandleFieldNameConstants extends EclipseAnnotationHandler fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { - for (EclipseNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, fieldNode, errorNode, source, whineIfExists); + private void createFieldNameConstantsForFields(AccessLevel level, String prefix, String suffix, Collection fieldNodes, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { + for (EclipseNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, prefix, suffix, fieldNode, errorNode, source, whineIfExists); } - private void createFieldNameConstantsForField(AccessLevel level, EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { + private void createFieldNameConstantsForField(AccessLevel level, String prefix, String suffix, EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists) { if (fieldNode.getKind() != Kind.FIELD) { errorNode.addError("@FieldNameConstants is only supported on a class, an enum, or a field"); return; @@ -111,7 +117,7 @@ public class HandleFieldNameConstants extends EclipseAnnotationHandler { - public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level) { + public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, String prefix, String suffix) { JCClassDecl typeDecl = null; if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get(); @@ -62,13 +62,13 @@ public class HandleFieldNameConstants extends JavacAnnotationHandler fieldNodes, JavacNode annotationNode, JavacNode errorNode, boolean whineIfExists) { - for (JavacNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, fieldNode, errorNode, whineIfExists); + private void createFieldNameConstantsForFields(AccessLevel level, String prefix, String suffix, Collection fieldNodes, JavacNode annotationNode, JavacNode errorNode, boolean whineIfExists) { + for (JavacNode fieldNode : fieldNodes) createFieldNameConstantsForField(level, prefix, suffix, fieldNode, errorNode, whineIfExists); } - private void createFieldNameConstantsForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists) { + private void createFieldNameConstantsForField(AccessLevel level, String prefix, String suffix, JavacNode fieldNode, JavacNode source, boolean whineIfExists) { if (fieldNode.getKind() != Kind.FIELD) { source.addError("@FieldNameConstants is only supported on a class, an enum, or a field"); return; @@ -115,7 +121,7 @@ public class HandleFieldNameConstants extends JavacAnnotationHandler() { + } + public FieldNameConstantsConfigKeys() { + super(); + } +} diff --git a/test/transform/resource/after-ecj/FieldNameConstantsWeird.java b/test/transform/resource/after-ecj/FieldNameConstantsWeird.java index c581b7ef..9958f664 100644 --- a/test/transform/resource/after-ecj/FieldNameConstantsWeird.java +++ b/test/transform/resource/after-ecj/FieldNameConstantsWeird.java @@ -1,8 +1,12 @@ import lombok.experimental.FieldNameConstants; import lombok.AccessLevel; public @FieldNameConstants class FieldNameConstantsWeird { + public static final java.lang.String FIELD_AZ = "A"; @FieldNameConstants(level = AccessLevel.NONE) String iAmADvdPlayer; - String X; + @FieldNameConstants(prefix = "") String X; + @FieldNameConstants(suffix = "Z") String A; + () { + } public FieldNameConstantsWeird() { super(); } diff --git a/test/transform/resource/before/FieldNameConstantsConfigKeys.java b/test/transform/resource/before/FieldNameConstantsConfigKeys.java new file mode 100644 index 00000000..ab8e3091 --- /dev/null +++ b/test/transform/resource/before/FieldNameConstantsConfigKeys.java @@ -0,0 +1,7 @@ +//CONF: lombok.fieldNameConstants.prefix = +//CONF: lombok.fieldNameConstants.suffix = _SFX + +@lombok.experimental.FieldNameConstants +public class FieldNameConstantsConfigKeys { + String iAmADvdPlayer; +} diff --git a/test/transform/resource/before/FieldNameConstantsWeird.java b/test/transform/resource/before/FieldNameConstantsWeird.java index 0f99133d..74ec299a 100644 --- a/test/transform/resource/before/FieldNameConstantsWeird.java +++ b/test/transform/resource/before/FieldNameConstantsWeird.java @@ -5,5 +5,8 @@ import lombok.AccessLevel; public class FieldNameConstantsWeird { @FieldNameConstants(level = AccessLevel.NONE) String iAmADvdPlayer; + @FieldNameConstants(prefix = "") String X; + @FieldNameConstants(suffix = "Z") + String A; } diff --git a/test/transform/resource/messages-delombok/FieldNameConstantsWeird.java.messages b/test/transform/resource/messages-delombok/FieldNameConstantsWeird.java.messages index d5fc44f5..02a38d58 100644 --- a/test/transform/resource/messages-delombok/FieldNameConstantsWeird.java.messages +++ b/test/transform/resource/messages-delombok/FieldNameConstantsWeird.java.messages @@ -1 +1 @@ -8 Not generating constant for this field: The name of the constant would be equal to the name of this field. +9 Not generating constant for this field: The name of the constant would be equal to the name of this field. diff --git a/test/transform/resource/messages-ecj/FieldNameConstantsWeird.java.messages b/test/transform/resource/messages-ecj/FieldNameConstantsWeird.java.messages index d5fc44f5..02a38d58 100644 --- a/test/transform/resource/messages-ecj/FieldNameConstantsWeird.java.messages +++ b/test/transform/resource/messages-ecj/FieldNameConstantsWeird.java.messages @@ -1 +1 @@ -8 Not generating constant for this field: The name of the constant would be equal to the name of this field. +9 Not generating constant for this field: The name of the constant would be equal to the name of this field. diff --git a/website/templates/features/experimental/FieldNameConstants.html b/website/templates/features/experimental/FieldNameConstants.html index 05c57f84..c5e57195 100644 --- a/website/templates/features/experimental/FieldNameConstants.html +++ b/website/templates/features/experimental/FieldNameConstants.html @@ -3,7 +3,7 @@ <@f.scaffold title="@FieldNameConstants" logline="Name... that... field! String constants for your field's names."> <@f.history>

- @FieldNameConstants was introduced as experimental feature in lombok v2.0.0. + @FieldNameConstants was introduced as experimental feature in lombok v1.16.22.

@@ -18,7 +18,7 @@ <@f.overview>

- The @FieldNameConstants annotation generates string constants (fields marked public static final, of type java.lang.String) containing the field's name, as a string. This is useful for various marshalling and serialization frameworks. The constant field has the same as the field it represents, except with all uppercase letters, with underscores in front of the uppercase letters in the original field. If this results in the exact same name, the constant is not generated (a warning is generated instead). + The @FieldNameConstants annotation generates string constants (fields marked public static final, of type java.lang.String) containing the field's name, as a string. This is useful for various marshalling and serialization frameworks. The constant field by default is named FIELD_NAME_OF_FIELD, where NAME_OF_FIELD is the same name as the field it represents, except with all uppercase letters, with underscores in front of the uppercase letters in the original field. The prefix (and suffix) is configurable on the @FieldNameConstants annotation.

The public access modifier can be changed via the parameter level = AccessLevel.PACKAGE for example. You can force a field to be skipped by supplying level = AccessLevel.NONE.

@@ -33,12 +33,22 @@ lombok.fieldNameConstants.flagUsage = [warning | error] (default: not set)

Lombok will flag any usage of @FieldDefaults as a warning or error if configured. +
+ lombok.fieldNameConstants.prefix = a string (default: 'PREFIX_') +
+ Lombok will generate the name for each fieldconstant by constant-casing the field name and then prefixing this string to the result. +
+ lombok.fieldNameConstants.suffix = a string (default: '' - the blank string) +
+ Lombok will generate the name for each fieldconstant by constant-casing the field name and then suffixing this string to the result.
<@f.smallPrint>

Like other lombok handlers that touch fields, any field whose name starts with a dollar ($) symbol is skipped entirely. Such a field will not be modified at all. Static fields are also skipped. +

+ The annotation can itself be used to set prefix/suffix. If you do so, it overrides the lombok.fieldNameConstants.prefix/suffix config key. Example: @FieldNameConstants(prefix = ""): This would generate for field helloWorld a constant named HELLO_WORLD instead of the default FIELD_HELLO_WORLD.

-- cgit