1 package com.fasterxml.jackson.module.jaxb;
2
3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.core.Version;
5
6 /**
7  * Module that can be registered to add support for JAXB annotations.
8  * It does basically equivalent of
9  *<pre>
10  *   objectMapper.setAnnotationIntrospector(...);
11  *</pre>
12  * with combination of {@link JaxbAnnotationIntrospector} and existing
13  * default introspector(s) (if any), depending on configuration
14  * (by default, JAXB annotations are used as {@link Priority#PRIMARY}
15  * annotations).
16  */

17 public class JaxbAnnotationModule extends com.fasterxml.jackson.databind.Module
18 {
19     /**
20      * Enumeration that defines how we use JAXB Annotations: either
21      * as "primary" annotations (before any other already configured
22      * introspector -- most likely default JacksonAnnotationIntrospector) or
23      * as "secondary" annotations (after any other already configured
24      * introspector(s)).
25      *<p>
26      * Default choice is <b>PRIMARY</b>
27      *<p>
28      * Note that if you want to use JAXB annotations as the only annotations,
29      * you must directly set annotation introspector by calling 
30      * {@link com.fasterxml.jackson.databind.ObjectMapper#setAnnotationIntrospector}.
31      */

32     public enum Priority {
33         PRIMARY, SECONDARY;
34     }
35     
36     /**
37      * Priority to use when registering annotation introspector: default
38      * value is {@link Priority#PRIMARY}.
39      */

40     protected Priority _priority = Priority.PRIMARY;
41
42     /**
43      * If the introspector is explicitly set or passed, we'll hold on to that
44      * until registration.
45      *
46      * @since 2.7
47      */

48     protected JaxbAnnotationIntrospector _introspector;
49
50     /**
51      * Value to pass to
52      * {@link JaxbAnnotationIntrospector#setNonNillableInclusion}
53      * if defined and non-null.
54      *
55      * @since 2.7
56      */

57     protected JsonInclude.Include _nonNillableInclusion;
58
59     /*
60     /**********************************************************
61     /* Life cycle
62     /**********************************************************
63      */

64
65     public JaxbAnnotationModule() { }
66
67     /**
68      * @since 2.7
69      */

70     public JaxbAnnotationModule(JaxbAnnotationIntrospector intr) {
71         _introspector = intr;
72     }
73
74     @Override
75     public String getModuleName() {
76         return getClass().getSimpleName();
77     }
78
79     @Override
80     public Version version() {
81         return PackageVersion.VERSION;
82     }
83     
84     @Override
85     public void setupModule(SetupContext context)
86     {
87         JaxbAnnotationIntrospector intr = _introspector;
88         if (intr == null) {
89             intr = new JaxbAnnotationIntrospector(context.getTypeFactory());
90             if (_nonNillableInclusion != null) {
91                 intr.setNonNillableInclusion(_nonNillableInclusion);
92             }
93         }
94         switch (_priority) {
95         case PRIMARY:
96             context.insertAnnotationIntrospector(intr);
97             break;
98         case SECONDARY:
99             context.appendAnnotationIntrospector(intr);
100             break;
101         }
102     }
103
104     /*
105     /**********************************************************
106     /* Configuration
107     /**********************************************************
108      */

109     
110     /**
111      * Method for defining whether JAXB annotations should be added
112      * as primary or secondary annotations (compared to already registered
113      * annotations).
114      *<p>
115      * NOTE: method MUST be called before registering the module -- calling
116      * afterwards will not have any effect on previous registrations.
117      */

118     public JaxbAnnotationModule setPriority(Priority p) {
119         _priority = p;
120         return this;
121     }
122     
123     public Priority getPriority() { return _priority; }
124
125     /**
126      * @since 2.7
127      */

128     public JaxbAnnotationModule setNonNillableInclusion(JsonInclude.Include incl) {
129         _nonNillableInclusion = incl;
130         if (_introspector != null) {
131             _introspector.setNonNillableInclusion(incl);
132         }
133         return this;
134     }
135
136     /**
137      * @since 2.7
138      */

139     public JsonInclude.Include getNonNillableInclusion() {
140         return _nonNillableInclusion;
141     }
142 }
143