12
12
13
13
import javax .lang .model .element .AnnotationMirror ;
14
14
import javax .lang .model .element .ExecutableElement ;
15
+ import javax .lang .model .element .Name ;
15
16
import javax .lang .model .element .TypeElement ;
17
+ import javax .lang .model .util .Elements ;
16
18
17
19
class JacksonSupport {
18
20
19
21
private static final String JSON_DESERIALIZE =
20
22
"com.fasterxml.jackson.databind.annotation.JsonDeserialize" ;
21
23
private static final QualifiedName JSON_PROPERTY =
22
24
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" ;
25
27
/** Annotations which disable automatic generation of JsonProperty annotations. */
26
28
private static final Set <QualifiedName > DISABLE_PROPERTY_ANNOTATIONS = ImmutableSet .of (
27
29
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonAnyGetter" ),
28
30
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonIgnore" ),
29
31
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonUnwrapped" ),
30
32
QualifiedName .of ("com.fasterxml.jackson.annotation" , "JsonValue" ));
31
33
32
- public static Optional <JacksonSupport > create (TypeElement userValueType ) {
34
+ public static Optional <JacksonSupport > create (TypeElement userValueType , Elements elements ) {
33
35
return findAnnotationMirror (userValueType , JSON_DESERIALIZE )
34
- .map ($ -> new JacksonSupport ());
36
+ .map ($ -> new JacksonSupport (elements ));
35
37
}
36
38
37
- private JacksonSupport () {}
39
+ private final Elements elements ;
40
+
41
+ private JacksonSupport (Elements elements ) {
42
+ this .elements = elements ;
43
+ }
38
44
39
45
public void addJacksonAnnotations (
40
- Property .Builder resultBuilder , ExecutableElement getterMethod ) {
46
+ Property .Builder resultBuilder ,
47
+ ExecutableElement getterMethod ) {
41
48
Optional <AnnotationMirror > jsonPropertyAnnotation = findAnnotationMirror (getterMethod ,
42
49
JSON_PROPERTY );
43
50
if (jsonPropertyAnnotation .isPresent ()) {
@@ -47,12 +54,18 @@ public void addJacksonAnnotations(
47
54
"@%s(\" %s\" )%n" , JSON_PROPERTY , resultBuilder .getName ()));
48
55
}
49
56
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 );
56
69
}
57
70
58
71
private static boolean generateDefaultAnnotations (ExecutableElement getterMethod ) {
@@ -66,5 +79,4 @@ private static boolean generateDefaultAnnotations(ExecutableElement getterMethod
66
79
}
67
80
return true ;
68
81
}
69
-
70
82
}
0 commit comments