mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 20:15:09 +00:00
Introduce ConfigurationKey<JacksonVersion> (lombok.jacksonized.jacksonVersion)
This commit is contained in:
1 parent
06f4161618
commit
2a3960babc
9 files changed
+71
-32
No files matched your search
@@ -29,6 +29,7 @@ import lombok.core.configuration.CheckerFrameworkVersion;
|
||||
import lombok.core.configuration.ConfigurationKey;
|
||||
import lombok.core.configuration.FlagUsageType;
|
||||
import lombok.core.configuration.IdentifierName;
|
||||
import lombok.core.configuration.JacksonVersion;
|
||||
import lombok.core.configuration.LogDeclaration;
|
||||
import lombok.core.configuration.NullAnnotationLibrary;
|
||||
import lombok.core.configuration.NullCheckExceptionType;
|
||||
@@ -713,18 +714,11 @@ public class ConfigurationKeys {
|
||||
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.jacksonized.useJackson2} = {@code true} | {@code false}.
|
||||
* lombok configuration: {@code lombok.jacksonized.jacksonVersion} = {@code 2} | {@code 3} | {@code 2_3}.
|
||||
*
|
||||
* If set to {@code true}, generate Jackson 2.x compatible code.
|
||||
* The jackson major version to use.
|
||||
*/
|
||||
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)") {};
|
||||
public static final ConfigurationKey<JacksonVersion> JACKSONIZED_JACKSON_VERSION = new ConfigurationKey<JacksonVersion>("lombok.jacksonized.jacksonVersion", "Select the jackson major version to use (default: 2)") {};
|
||||
|
||||
|
||||
// ----- Configuration System -----
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package lombok.core.configuration;
|
||||
|
||||
public final class JacksonVersion implements ConfigurationValueType {
|
||||
|
||||
private final boolean useJackson2;
|
||||
private final boolean useJackson3;
|
||||
|
||||
public static final JacksonVersion _2 = new JacksonVersion(true, false);
|
||||
public static final JacksonVersion _3 = new JacksonVersion(true, false);
|
||||
public static final JacksonVersion _23 = new JacksonVersion(true, true);
|
||||
|
||||
public JacksonVersion(boolean useJackson2, boolean useJackson3) {
|
||||
this.useJackson2 = useJackson2;
|
||||
this.useJackson3 = useJackson3;
|
||||
}
|
||||
|
||||
public static String exampleValue() {
|
||||
return "2";
|
||||
}
|
||||
|
||||
public static String description() {
|
||||
return "Select the major version of the jackson framework.";
|
||||
}
|
||||
|
||||
public static JacksonVersion valueOf(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return _2;
|
||||
} else if ("2".equals(value.trim())) {
|
||||
return _2;
|
||||
} else if ("3".equals(value.trim())) {
|
||||
return _3;
|
||||
} else if ("2_3".equals(value.trim())) {
|
||||
return _23;
|
||||
} else{
|
||||
throw new IllegalArgumentException("Unsupported Jackson version selector. Supported values: 2, 3 or 2_3 ");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean useJackson2() {
|
||||
return useJackson2;
|
||||
}
|
||||
|
||||
public boolean useJackson3() {
|
||||
return useJackson3;
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@ import lombok.ConfigurationKeys;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.HandlerPriority;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.configuration.JacksonVersion;
|
||||
import lombok.core.handlers.HandlerUtil;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
@@ -147,17 +148,16 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression);
|
||||
MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess);
|
||||
|
||||
Boolean useJackson2 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON2, Boolean.TRUE);
|
||||
Boolean useJackson3 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON3, Boolean.FALSE);
|
||||
if (! (useJackson2 || useJackson3)) {
|
||||
annotationNode.addError("Usage: lombok.jacksonized.useJackson2, lombok.jacksonized.useJackson3 or both must be true.");
|
||||
JacksonVersion jacksonVersion = annotationNode.getAst().readConfiguration(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION);
|
||||
if (! (jacksonVersion.useJackson2() || jacksonVersion.useJackson3())) {
|
||||
annotationNode.addError("Usage: No valid jackson version selected");
|
||||
return;
|
||||
}
|
||||
|
||||
if (useJackson2) {
|
||||
if (jacksonVersion.useJackson2()) {
|
||||
td.annotations = addAnnotation(td, td.annotations, JACKSON2_JSON_DESERIALIZE_ANNOTATION, builderMvp);
|
||||
}
|
||||
if (useJackson3) {
|
||||
if (jacksonVersion.useJackson3()) {
|
||||
td.annotations = addAnnotation(td, td.annotations, JACKSON3_JSON_DESERIALIZE_ANNOTATION, builderMvp);
|
||||
}
|
||||
// Copy annotations from the class to the builder class.
|
||||
@@ -169,10 +169,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 (useJackson2) {
|
||||
if (jacksonVersion.useJackson2()) {
|
||||
builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JACKSON2_JSON_POJO_BUILDER_ANNOTATION, withPrefixMvp, buildMethodNameMvp);
|
||||
}
|
||||
if (useJackson3) {
|
||||
if (jacksonVersion.useJackson3()) {
|
||||
builderClass.annotations = addAnnotation(builderClass, builderClass.annotations, JACKSON3_JSON_POJO_BUILDER_ANNOTATION, withPrefixMvp, buildMethodNameMvp);
|
||||
}
|
||||
// @SuperBuilder? Make it package-private!
|
||||
|
||||
@@ -42,6 +42,7 @@ import lombok.ConfigurationKeys;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.HandlerPriority;
|
||||
import lombok.core.configuration.JacksonVersion;
|
||||
import lombok.core.handlers.HandlerUtil;
|
||||
import lombok.experimental.Accessors;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
@@ -173,14 +174,12 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
return;
|
||||
}
|
||||
|
||||
Boolean useJackson2 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON2, Boolean.TRUE);
|
||||
Boolean useJackson3 = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_USE_JACKSON3, Boolean.FALSE);
|
||||
|
||||
if (! (useJackson2 || useJackson3)) {
|
||||
annotationNode.addError("Usage: lombok.jacksonized.useJackson2, lombok.jacksonized.useJackson3 or both must be true.");
|
||||
JacksonVersion jacksonVersion = annotationNode.getAst().readConfiguration(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION);
|
||||
if (! (jacksonVersion.useJackson2() || jacksonVersion.useJackson3())) {
|
||||
annotationNode.addError("Usage: No valid jackson version selected");
|
||||
return;
|
||||
}
|
||||
if (useJackson2) {
|
||||
if (jacksonVersion.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"));
|
||||
@@ -189,7 +188,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
recursiveSetGeneratedBy(annotationJsonDeserialize, annotationNode);
|
||||
td.mods.annotations = td.mods.annotations.append(annotationJsonDeserialize);
|
||||
}
|
||||
if (useJackson3) {
|
||||
if (jacksonVersion.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"));
|
||||
@@ -207,7 +206,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
}
|
||||
builderClass.mods.annotations = builderClass.mods.annotations.appendList(copiedAnnotations);
|
||||
|
||||
if (useJackson2) {
|
||||
if (jacksonVersion.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));
|
||||
@@ -216,7 +215,7 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
recursiveSetGeneratedBy(annotationJsonPOJOBuilder, annotatedNode);
|
||||
builderClass.mods.annotations = builderClass.mods.annotations.append(annotationJsonPOJOBuilder);
|
||||
}
|
||||
if (useJackson3) {
|
||||
if (jacksonVersion.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));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//version 8: Jackson deps are at least Java7+.
|
||||
//CONF: lombok.builder.className = Test*Name
|
||||
//CONF: lombok.jacksonized.useJackson3 = true
|
||||
//CONF: lombok.jacksonized.jacksonVersion = 2_3
|
||||
import java.util.List;
|
||||
import lombok.Builder;
|
||||
import lombok.extern.jackson.Jacksonized;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//version 8: Jackson deps are at least Java7+.
|
||||
//CONF: lombok.jacksonized.useJackson3 = true
|
||||
//CONF: lombok.jacksonized.jacksonVersion = 2_3
|
||||
import java.util.List;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//version 8: Jackson deps are at least Java7+.
|
||||
//CONF: lombok.jacksonized.useJackson3 = true
|
||||
//CONF: lombok.jacksonized.jacksonVersion = 2_3
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//version 14:
|
||||
//CONF: lombok.jacksonized.useJackson3 = true
|
||||
//CONF: lombok.jacksonized.jacksonVersion = 2_3
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//CONF: lombok.jacksonized.useJackson3 = true
|
||||
//CONF: lombok.jacksonized.jacksonVersion = 2_3
|
||||
//version 8: Jackson deps are at least Java7+.
|
||||
public class JacksonizedSuperBuilderSimple {
|
||||
@lombok.extern.jackson.Jacksonized
|
||||
|
||||
Reference in new issue
Block a user