diff --git a/src/core/lombok/eclipse/handlers/HandleJacksonized.java b/src/core/lombok/eclipse/handlers/HandleJacksonized.java index 360e499f..cd8821d5 100644 --- a/src/core/lombok/eclipse/handlers/HandleJacksonized.java +++ b/src/core/lombok/eclipse/handlers/HandleJacksonized.java @@ -90,9 +90,11 @@ public class HandleJacksonized extends EclipseAnnotationHandler { return; } + List 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 { } } - 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 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 { ClassLiteralAccess builderClassLiteralAccess = new ClassLiteralAccess(td.sourceEnd, builderClassExpression); MemberValuePair builderMvp = new MemberValuePair("builder".toCharArray(), td.sourceStart, td.sourceEnd, builderClassLiteralAccess); - List jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.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 { 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 builderAnnotation) { String builderClassName = builderAnnotation != null ? builderAnnotation.getInstance().builderClassName() : null; @@ -248,6 +243,13 @@ public class HandleJacksonized extends EclipseAnnotationHandler { return builderClassName; } + private List readConfiguredJacksonVersions(EclipseNode annotationNode) { + List jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.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) { diff --git a/src/core/lombok/javac/handlers/HandleJacksonized.java b/src/core/lombok/javac/handlers/HandleJacksonized.java index e5484d38..a8b8555a 100644 --- a/src/core/lombok/javac/handlers/HandleJacksonized.java +++ b/src/core/lombok/javac/handlers/HandleJacksonized.java @@ -114,9 +114,10 @@ public class HandleJacksonized extends JavacAnnotationHandler { // 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 { private void createJsonPropertyForField(JavacNode fieldNode, JavacNode annotationNode) { JavacTreeMaker maker = fieldNode.getTreeMaker(); - - JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_PROPERTY2); - JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.of(maker.Literal(fieldNode.getName()))); - recursiveSetGeneratedBy(annotationJsonProperty, annotationNode); - JCVariableDecl fieldDecl = ((JCVariableDecl)fieldNode.get()); - fieldDecl.mods.annotations = fieldDecl.mods.annotations.append(annotationJsonProperty); + List args = List.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.nil()); + } + + private void addFieldAnnotation(JavacNode fieldNode, JavacNode annotationNode, JacksonAnnotationType annotationType, List args) { JavacTreeMaker maker = fieldNode.getTreeMaker(); - - JCExpression jsonPropertyType = chainDots(fieldNode, JacksonAnnotationType.JSON_IGNORE2); - JCAnnotation annotationJsonProperty = maker.Annotation(jsonPropertyType, List.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 { return; } + Collection 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 jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.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 { return builderClassName; } + private Collection readConfiguredJacksonVersions(JavacNode annotationNode) { + Collection jacksonVersions = annotationNode.getAst().readConfigurationOr(ConfigurationKeys.JACKSONIZED_JACKSON_VERSION, Arrays.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 findJacksonAnnotationsOnClass(JavacNode node) { ListBuffer result = new ListBuffer(); for (JavacNode child : node.down()) {