GeezerVison is showing github.com — feat: Add Jackson 3 support to @Jacksonized handlers · projectlombok/lombok@184d423
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/core/lombok/eclipse/handlers/HandleJacksonized.java +15-13Lines changed: 15 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,17 +90,19 @@ static boolean hasAnnotation(EclipseNode node, JacksonAnnotationType annotation) | 90 | 90 | return; | 91 | 91 | } | 92 | 92 |
| | 93 | + List<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode); | | 94 | + | 93 | 95 | boolean jacksonizedBuilder = builderAnnotationNode != null || superBuilderAnnotationNode != null; | 94 | 96 | if (jacksonizedBuilder) { | 95 | | - handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode); | | 97 | + handleJacksonizedBuilder(ast, annotationNode, annotatedNode, tdNode, td, builderAnnotationNode, superBuilderAnnotationNode, jacksonVersions); | 96 | 98 | } | 97 | 99 |
| 98 | 100 | if (accessorsAnnotationNode != null) { | 99 | 101 | handleJacksonizedAccessors(ast, annotationNode, annotatedNode, tdNode, td, accessorsAnnotationNode, jacksonizedBuilder); | 100 | 102 | } | 101 | 103 | } | 102 | 104 |
| 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) { | 104 | 106 | boolean isAbstract = (td.modifiers & ClassFileConstants.AccAbstract) != 0; | 105 | 107 | if (isAbstract) { | 106 | 108 | 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 | 141 | 143 | ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression); | 142 | 144 | MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess); | 143 | 145 |
| 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 | | - | 151 | 146 | if (jacksonVersions.contains(JacksonVersion.TWO)) { | 152 | 147 | td.annotations = addAnnotation(td, td.annotations, JacksonAnnotationType.JSON_DESERIALIZE2.getQualifiednameAsCharArrayArray(), builderMvp); | 153 | 148 | } | @@ -212,18 +207,18 @@ private void createJsonPropertyForField(EclipseNode fieldNode, EclipseNode annot | 212 | 207 | if (astNode instanceof FieldDeclaration) { | 213 | 208 | FieldDeclaration fd = (FieldDeclaration) astNode; | 214 | 209 | 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); | 216 | 211 | } | 217 | 212 | } | 218 | | - | | 213 | + | 219 | 214 | private void createJsonIgnoreForField(EclipseNode fieldNode, EclipseNode annotationNode) { | 220 | 215 | ASTNode astNode = fieldNode.get(); | 221 | 216 | if (astNode instanceof FieldDeclaration) { | 222 | 217 | 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()); | 224 | 219 | } | 225 | 220 | } | 226 | | - | | 221 | + | 227 | 222 | private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, EclipseNode annotatedNode, TypeDeclaration td, AnnotationValues<Builder> builderAnnotation) { | 228 | 223 | String builderClassName = builderAnnotation != null ? | 229 | 224 | builderAnnotation.getInstance().builderClassName() : null; | @@ -248,6 +243,13 @@ private String getBuilderClassName(Annotation ast, EclipseNode annotationNode, E | 248 | 243 | return builderClassName; | 249 | 244 | } | 250 | 245 |
| | 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 | + | 251 | 253 | private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0]; | 252 | 254 |
| 253 | 255 | private static Annotation[] findJacksonAnnotationsOnClass(TypeDeclaration td, EclipseNode node) { | |
Collapse file Copy file name to clipboardExpand all lines: src/core/lombok/javac/handlers/HandleJacksonized.java +23-21Lines changed: 23 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,9 +114,10 @@ private void handleJacksonizedAccessors(JavacNode annotationNode, JavacNode anno | 114 | 114 |
| 115 | 115 | // Add @JsonProperty to all non-transient fields. It will be automatically copied to the getter/setters later. | 116 | 116 | // Add @JsonIgnore to all transient fields. It will be automatically copied to the getter/setters later. | | 117 | + readConfiguredJacksonVersions(annotationNode); | 117 | 118 | for (JavacNode javacNode : tdNode.down()) { | 118 | 119 | if (javacNode.getKind() == Kind.FIELD) { | 119 | | - if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) || | | 120 | + if (hasAnnotation(javacNode, JacksonAnnotationType.JSON_PROPERTY2) || | 120 | 121 | hasAnnotation(javacNode, JacksonAnnotationType.JSON_IGNORE2)) { | 121 | 122 | continue; | 122 | 123 | } | @@ -131,22 +132,21 @@ private void handleJacksonizedAccessors(JavacNode annotationNode, JavacNode anno | 131 | 132 |
| 132 | 133 | private void createJsonPropertyForField(JavacNode fieldNode, JavacNode annotationNode) { | 133 | 134 | 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); | 140 | 137 | } | 141 | | - | | 138 | + | 142 | 139 | 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) { | 143 | 144 | 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); | 150 | 150 | } | 151 | 151 |
| 152 | 152 | 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 | 191 | 191 | return; | 192 | 192 | } | 193 | 193 |
| | 194 | + Collection<JacksonVersion> jacksonVersions = readConfiguredJacksonVersions(annotationNode); | | 195 | + | 194 | 196 | // Insert @JsonDeserialize on annotated class. | 195 | 197 | if (hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE2) || hasAnnotation(tdNode, JacksonAnnotationType.JSON_DESERIALIZE3)) { | 196 | 198 | annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson."); | 197 | 199 | return; | 198 | 200 | } | 199 | 201 |
| 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 | | - | 207 | 202 | if (jacksonVersions.contains(JacksonVersion.TWO)) { | 208 | 203 | JCExpression jsonDeserializeType = chainDots(annotatedNode, JacksonAnnotationType.JSON_DESERIALIZE2); | 209 | 204 | insertJsonDeserializeAnnotation(annotationNode, annotatedNode, tdNode, td, maker, builderClassName, jsonDeserializeType); | @@ -280,6 +275,13 @@ private String getBuilderClassName(JavacNode annotationNode, JavacNode annotated | 280 | 275 | return builderClassName; | 281 | 276 | } | 282 | 277 |
| | 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 | + | 283 | 285 | private static List<JCAnnotation> findJacksonAnnotationsOnClass(JavacNode node) { | 284 | 286 | ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>(); | 285 | 287 | for (JavacNode child : node.down()) { | |
|