Refactor eclipse handler

This commit is contained in:
Liam Pace committed 2025-11-21 13:50:00 -05:00
1 parent e3c9e3d9ff
commit 3ad435f082
1 file changed
+20 -8
@@ -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<Jacksonized> {
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<Jacksonized> annotation, Annotation ast, EclipseNode annotationNode) {
handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.JACKSONIZED_FLAG_USAGE, "@Jacksonized");
@@ -132,7 +144,7 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
}
// 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<Jacksonized> {
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<Jacksonized> {
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<Jacksonized> {
}
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);
}
}