Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit e82d972

Browse files
authored
#396 Copy all Jackson XML annotations to accessor methods
2 parents fbe47d8 + 0ae6bed commit e82d972

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

src/main/java/org/inferred/freebuilder/processor/Analyser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ private Map<Property, PropertyCodeGenerator> pickPropertyGenerators(
411411
DeclaredType builder,
412412
Iterable<ExecutableElement> methods) {
413413
NamingConvention namingConvention = determineNamingConvention(type, methods, messager, types);
414-
Optional<JacksonSupport> jacksonSupport = JacksonSupport.create(type);
414+
Optional<JacksonSupport> jacksonSupport = JacksonSupport.create(type, elements);
415415
Set<String> methodsInvokedInBuilderConstructor =
416416
getMethodsInvokedInBuilderConstructor(asElement(builder));
417417

src/main/java/org/inferred/freebuilder/processor/JacksonSupport.java

+25-13
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,39 @@
1212

1313
import javax.lang.model.element.AnnotationMirror;
1414
import javax.lang.model.element.ExecutableElement;
15+
import javax.lang.model.element.Name;
1516
import javax.lang.model.element.TypeElement;
17+
import javax.lang.model.util.Elements;
1618

1719
class JacksonSupport {
1820

1921
private static final String JSON_DESERIALIZE =
2022
"com.fasterxml.jackson.databind.annotation.JsonDeserialize";
2123
private static final QualifiedName JSON_PROPERTY =
2224
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonProperty");
23-
private static final QualifiedName JACKSON_XML_PROPERTY =
24-
QualifiedName.of("com.fasterxml.jackson.dataformat.xml.annotation", "JacksonXmlProperty");
25+
private static final String JACKSON_XML_ANNOTATION_PACKAGE =
26+
"com.fasterxml.jackson.dataformat.xml.annotation";
2527
/** Annotations which disable automatic generation of JsonProperty annotations. */
2628
private static final Set<QualifiedName> DISABLE_PROPERTY_ANNOTATIONS = ImmutableSet.of(
2729
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonAnyGetter"),
2830
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonIgnore"),
2931
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonUnwrapped"),
3032
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonValue"));
3133

32-
public static Optional<JacksonSupport> create(TypeElement userValueType) {
34+
public static Optional<JacksonSupport> create(TypeElement userValueType, Elements elements) {
3335
return findAnnotationMirror(userValueType, JSON_DESERIALIZE)
34-
.map($ -> new JacksonSupport());
36+
.map($ -> new JacksonSupport(elements));
3537
}
3638

37-
private JacksonSupport() {}
39+
private final Elements elements;
40+
41+
private JacksonSupport(Elements elements) {
42+
this.elements = elements;
43+
}
3844

3945
public void addJacksonAnnotations(
40-
Property.Builder resultBuilder, ExecutableElement getterMethod) {
46+
Property.Builder resultBuilder,
47+
ExecutableElement getterMethod) {
4148
Optional<AnnotationMirror> jsonPropertyAnnotation = findAnnotationMirror(getterMethod,
4249
JSON_PROPERTY);
4350
if (jsonPropertyAnnotation.isPresent()) {
@@ -47,12 +54,18 @@ public void addJacksonAnnotations(
4754
"@%s(\"%s\")%n", JSON_PROPERTY, resultBuilder.getName()));
4855
}
4956

50-
Optional<AnnotationMirror> jacksonXmlPropertyAnnotation = findAnnotationMirror(getterMethod,
51-
JACKSON_XML_PROPERTY);
52-
if (jacksonXmlPropertyAnnotation.isPresent()) {
53-
resultBuilder
54-
.addAccessorAnnotations(Excerpts.add("%s%n", jacksonXmlPropertyAnnotation.get()));
55-
}
57+
getterMethod
58+
.getAnnotationMirrors()
59+
.stream()
60+
.filter(this::isXmlAnnotation)
61+
.forEach(annotation -> {
62+
resultBuilder.addAccessorAnnotations(code -> code.addLine("%s", annotation));
63+
});
64+
}
65+
66+
private boolean isXmlAnnotation(AnnotationMirror mirror) {
67+
Name pkg = elements.getPackageOf(mirror.getAnnotationType().asElement()).getQualifiedName();
68+
return pkg.contentEquals(JACKSON_XML_ANNOTATION_PACKAGE);
5669
}
5770

5871
private static boolean generateDefaultAnnotations(ExecutableElement getterMethod) {
@@ -66,5 +79,4 @@ private static boolean generateDefaultAnnotations(ExecutableElement getterMethod
6679
}
6780
return true;
6881
}
69-
7082
}

src/test/java/org/inferred/freebuilder/processor/JacksonSupportTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.fasterxml.jackson.annotation.JsonProperty;
2323
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2424
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
25+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
2526

2627
import org.inferred.freebuilder.processor.Analyser.CannotGenerateCodeException;
2728
import org.inferred.freebuilder.processor.util.Excerpt;
@@ -97,6 +98,22 @@ public void jacksonXmlAnnotationAddedWithExplicitName() throws CannotGenerateCod
9798
JacksonXmlProperty.class, "@JacksonXmlProperty(localName = \"b-ob\")");
9899
}
99100

101+
@Test
102+
public void jacksonXmlTextAnnotationAdded() throws CannotGenerateCodeException {
103+
// See also https://github.com/google/FreeBuilder/issues/68
104+
GeneratedBuilder builder = (GeneratedBuilder) analyser.analyse(model.newType(
105+
"package com.example;",
106+
"import " + JacksonXmlText.class.getName() + ";",
107+
"@" + JsonDeserialize.class.getName() + "(builder = DataType.Builder.class)",
108+
"public interface DataType {",
109+
" @JacksonXmlText String getFooBar();",
110+
" class Builder extends DataType_Builder {}",
111+
"}"));
112+
113+
Property property = getOnlyElement(builder.getGeneratorsByProperty().keySet());
114+
assertPropertyHasAnnotation(property, JacksonXmlText.class, "@JacksonXmlText");
115+
}
116+
100117
@Test
101118
public void jacksonAnnotationAddedWithImplicitName() throws CannotGenerateCodeException {
102119
// See also https://github.com/google/FreeBuilder/issues/90

0 commit comments

Comments
 (0)