Introduce useJackson2 and useJackson3 option #3950

This commit is contained in:
Lars Uffmann committed 2025-09-22 14:16:53 +02:00
1 parent 086b351830
commit 03d40e8685
3 files changed
+32 -8

No files matched your search

+16
View File
@@ -711,6 +711,22 @@ public class ConfigurationKeys {
*/
public static final ConfigurationKey<FlagUsageType> JACKSONIZED_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.jacksonized.flagUsage", "Emit a warning or error if @Jacksonized is used.") {};
/**
* lombok configuration: {@code lombok.jacksonized.useJackson2} = {@code true} | {@code false}.
*
* If set to {@code true}, generate Jackson 2.x compatible code.
*/
public static final ConfigurationKey<Boolean> JACKSONIZED_USE_JACKSON2 = new ConfigurationKey<Boolean>("lombok.jacksonized.useJackson2", "Generate Jackson 2.x compatible code (default: true)") {};
/**
* lombok configuration: {@code lombok.jacksonized.useJackson3} = {@code true} | {@code false}.
*
* If set to {@code true}, generate Jackson 3.x compatible code.
*/
public static final ConfigurationKey<Boolean> JACKSONIZED_USE_JACKSON3 = new ConfigurationKey<Boolean>("lombok.jacksonized.useJackson3", "Generate Jackson 3.x compatible code (default: false)") {};
// ----- Configuration System -----
/**
@@ -146,10 +146,14 @@ 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);
/* if (jackson2) */ {
Boolean useJackson2 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON2, Boolean.TRUE);
Boolean useJackson3 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON3, Boolean.FALSE);
if (useJackson2) {
td.annotations = addAnnotation(td, td.annotations, JACKSON2_JSON_DESERIALIZE_ANNOTATION, builderMvp);
}
/* if (jackson3) */ {
if (useJackson3) {
td.annotations = addAnnotation(td, td.annotations, JACKSON3_JSON_DESERIALIZE_ANNOTATION, builderMvp);
}
// Copy annotations from the class to the builder class.
@@ -161,10 +165,10 @@ 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);
/* if (jackson2) */ {
if (useJackson2) {
builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JACKSON2_JSON_POJO_BUILDER_ANNOTATION, withPrefixMvp, buildMethodNameMvp);
}
/* if (jackson3) */ {
if (useJackson3) {
builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JACKSON3_JSON_POJO_BUILDER_ANNOTATION, withPrefixMvp, buildMethodNameMvp);
}
// @SuperBuilder? Make it package-private!
@@ -172,7 +172,11 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson.");
return;
}
/* if (jackson2) */ {
Boolean useJackson2 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON2, Boolean.TRUE);
Boolean useJackson3 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON3, Boolean.TRUE);
if (useJackson2) {
JCExpression jsonDeserializeType = chainDots(annotatedNode, "com", "fasterxml", "jackson", "databind", "annotation", "JsonDeserialize");
JCExpression builderClassExpression = namePlusTypeParamsToTypeReference(maker, tdNode, annotationNode.toName(builderClassName), false, List.<JCTypeParameter>nil());
JCFieldAccess builderClassReference = maker.Select(builderClassExpression, annotatedNode.toName("class"));
@@ -181,7 +185,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
recursiveSetGeneratedBy(annotationJsonDeserialize, annotationNode);
td.mods.annotations = td.mods.annotations.append(annotationJsonDeserialize);
}
/* if (jackson3) */ {
if (useJackson3) {
JCExpression jsonDeserializeType = chainDots(annotatedNode, "tools", "jackson", "databind", "annotation", "JsonDeserialize");
JCExpression builderClassExpression = namePlusTypeParamsToTypeReference(maker, tdNode, annotationNode.toName(builderClassName), false, List.<JCTypeParameter>nil());
JCFieldAccess builderClassReference = maker.Select(builderClassExpression, annotatedNode.toName("class"));
@@ -199,7 +203,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
}
builderClass.mods.annotations = builderClass.mods.annotations.appendList(copiedAnnotations);
/* if (jackson2) */ {
if (useJackson2) {
// Insert @JsonPOJOBuilder on the builder class.
JCExpression jsonPOJOBuilderType = chainDots(annotatedNode, "com", "fasterxml", "jackson", "databind", "annotation", "JsonPOJOBuilder");
JCExpression withPrefixExpr = maker.Assign(maker.Ident(annotationNode.toName("withPrefix")), maker.Literal(setPrefix));
@@ -208,7 +212,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
recursiveSetGeneratedBy(annotationJsonPOJOBuilder, annotatedNode);
builderClass.mods.annotations = builderClass.mods.annotations.append(annotationJsonPOJOBuilder);
}
/* if (jackson3) */ {
if (useJackson3) {
// Insert @JsonPOJOBuilder on the builder class.
JCExpression jsonPOJOBuilderType = chainDots(annotatedNode, "tools", "jackson", "databind", "annotation", "JsonPOJOBuilder");
JCExpression withPrefixExpr = maker.Assign(maker.Ident(annotationNode.toName("withPrefix")), maker.Literal(setPrefix));