From ce9a103dd2d695f2f5af9efddb0d33932c84af25 Mon Sep 17 00:00:00 2001 From: Liam Pace <146035497+liampace@users.noreply.github.com> Date: Thu, 18 Sep 2025 02:11:57 -0400 Subject: [PATCH] Implement javac handler --- .../javac/handlers/HandleJacksonized.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/core/lombok/javac/handlers/HandleJacksonized.java b/src/core/lombok/javac/handlers/HandleJacksonized.java index 61400559..6a43abe4 100644 --- a/src/core/lombok/javac/handlers/HandleJacksonized.java +++ b/src/core/lombok/javac/handlers/HandleJacksonized.java @@ -118,10 +118,12 @@ public class HandleJacksonized extends JavacAnnotationHandler { return; } - // Add @JsonProperty to all fields. It will be automatically copied to the getter/setters later. + // Add @JsonProperty to all non-transient fields. It will be automatically copied to the getter/setters later. + // Add @JsonIgnore to all transient fields. It will be automatically copied to the getter/setters later. for (JavacNode javacNode : tdNode.down()) { if (javacNode.getKind() == Kind.FIELD) { - createJsonPropertyForField(javacNode, annotationNode); + if (javacNode.isTransient()) createJsonIgnoreForField(javacNode, annotationNode); + else createJsonPropertyForField(javacNode, annotationNode); } } } @@ -139,6 +141,19 @@ public class HandleJacksonized extends JavacAnnotationHandler { fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty); } + private void createJsonIgnoreForField(JavacNode fieldNode, JavacNode annotationNode) { + if (JacksonAnnotations.JSON_IGNORE.isAnnotating(fieldNode)) { + return; + } + JavacTreeMaker maker = fieldNode.getTreeMaker(); + + JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotations.JSON_IGNORE.chainedDots); + JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.nil()); + recursiveSetGeneratedBy(annotationJsonProperty, annotationNode); + JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get()); + fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty); + } + private void handleJacksonizedBuilder(JavacNode annotationNode, JavacNode annotatedNode, JavacNode tdNode, JCClassDecl td, JavacNode builderAnnotationNode, JavacNode superBuilderAnnotationNode) { if (builderAnnotationNode != null && superBuilderAnnotationNode != null) { annotationNode.addError("@Jacksonized cannot process both @Builder and @SuperBuilder on the same class.");