19
19
20
20
class JacksonSupport {
21
21
22
+ private enum GenerateAnnotation {
23
+ DEFAULT ,
24
+ JSON_ANY ,
25
+ NONE
26
+ }
27
+
22
28
private static final String JSON_DESERIALIZE =
23
29
"com.fasterxml.jackson.databind.annotation.JsonDeserialize" ;
24
30
private static final QualifiedName JSON_PROPERTY =
25
31
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonProperty" );
26
32
private static final String JACKSON_XML_ANNOTATION_PACKAGE =
27
33
"com.fasterxml.jackson.dataformat.xml.annotation" ;
34
+ private static final QualifiedName JSON_ANY_GETTER =
35
+ QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonAnyGetter" );
36
+ private static final QualifiedName JSON_ANY_SETTER =
37
+ QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonAnySetter" );
28
38
/** Annotations which disable automatic generation of JsonProperty annotations. */
29
39
private static final Set <QualifiedName > DISABLE_PROPERTY_ANNOTATIONS = ImmutableSet .of (
30
- QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonAnyGetter" ),
31
40
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonIgnore" ),
32
41
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonUnwrapped" ),
33
42
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonValue" ));
@@ -50,9 +59,20 @@ public void addJacksonAnnotations(
50
59
JSON_PROPERTY );
51
60
if (jsonPropertyAnnotation .isPresent ()) {
52
61
resultBuilder .addAccessorAnnotations (Excerpts .add ("%s%n" , jsonPropertyAnnotation .get ()));
53
- } else if (generateDefaultAnnotations (getterMethod )) {
54
- resultBuilder .addAccessorAnnotations (Excerpts .add (
55
- "@%s(\" %s\" )%n" , JSON_PROPERTY , resultBuilder .getName ()));
62
+ } else {
63
+ switch (generateDefaultAnnotations (getterMethod )) {
64
+ case DEFAULT :
65
+ resultBuilder .addAccessorAnnotations (Excerpts .add (
66
+ "@%s(\" %s\" )%n" , JSON_PROPERTY , resultBuilder .getName ()));
67
+ break ;
68
+ case JSON_ANY :
69
+ resultBuilder .addPutAnnotations (Excerpts .add ("@%s%n" , JSON_ANY_SETTER ));
70
+ resultBuilder .addGetterAnnotations (Excerpts .add ("@%s%n" , JSON_ANY_GETTER ));
71
+ break ;
72
+ case NONE :
73
+ default :
74
+ break ;
75
+ }
56
76
}
57
77
58
78
getterMethod
@@ -69,15 +89,18 @@ private boolean isXmlAnnotation(AnnotationMirror mirror) {
69
89
return pkg .contentEquals (JACKSON_XML_ANNOTATION_PACKAGE );
70
90
}
71
91
72
- private static boolean generateDefaultAnnotations (ExecutableElement getterMethod ) {
92
+ private static GenerateAnnotation generateDefaultAnnotations (ExecutableElement getterMethod ) {
73
93
for (AnnotationMirror annotationMirror : getterMethod .getAnnotationMirrors ()) {
74
94
TypeElement annotationTypeElement =
75
95
(TypeElement ) (annotationMirror .getAnnotationType ().asElement ());
76
96
QualifiedName annotationType = QualifiedName .of (annotationTypeElement );
77
97
if (DISABLE_PROPERTY_ANNOTATIONS .contains (annotationType )) {
78
- return false ;
98
+ return GenerateAnnotation .NONE ;
99
+ }
100
+ if (JSON_ANY_GETTER .equals (annotationType )) {
101
+ return GenerateAnnotation .JSON_ANY ;
79
102
}
80
103
}
81
- return true ;
104
+ return GenerateAnnotation . DEFAULT ;
82
105
}
83
106
}
0 commit comments