mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 00:17:45 +00:00
feat: Add Jackson 3 support to @Jacksonized handlers
Update both javac and eclipse handlers to emit Jackson 3 databind annotations (@JsonDeserialize, @JsonPOJOBuilder) when configured via lombok.jacksonized.jacksonVersion. # Conflicts: # src/core/lombok/javac/handlers/HandleJacksonized.java
This commit is contained in:
1 parent
e027ad0f15
commit
184d42363d
2 files changed
+38
-34
No files matched your search
@@ -90,9 +90,11 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
return;
|
||||
}
|
||||
|
||||
List<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode);
|
||||
|
||||
boolean jacksonizedBuilder = builderAnnotationNode != null || superBuilderAnnotationNode != null;
|
||||
if (jacksonizedBuilder) {
|
||||
handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode);
|
||||
handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode, jacksonVersions);
|
||||
}
|
||||
|
||||
if (accessorsAnnotationNode != null) {
|
||||
@@ -100,7 +102,7 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleJacksonizedBuilder(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, EclipseNode tdNode, TypeDeclaration td, EclipseNode builderAnnotationNode, EclipseNode superBuilderAnnotationNode) {
|
||||
private void handleJacksonizedBuilder(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, EclipseNode tdNode, TypeDeclaration td, EclipseNode builderAnnotationNode, EclipseNode superBuilderAnnotationNode, List<JacksonVersion> jacksonVersions) {
|
||||
boolean isAbstract = (td.modifiers & ClassFileConstants.AccAbstract) != 0;
|
||||
if (isAbstract) {
|
||||
annotationNode.addError("Builders on abstract classes cannot be @Jacksonized (the builder would never be used).");
|
||||
@@ -141,13 +143,6 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression);
|
||||
MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess);
|
||||
|
||||
List<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
|
||||
|
||||
if (jacksonVersions.isEmpty()) {
|
||||
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
|
||||
jacksonVersions = Arrays.asList(JacksonVersion.TWO);
|
||||
}
|
||||
|
||||
if (jacksonVersions.contains(JacksonVersion.TWO)) {
|
||||
td.annotations = addAnnotation(td, td.annotations, JacksonAnnotationType.JSON_DESERIALIZE2.getQualifiednameAsCharArrayArray(), builderMvp);
|
||||
}
|
||||
@@ -212,18 +207,18 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
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, JacksonAnnotationType.JSON_PROPERTY2.getQualifiednameAsCharArrayArray(), fieldName);
|
||||
fd.annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_PROPERTY2.getQualifiednameAsCharArrayArray(), fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void createJsonIgnoreForField(EclipseNode fieldNode, EclipseNode annotationNode) {
|
||||
ASTNode astNode = fieldNode.get();
|
||||
if (astNode instanceof FieldDeclaration) {
|
||||
FieldDeclaration fd = (FieldDeclaration) astNode;
|
||||
((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_IGNORE2.getQualifiednameAsCharArrayArray());
|
||||
fd.annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_IGNORE2.getQualifiednameAsCharArrayArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, TypeDeclaration td, AnnotationValues<Builder> builderAnnotation) {
|
||||
String builderClassName = builderAnnotation != null ?
|
||||
builderAnnotation.getInstance().builderClassName() : null;
|
||||
@@ -248,6 +243,13 @@ public class HandleJacksonized extends EclipseAnnotationHandler<Jacksonized> {
|
||||
return builderClassName;
|
||||
}
|
||||
|
||||
private List<JacksonVersion> readConfiguredJacksonVersions(EclipseNode annotationNode) {
|
||||
List<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
|
||||
if (!jacksonVersions.isEmpty()) return jacksonVersions;
|
||||
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
|
||||
return Arrays.asList(JacksonVersion.TWO);
|
||||
}
|
||||
|
||||
private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
|
||||
|
||||
private static Annotation[] findJacksonAnnotationsOnClass(TypeDeclaration td, EclipseNode node) {
|
||||
|
||||
@@ -114,9 +114,10 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
|
||||
// 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.
|
||||
readConfiguredJacksonVersions(annotationNode);
|
||||
for (JavacNode javacNode : tdNode.down()) {
|
||||
if (javacNode.getKind() == Kind.FIELD) {
|
||||
if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) ||
|
||||
if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) ||
|
||||
hasAnnotation(javacNode, JacksonAnnotationType.JSON_IGNORE2)) {
|
||||
continue;
|
||||
}
|
||||
@@ -131,22 +132,21 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
|
||||
private void createJsonPropertyForField(JavacNode fieldNode, JavacNode annotationNode) {
|
||||
JavacTreeMaker maker = fieldNode.getTreeMaker();
|
||||
|
||||
JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_PROPERTY2);
|
||||
JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.<JCExpression>of(maker.Literal(fieldNode.getName())));
|
||||
recursiveSetGeneratedBy(annotationJsonProperty, annotationNode);
|
||||
JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get());
|
||||
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
|
||||
List<JCExpression> args = List.<JCExpression>of(maker.Literal(fieldNode.getName()));
|
||||
addFieldAnnotation(fieldNode, annotationNode, JacksonAnnotationType.JSON_PROPERTY2, args);
|
||||
}
|
||||
|
||||
|
||||
private void createJsonIgnoreForField(JavacNode fieldNode, JavacNode annotationNode) {
|
||||
addFieldAnnotation(fieldNode, annotationNode, JacksonAnnotationType.JSON_IGNORE2, List.<JCExpression>nil());
|
||||
}
|
||||
|
||||
private void addFieldAnnotation(JavacNode fieldNode, JavacNode annotationNode, JacksonAnnotationType annotationType, List<JCExpression> args) {
|
||||
JavacTreeMaker maker = fieldNode.getTreeMaker();
|
||||
|
||||
JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_IGNORE2);
|
||||
JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.<JCExpression>nil());
|
||||
recursiveSetGeneratedBy(annotationJsonProperty, annotationNode);
|
||||
JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get());
|
||||
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
|
||||
JCExpression type = chainDots(fieldNode, annotationType);
|
||||
JCAnnotation annotation = maker.Annotation(type, args);
|
||||
recursiveSetGeneratedBy(annotation, annotationNode);
|
||||
JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
|
||||
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotation);
|
||||
}
|
||||
|
||||
private void handleJacksonizedBuilder(JavacNode annotationNode, JavacNode annotatedNode, JavacNode tdNode, JCClassDecl td, JavacNode builderAnnotationNode, JavacNode superBuilderAnnotationNode) {
|
||||
@@ -191,19 +191,14 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode);
|
||||
|
||||
// Insert @JsonDeserialize on annotated class.
|
||||
if (hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE2) || hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE3)) {
|
||||
annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson.");
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
|
||||
|
||||
if (jacksonVersions.isEmpty()) {
|
||||
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
|
||||
jacksonVersions = Arrays.asList(JacksonVersion.TWO);
|
||||
}
|
||||
|
||||
if (jacksonVersions.contains(JacksonVersion.TWO)) {
|
||||
JCExpression jsonDeserializeType = chainDots(annotatedNode, JacksonAnnotationType.JSON_DESERIALIZE2);
|
||||
insertJsonDeserializeAnnotation(annotationNode, annotatedNode, tdNode, td, maker, builderClassName, jsonDeserializeType);
|
||||
@@ -280,6 +275,13 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
|
||||
return builderClassName;
|
||||
}
|
||||
|
||||
private Collection<JacksonVersion> readConfiguredJacksonVersions(JavacNode annotationNode) {
|
||||
Collection<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
|
||||
if (!jacksonVersions.isEmpty()) return jacksonVersions;
|
||||
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
|
||||
return Arrays.asList(JacksonVersion.TWO);
|
||||
}
|
||||
|
||||
private static List<JCAnnotation> findJacksonAnnotationsOnClass(JavacNode node) {
|
||||
ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
|
||||
for (JavacNode child : node.down()) {
|
||||
|
||||
Reference in new issue
Block a user