From 592882feefcac110ec800ecb10d81f0328c62685 Mon Sep 17 00:00:00 2001 From: Jan Rieke Date: Mon, 31 Mar 2025 23:47:50 +0200 Subject: [PATCH] also include getters, rename to copyJacksonAnnotationsToAccessors --- src/core/lombok/ConfigurationKeys.java | 6 +++--- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 8 ++++++-- src/core/lombok/eclipse/handlers/HandleGetter.java | 7 ++++++- src/core/lombok/javac/handlers/HandleGetter.java | 7 ++++++- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 8 ++++++-- test/transform/resource/before/JacksonJsonProperty.java | 2 +- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index c060d7b0..948f55d6 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -746,10 +746,10 @@ public class ConfigurationKeys { public static final ConfigurationKey STANDARD_EXCEPTION_FLAG_USAGE = new ConfigurationKey("lombok.standardException.flagUsage", "Emit a warning or error if @StandardException is used.") {}; /** - * lombok configuration: {@code lombok.copyJacksonAnnotationsToSetters} = {@code true} | {@code false}. + * lombok configuration: {@code lombok.copyJacksonAnnotationsToAccessors} = {@code true} | {@code false}. * - * If true, copy certain Jackson annotations from a field to its corresponding getter. This was the behavior from lombok 1.18.16 to 1.18.38. + * If true, copy certain Jackson annotations from a field to its corresponding accessors (getter/setters). This was the behavior from lombok 1.18.16 to 1.18.38. * However, it turned out to be harmful in certain situations. Thus, the default is now false. */ - public static final ConfigurationKey COPY_JACKSON_ANNOTATIONS_TO_SETTERS = new ConfigurationKey("lombok.copyJacksonAnnotationsToSetters", "Copy these annotations to getters, setters, with methods, builder-setters, etc.") {}; + public static final ConfigurationKey COPY_JACKSON_ANNOTATIONS_TO_ACCESSORS = new ConfigurationKey("lombok.copyJacksonAnnotationsToAccessors", "Copy Jackson annotations from fields to their getters and setters.") {}; } diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index eeed200f..410015a6 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -822,7 +822,11 @@ public class EclipseHandlerUtil { /** * Searches the given field node for annotations that are specifically intentioned to be copied to the setter. */ - public static Annotation[] findCopyableToGetterAnnotations(EclipseNode node) { + public static Annotation[] findCopyableToGetterAnnotations(EclipseNode node, boolean forceCopyJacksonAnnotations) { + if (!forceCopyJacksonAnnotations) { + Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_ACCESSORS); + if (copyAnnotations == null || !copyAnnotations) return EMPTY_ANNOTATIONS_ARRAY; + } return findAnnotationsInList(node, JACKSON_COPY_TO_GETTER_ANNOTATIONS); } @@ -833,7 +837,7 @@ public class EclipseHandlerUtil { */ public static Annotation[] findCopyableToSetterAnnotations(EclipseNode node, boolean forceCopyJacksonAnnotations) { if (!forceCopyJacksonAnnotations) { - Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_SETTERS); + Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_ACCESSORS); if (copyAnnotations == null || !copyAnnotations) return EMPTY_ANNOTATIONS_ARRAY; } return findAnnotationsInList(node, JACKSON_COPY_TO_SETTER_ANNOTATIONS); diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 2af5ba1f..3e1abcc8 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -281,10 +281,15 @@ public class HandleGetter extends EclipseAnnotationHandler { if (getCheckerFrameworkVersion(fieldNode).generateSideEffectFree()) checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE) }; } + // Copying Jackson annotations is required for fluent accessors (otherwise Jackson would not find the accessor). + boolean fluent = accessors.isExplicit("fluent"); + Boolean fluentConfig = fieldNode.getAst().readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT); + if (fluentConfig != null && fluentConfig) fluent = fluentConfig; + method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), findCopyableAnnotations(fieldNode), - findCopyableToGetterAnnotations(fieldNode), + findCopyableToGetterAnnotations(fieldNode, fluent), findDelegatesAndMarkAsHandled(fieldNode), checkerFramework, deprecated); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 64dcfb68..9806a550 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -252,7 +252,12 @@ public class HandleGetter extends JavacAnnotationHandler { JCExpression annotationMethodDefaultValue = null; List copyableAnnotations = findCopyableAnnotations(field); - List copyableToGetterAnnotations = copyAnnotations(findCopyableToGetterAnnotations(field), treeMaker); + + // Copying Jackson annotations is required for fluent accessors (otherwise Jackson would not find the accessor). + boolean fluent = accessors.isExplicit("fluent"); + Boolean fluentConfig = field.getAst().readConfiguration(ConfigurationKeys.ACCESSORS_FLUENT); + if (fluentConfig != null && fluentConfig) fluent = fluentConfig; + List copyableToGetterAnnotations = copyAnnotations(findCopyableToGetterAnnotations(field, fluent), treeMaker); List delegates = findDelegatesAndRemoveFromField(field); List annsOnMethod = copyAnnotations(onMethod, treeMaker).appendList(copyableAnnotations).appendList(copyableToGetterAnnotations); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 94b58fff..b1f0be1a 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1759,7 +1759,11 @@ public class JavacHandlerUtil { /** * Searches the given field node for annotations that are specifically intentioned to be copied to the getter. */ - public static List findCopyableToGetterAnnotations(JavacNode node) { + public static List findCopyableToGetterAnnotations(JavacNode node, boolean forceCopyJacksonAnnotations) { + if (!forceCopyJacksonAnnotations) { + Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_ACCESSORS); + if (copyAnnotations == null || !copyAnnotations) return List.nil(); + } return findAnnotationsInList(node, JACKSON_COPY_TO_GETTER_ANNOTATIONS); } @@ -1770,7 +1774,7 @@ public class JavacHandlerUtil { */ public static List findCopyableToSetterAnnotations(JavacNode node, boolean forceCopyJacksonAnnotations) { if (!forceCopyJacksonAnnotations) { - Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_SETTERS); + Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_ACCESSORS); if (copyAnnotations == null || !copyAnnotations) return List.nil(); } return findAnnotationsInList(node, JACKSON_COPY_TO_SETTER_ANNOTATIONS); diff --git a/test/transform/resource/before/JacksonJsonProperty.java b/test/transform/resource/before/JacksonJsonProperty.java index 4d3018cc..716298f2 100644 --- a/test/transform/resource/before/JacksonJsonProperty.java +++ b/test/transform/resource/before/JacksonJsonProperty.java @@ -1,4 +1,4 @@ -//CONF: lombok.copyJacksonAnnotationsToSetters = true +//CONF: lombok.copyJacksonAnnotationsToAccessors = true //version 8: Jackson deps are at least Java7+. import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter;