Skip to content

Commit 5259a26

Browse files
committed
Revert "Remove redundant @JsonIgnore annotation"
This reverts commit 3a49c0a.
1 parent 3a49c0a commit 5259a26

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ private void handleJacksonizedAccessors(Annotation ast, EclipseNode annotationNo
187187

188188
// Add @JsonProperty to all fields. It will be automatically copied to the getter/setters later.
189189
for (EclipseNode eclipseNode : tdNode.down()) {
190-
if (eclipseNode.getKind() != Kind.FIELD || eclipseNode.isTransient()) {
191-
continue;
190+
if (eclipseNode.getKind() == Kind.FIELD) {
191+
if (eclipseNode.isTransient()) createJsonIgnoreForField(eclipseNode, annotationNode);
192+
else createJsonPropertyForField(eclipseNode, annotationNode);
192193
}
193-
createJsonPropertyForField(eclipseNode, annotationNode);
194194
}
195195
tdNode.rebuild();
196196
}
@@ -205,6 +205,15 @@ private void createJsonPropertyForField(EclipseNode fieldNode, EclipseNode annot
205205
}
206206
}
207207

208+
private void createJsonIgnoreForField(EclipseNode fieldNode, EclipseNode annotationNode) {
209+
if (JacksonAnnotations.JSON_IGNORE.isAnnotating(fieldNode)) return;
210+
ASTNode astNode = fieldNode.get();
211+
if (astNode instanceof FieldDeclaration) {
212+
FieldDeclaration fd = (FieldDeclaration)astNode;
213+
((FieldDeclaration) astNode).annotations = addAnnotation(fieldNode.get(), fd.annotations, JacksonAnnotations.JSON_IGNORE.annotation);
214+
}
215+
}
216+
208217
private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, TypeDeclaration td, AnnotationValues<Builder> builderAnnotation) {
209218
String builderClassName = builderAnnotation != null ?
210219
builderAnnotation.getInstance().builderClassName() : null;

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public class HandleJacksonized extends JavacAnnotationHandler<Jacksonized> {
6363
private static enum JacksonAnnotations {
6464
JSON_POJO_BUILDER("com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder"),
6565
JSON_DESERIALIZE("com.fasterxml.jackson.databind.annotation.JsonDeserialize"),
66-
JSON_PROPERTY("com.fasterxml.jackson.annotation.JsonProperty");
66+
JSON_PROPERTY("com.fasterxml.jackson.annotation.JsonProperty"),
67+
JSON_IGNORE("com.fasterxml.jackson.annotation.JsonIgnore");
6768

6869
private final String qualifiedName;
6970
private final String[] chainedDots;
@@ -118,11 +119,12 @@ private void handleJacksonizedAccessors(JavacNode annotationNode, JavacNode anno
118119
}
119120

120121
// Add @JsonProperty to all non-transient fields. It will be automatically copied to the getter/setters later.
122+
// Add @JsonIgnore to all transient fields. It will be automatically copied to the getter/setters later.
121123
for (JavacNode javacNode : tdNode.down()) {
122-
if (javacNode.getKind() != Kind.FIELD || javacNode.isTransient()) {
123-
continue;
124+
if (javacNode.getKind() == Kind.FIELD) {
125+
if (javacNode.isTransient()) createJsonIgnoreForField(javacNode, annotationNode);
126+
else createJsonPropertyForField(javacNode, annotationNode);
124127
}
125-
createJsonPropertyForField(javacNode, annotationNode);
126128
}
127129
}
128130

@@ -139,6 +141,19 @@ private void createJsonPropertyForField(JavacNode fieldNode, JavacNode annotatio
139141
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
140142
}
141143

144+
private void createJsonIgnoreForField(JavacNode fieldNode, JavacNode annotationNode) {
145+
if (JacksonAnnotations.JSON_IGNORE.isAnnotating(fieldNode)) {
146+
return;
147+
}
148+
JavacTreeMaker maker = fieldNode.getTreeMaker();
149+
150+
JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotations.JSON_IGNORE.chainedDots);
151+
JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.<JCExpression>nil());
152+
recursiveSetGeneratedBy(annotationJsonProperty, annotationNode);
153+
JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get());
154+
fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty);
155+
}
156+
142157
private void handleJacksonizedBuilder(JavacNode annotationNode, JavacNode annotatedNode, JavacNode tdNode, JCClassDecl td, JavacNode builderAnnotationNode, JavacNode superBuilderAnnotationNode) {
143158
if (builderAnnotationNode != null && superBuilderAnnotationNode != null) {
144159
annotationNode.addError("@Jacksonized cannot process both @Builder and @SuperBuilder on the same class.");

test/transform/resource/after-delombok/JacksonizedAccessorsTransient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
public class JacksonizedAccessorsTransient {
2+
@com.fasterxml.jackson.annotation.JsonIgnore
23
private transient int intValue;
4+
@com.fasterxml.jackson.annotation.JsonIgnore
35
@java.lang.SuppressWarnings("all")
46
@lombok.Generated
57
public int intValue() {
@@ -8,6 +10,7 @@ public int intValue() {
810
/**
911
* @return {@code this}.
1012
*/
13+
@com.fasterxml.jackson.annotation.JsonIgnore
1114
@java.lang.SuppressWarnings("all")
1215
@lombok.Generated
1316
public JacksonizedAccessorsTransient intValue(final int intValue) {

test/transform/resource/after-ecj/JacksonizedAccessorsTransient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
public @lombok.extern.jackson.Jacksonized @lombok.experimental.Accessors(fluent = true) @lombok.Getter @lombok.Setter class JacksonizedAccessorsTransient {
2-
private transient int intValue;
2+
private transient @com.fasterxml.jackson.annotation.JsonIgnore int intValue;
33
public JacksonizedAccessorsTransient() {
44
super();
55
}
6-
public @java.lang.SuppressWarnings("all") @lombok.Generated int intValue() {
6+
public @com.fasterxml.jackson.annotation.JsonIgnore @java.lang.SuppressWarnings("all") @lombok.Generated int intValue() {
77
return this.intValue;
88
}
99
/**
1010
* @return {@code this}.
1111
*/
12-
public @java.lang.SuppressWarnings("all") @lombok.Generated JacksonizedAccessorsTransient intValue(final int intValue) {
12+
public @com.fasterxml.jackson.annotation.JsonIgnore @java.lang.SuppressWarnings("all") @lombok.Generated JacksonizedAccessorsTransient intValue(final int intValue) {
1313
this.intValue = intValue;
1414
return this;
1515
}

0 commit comments

Comments
 (0)