Skip to content

Commit 184d423

Browse files
mandreanrzwitserloot
authored andcommitted
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
1 parent e027ad0 commit 184d423

2 files changed

Lines changed: 38 additions & 34 deletions

File tree

src/core/lombok/eclipse/handlers/HandleJacksonized.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,19 @@ static boolean hasAnnotation(EclipseNode node, JacksonAnnotationType annotation)
9090
return;
9191
}
9292

93+
List<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode);
94+
9395
boolean jacksonizedBuilder = builderAnnotationNode != null || superBuilderAnnotationNode != null;
9496
if (jacksonizedBuilder) {
95-
handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode);
97+
handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode, jacksonVersions);
9698
}
9799

98100
if (accessorsAnnotationNode != null) {
99101
handleJacksonizedAccessors(ast, annotationNode, annotatedNode, tdNode, td, accessorsAnnotationNode, jacksonizedBuilder);
100102
}
101103
}
102104

103-
private void handleJacksonizedBuilder(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, EclipseNode tdNode, TypeDeclaration td, EclipseNode builderAnnotationNode, EclipseNode superBuilderAnnotationNode) {
105+
private void handleJacksonizedBuilder(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, EclipseNode tdNode, TypeDeclaration td, EclipseNode builderAnnotationNode, EclipseNode superBuilderAnnotationNode, List<JacksonVersion> jacksonVersions) {
104106
boolean isAbstract = (td.modifiers & ClassFileConstants.AccAbstract) != 0;
105107
if (isAbstract) {
106108
annotationNode.addError("Builders on abstract classes cannot be @Jacksonized (the builder would never be used).");
@@ -141,13 +143,6 @@ private void handleJacksonizedBuilder(Annotation ast, EclipseNode annotationNode
141143
ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression);
142144
MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess);
143145

144-
List<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
145-
146-
if (jacksonVersions.isEmpty()) {
147-
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
148-
jacksonVersions = Arrays.asList(JacksonVersion.TWO);
149-
}
150-
151146
if (jacksonVersions.contains(JacksonVersion.TWO)) {
152147
td.annotations = addAnnotation(td, td.annotations, JacksonAnnotationType.JSON_DESERIALIZE2.getQualifiednameAsCharArrayArray(), builderMvp);
153148
}
@@ -212,18 +207,18 @@ private void createJsonPropertyForField(EclipseNode fieldNode, EclipseNode annot
212207
if (astNode instanceof FieldDeclaration) {
213208
FieldDeclaration fd = (FieldDeclaration) astNode;
214209
StringLiteral fieldName = new StringLiteral(fd.name, 0, 0, 0);
215-
((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_PROPERTY2.getQualifiednameAsCharArrayArray(), fieldName);
210+
fd.annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_PROPERTY2.getQualifiednameAsCharArrayArray(), fieldName);
216211
}
217212
}
218-
213+
219214
private void createJsonIgnoreForField(EclipseNode fieldNode, EclipseNode annotationNode) {
220215
ASTNode astNode = fieldNode.get();
221216
if (astNode instanceof FieldDeclaration) {
222217
FieldDeclaration fd = (FieldDeclaration) astNode;
223-
((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_IGNORE2.getQualifiednameAsCharArrayArray());
218+
fd.annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotationType.JSON_IGNORE2.getQualifiednameAsCharArrayArray());
224219
}
225220
}
226-
221+
227222
private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, TypeDeclaration td, AnnotationValues<Builder> builderAnnotation) {
228223
String builderClassName = builderAnnotation != null ?
229224
builderAnnotation.getInstance().builderClassName() : null;
@@ -248,6 +243,13 @@ private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, E
248243
return builderClassName;
249244
}
250245

246+
private List<JacksonVersion> readConfiguredJacksonVersions(EclipseNode annotationNode) {
247+
List<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
248+
if (!jacksonVersions.isEmpty()) return jacksonVersions;
249+
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
250+
return Arrays.asList(JacksonVersion.TWO);
251+
}
252+
251253
private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
252254

253255
private static Annotation[] findJacksonAnnotationsOnClass(TypeDeclaration td, EclipseNode node) {

src/core/lombok/javac/handlers/HandleJacksonized.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ private void handleJacksonizedAccessors(JavacNode annotationNode, JavacNode anno
114114

115115
// Add @JsonProperty to all non-transient fields. It will be automatically copied to the getter/setters later.
116116
// Add @JsonIgnore to all transient fields. It will be automatically copied to the getter/setters later.
117+
readConfiguredJacksonVersions(annotationNode);
117118
for (JavacNode javacNode : tdNode.down()) {
118119
if (javacNode.getKind() == Kind.FIELD) {
119-
if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) ||
120+
if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) ||
120121
hasAnnotation(javacNode, JacksonAnnotationType.JSON_IGNORE2)) {
121122
continue;
122123
}
@@ -131,22 +132,21 @@ private void handleJacksonizedAccessors(JavacNode annotationNode, JavacNode anno
131132

132133
private void createJsonPropertyForField(JavacNode fieldNode, JavacNode annotationNode) {
133134
JavacTreeMaker maker = fieldNode.getTreeMaker();
134-
135-
JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_PROPERTY2);
136-
JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.<JCExpression>of(maker.Literal(fieldNode.getName())));
137-
recursiveSetGeneratedBy(annotationJsonProperty, annotationNode);
138-
JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get());
139-
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
135+
List<JCExpression> args = List.<JCExpression>of(maker.Literal(fieldNode.getName()));
136+
addFieldAnnotation(fieldNode, annotationNode, JacksonAnnotationType.JSON_PROPERTY2, args);
140137
}
141-
138+
142139
private void createJsonIgnoreForField(JavacNode fieldNode, JavacNode annotationNode) {
140+
addFieldAnnotation(fieldNode, annotationNode, JacksonAnnotationType.JSON_IGNORE2, List.<JCExpression>nil());
141+
}
142+
143+
private void addFieldAnnotation(JavacNode fieldNode, JavacNode annotationNode, JacksonAnnotationType annotationType, List<JCExpression> args) {
143144
JavacTreeMaker maker = fieldNode.getTreeMaker();
144-
145-
JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_IGNORE2);
146-
JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.<JCExpression>nil());
147-
recursiveSetGeneratedBy(annotationJsonProperty, annotationNode);
148-
JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get());
149-
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
145+
JCExpression type = chainDots(fieldNode, annotationType);
146+
JCAnnotation annotation = maker.Annotation(type, args);
147+
recursiveSetGeneratedBy(annotation, annotationNode);
148+
JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
149+
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotation);
150150
}
151151

152152
private void handleJacksonizedBuilder(JavacNode annotationNode, JavacNode annotatedNode, JavacNode tdNode, JCClassDecl td, JavacNode builderAnnotationNode, JavacNode superBuilderAnnotationNode) {
@@ -191,19 +191,14 @@ private void handleJacksonizedBuilder(JavacNode annotationNode, JavacNode annota
191191
return;
192192
}
193193

194+
Collection<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode);
195+
194196
// Insert @JsonDeserialize on annotated class.
195197
if (hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE2) || hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE3)) {
196198
annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson.");
197199
return;
198200
}
199201

200-
Collection<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
201-
202-
if (jacksonVersions.isEmpty()) {
203-
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
204-
jacksonVersions = Arrays.asList(JacksonVersion.TWO);
205-
}
206-
207202
if (jacksonVersions.contains(JacksonVersion.TWO)) {
208203
JCExpression jsonDeserializeType = chainDots(annotatedNode, JacksonAnnotationType.JSON_DESERIALIZE2);
209204
insertJsonDeserializeAnnotation(annotationNode, annotatedNode, tdNode, td, maker, builderClassName, jsonDeserializeType);
@@ -280,6 +275,13 @@ private String getBuilderClassName(JavacNode annotationNode, JavacNode annotated
280275
return builderClassName;
281276
}
282277

278+
private Collection<JacksonVersion> readConfiguredJacksonVersions(JavacNode annotationNode) {
279+
Collection<JacksonVersion> jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.<JacksonVersion>asList());
280+
if (!jacksonVersions.isEmpty()) return jacksonVersions;
281+
annotationNode.addWarning("Ambiguous: Jackson2 and Jackson3 exist; define which variant(s) you want in 'lombok.config'. See https://projectlombok.org/features/experimental/Jacksonized");
282+
return Arrays.asList(JacksonVersion.TWO);
283+
}
284+
283285
private static List<JCAnnotation> findJacksonAnnotationsOnClass(JavacNode node) {
284286
ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
285287
for (JavacNode child : node.down()) {

0 commit comments

Comments
 (0)