From 3ad435f08241bfd25d01c0519ec3fc1f685ef7d9 Mon Sep 17 00:00:00 2001 From: Liam Pace <146035497+liampace@users.noreply.github.com> Date: Thu, 18 Sep 2025 01:33:31 -0400 Subject: [PATCH] Refactor eclipse handler --- .../eclipse/handlers/HandleJacksonized.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/core/lombok/eclipse/handlers/HandleJacksonized.java b/src/core/lombok/eclipse/handlers/HandleJacksonized.java index 0a3f5dba..da45c981 100644 --- a/src/core/lombok/eclipse/handlers/HandleJacksonized.java +++ b/src/core/lombok/eclipse/handlers/HandleJacksonized.java @@ -62,9 +62,21 @@ import lombok.spi.Provides; @HandlerPriority(-512) // Above Handle(Super)Builder's level (builders must be already generated). public class HandleJacksonized extends EclipseAnnotationHandler { - private static final char[][] JSON_POJO_BUILDER_ANNOTATION = Eclipse.fromQualifiedName("com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder"); - private static final char[][] JSON_DESERIALIZE_ANNOTATION = Eclipse.fromQualifiedName("com.fasterxml.jackson.databind.annotation.JsonDeserialize"); - private static final char[][] JSON_PROPERTY_ANNOTATION = Eclipse.fromQualifiedName("com.fasterxml.jackson.annotation.JsonProperty"); + private static enum JacksonAnnotations { + JSON_POJO_BUILDER("com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder"), + JSON_DESERIALIZE("com.fasterxml.jackson.databind.annotation.JsonDeserialize"), + JSON_PROPERTY("com.fasterxml.jackson.annotation.JsonProperty"); + + private final String qualifiedName; + private final char[][] annotation; + private JacksonAnnotations(final String qualifiedName) { + this.qualifiedName = qualifiedName; + this.annotation = Eclipse.fromQualifiedName(qualifiedName); + } + private boolean isAnnotating(EclipseNode node) { + return hasAnnotation(this.qualifiedName, node); + } + } @Override public void handle(AnnotationValues annotation, Annotation ast, EclipseNode annotationNode) { handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.JACKSONIZED_FLAG_USAGE, "@Jacksonized"); @@ -132,7 +144,7 @@ public class HandleJacksonized extends EclipseAnnotationHandler { } // Insert @JsonDeserialize on annotated class. - if (hasAnnotation("com.fasterxml.jackson.databind.annotation.JsonDeserialize", tdNode)) { + if (JacksonAnnotations.JSON_DESERIALIZE.isAnnotating(tdNode)) { annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson."); return; } @@ -140,7 +152,7 @@ public class HandleJacksonized extends EclipseAnnotationHandler { TypeReference builderClassExpression = namePlusTypeParamsToTypeReference(builderClassNode, null, p); ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression); MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess); - td.annotations = addAnnotation(td, td.annotations, JSON_DESERIALIZE_ANNOTATION, builderMvp); + td.annotations = addAnnotation(td, td.annotations, JacksonAnnotations.JSON_DESERIALIZE.annotation, builderMvp); // Copy annotations from the class to the builder class. Annotation[] copyableAnnotations = findJacksonAnnotationsOnClass(td, tdNode); @@ -151,7 +163,7 @@ public class HandleJacksonized extends EclipseAnnotationHandler { MemberValuePair withPrefixMvp = new MemberValuePair("withPrefix".toCharArray(), builderClass.sourceStart, builderClass.sourceEnd, withPrefixLiteral); StringLiteral buildMethodNameLiteral = new StringLiteral(buildMethodName.toCharArray(), builderClass.sourceStart, builderClass.sourceEnd, 0); MemberValuePair buildMethodNameMvp = new MemberValuePair("buildMethodName".toCharArray(), builderClass.sourceStart, builderClass.sourceEnd, buildMethodNameLiteral); - builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JSON_POJO_BUILDER_ANNOTATION, withPrefixMvp, buildMethodNameMvp); + builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JacksonAnnotations.JSON_POJO_BUILDER.annotation, withPrefixMvp, buildMethodNameMvp); // @SuperBuilder? Make it package-private! if (superBuilderAnnotationNode != null) @@ -181,12 +193,12 @@ public class HandleJacksonized extends EclipseAnnotationHandler { } private void createJsonPropertyForField(EclipseNode fieldNode, EclipseNode annotationNode) { - if (hasAnnotation("com.fasterxml.jackson.annotation.JsonProperty", fieldNode)) return; + if (JacksonAnnotations.JSON_PROPERTY.isAnnotating(fieldNode)) return; ASTNode astNode = fieldNode.get(); if (astNode instanceof FieldDeclaration) { FieldDeclaration fd = (FieldDeclaration)astNode; StringLiteral fieldName = new StringLiteral(fd.name, 0, 0, 0); - ((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JSON_PROPERTY_ANNOTATION, fieldName); + ((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotations.JSON_PROPERTY.annotation, fieldName); } }