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

Commit c9def25

Browse files
authored
#295 Pass through @JacksonXmlProperty annotations
2 parents 48c7cce + 6fa58b6 commit c9def25

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
testCompile jackson('databind')
5454
testCompile jackson('datatype-guava')
5555
testCompile jackson('datatype-jdk8')
56+
testCompile jackson('dataformat-xml')
5657
testCompile javassist
5758
testCompile junit
5859
testCompile mockito
@@ -63,6 +64,8 @@ String jackson(String pkg) {
6364
String namespace
6465
if (pkg.startsWith('datatype-')) {
6566
namespace = 'datatype'
67+
} else if (pkg.startsWith('dataformat-')) {
68+
namespace = 'dataformat'
6669
} else {
6770
namespace = 'core'
6871
}

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class JacksonSupport {
2121
"com.fasterxml.jackson.databind.annotation.JsonDeserialize";
2222
private static final QualifiedName JSON_PROPERTY =
2323
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonProperty");
24+
private static final QualifiedName JACKSON_XML_PROPERTY =
25+
QualifiedName.of("com.fasterxml.jackson.dataformat.xml.annotation", "JacksonXmlProperty");
2426
/** Annotations which disable automatic generation of JsonProperty annotations. */
2527
private static final Set<QualifiedName> DISABLE_PROPERTY_ANNOTATIONS = ImmutableSet.of(
2628
QualifiedName.of("com.fasterxml.jackson.annotation", "JsonAnyGetter"),
@@ -39,13 +41,21 @@ private JacksonSupport() {}
3941

4042
public void addJacksonAnnotations(
4143
Property.Builder resultBuilder, ExecutableElement getterMethod) {
42-
Optional<AnnotationMirror> annotation = findAnnotationMirror(getterMethod, JSON_PROPERTY);
43-
if (annotation.isPresent()) {
44-
resultBuilder.addAccessorAnnotations(Excerpts.add("%s%n", annotation.get()));
44+
Optional<AnnotationMirror> jsonPropertyAnnotation = findAnnotationMirror(getterMethod,
45+
JSON_PROPERTY);
46+
if (jsonPropertyAnnotation.isPresent()) {
47+
resultBuilder.addAccessorAnnotations(Excerpts.add("%s%n", jsonPropertyAnnotation.get()));
4548
} else if (generateDefaultAnnotations(getterMethod)) {
4649
resultBuilder.addAccessorAnnotations(Excerpts.add(
4750
"@%s(\"%s\")%n", JSON_PROPERTY, resultBuilder.getName()));
4851
}
52+
53+
Optional<AnnotationMirror> jacksonXmlPropertyAnnotation = findAnnotationMirror(getterMethod,
54+
JACKSON_XML_PROPERTY);
55+
if (jacksonXmlPropertyAnnotation.isPresent()) {
56+
resultBuilder
57+
.addAccessorAnnotations(Excerpts.add("%s%n", jacksonXmlPropertyAnnotation.get()));
58+
}
4959
}
5060

5161
private static boolean generateDefaultAnnotations(ExecutableElement getterMethod) {

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

+31-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.annotation.JsonAnyGetter;
2222
import com.fasterxml.jackson.annotation.JsonProperty;
2323
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
2425
import org.inferred.freebuilder.processor.Analyser.CannotGenerateCodeException;
2526
import org.inferred.freebuilder.processor.Metadata.Property;
2627
import org.inferred.freebuilder.processor.util.Excerpt;
@@ -34,6 +35,7 @@
3435
import org.junit.runners.JUnit4;
3536

3637
import java.util.Map;
38+
import java.util.Optional;
3739

3840
import javax.lang.model.element.TypeElement;
3941

@@ -85,7 +87,25 @@ public void jacksonAnnotationAddedWithExplicitName() throws CannotGenerateCodeEx
8587
Metadata metadata = analyser.analyse(dataType);
8688

8789
Property property = getOnlyElement(metadata.getProperties());
88-
assertPropertyHasJsonPropertyAnnotation(property, "bob");
90+
assertPropertyHasAnnotation(property, JsonProperty.class, "@JsonProperty(\"bob\")");
91+
}
92+
@Test
93+
public void jacksonXmlAnnotationAddedWithExplicitName() throws CannotGenerateCodeException {
94+
// See also https://github.com/google/FreeBuilder/issues/68
95+
TypeElement dataType = model.newType(
96+
"package com.example;",
97+
"import " + JacksonXmlProperty.class.getName() + ";",
98+
"@" + JsonDeserialize.class.getName() + "(builder = DataType.Builder.class)",
99+
"public interface DataType {",
100+
" @JacksonXmlProperty(localName=\"b-ob\") int getFooBar();",
101+
" class Builder extends DataType_Builder {}",
102+
"}");
103+
104+
Metadata metadata = analyser.analyse(dataType);
105+
106+
Property property = getOnlyElement(metadata.getProperties());
107+
assertPropertyHasAnnotation(property,
108+
JacksonXmlProperty.class, "@JacksonXmlProperty(localName = \"b-ob\")");
89109
}
90110

91111
@Test
@@ -102,7 +122,7 @@ public void jacksonAnnotationAddedWithImplicitName() throws CannotGenerateCodeEx
102122
Metadata metadata = analyser.analyse(dataType);
103123

104124
Property property = getOnlyElement(metadata.getProperties());
105-
assertPropertyHasJsonPropertyAnnotation(property, "fooBar");
125+
assertPropertyHasAnnotation(property, JsonProperty.class, "@JsonProperty(\"fooBar\")");
106126
}
107127

108128
@Test
@@ -122,11 +142,15 @@ public void jsonAnyGetterAnnotationDisablesImplicitProperty() throws CannotGener
122142
assertThat(property.getAccessorAnnotations()).named("property accessor annotations").isEmpty();
123143
}
124144

125-
private static void assertPropertyHasJsonPropertyAnnotation(
126-
Property property, String propertyName) {
127-
assertThat(property.getAccessorAnnotations()).named("property accessor annotations").hasSize(1);
128-
Excerpt accessorAnnotation = getOnlyElement(property.getAccessorAnnotations());
129-
assertThat(asString(accessorAnnotation)).isEqualTo("@JsonProperty(\"" + propertyName + "\")\n");
145+
private void assertPropertyHasAnnotation(Property property, Class annotationClass,
146+
String annotationString) {
147+
Optional<Excerpt> annotationExcerpt = property.getAccessorAnnotations()
148+
.stream()
149+
.filter(excerpt -> excerpt.toString().contains(annotationClass.getCanonicalName()))
150+
.findFirst();
151+
assertThat(annotationExcerpt).named("property accessor annotations").isNotNull();
152+
assertThat(asString(annotationExcerpt.get()))
153+
.isEqualTo(String.format("%s%n", annotationString));
130154
}
131155

132156
private static String asString(Excerpt excerpt) {

0 commit comments

Comments
 (0)