001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.bind.annotation;
027
028 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
029 import java.lang.annotation.Retention;
030 import java.lang.annotation.Target;
031
032 import static java.lang.annotation.RetentionPolicy.RUNTIME;
033 import static java.lang.annotation.ElementType.FIELD;
034 import static java.lang.annotation.ElementType.METHOD;
035
036 /**
037 * <p>
038 * Maps a JavaBean property to a XML element derived from property's type.
039 * <p>
040 * <b>Usage</b>
041 * <p>
042 * <tt>@XmlElementRef</tt> annotation can be used with a
043 * JavaBean property or from within {@link XmlElementRefs}
044 * <p>
045 * This annotation dynamically associates an XML element name with the JavaBean
046 * property. When a JavaBean property is annotated with {@link
047 * XmlElement}, the XML element name is statically derived from the
048 * JavaBean property name. However, when this annotation is used, the
049 * XML element name is derived from the instance of the type of the
050 * JavaBean property at runtime.
051 *
052 * <h3> XML Schema substitution group support </h3>
053 * XML Schema allows a XML document author to use XML element names
054 * that were not statically specified in the content model of a
055 * schema using substitution groups. Schema derived code provides
056 * support for substitution groups using an <i>element property</i>,
057 * (section 5.5.5, "Element Property" of JAXB 2.0 specification). An
058 * element property method signature is of the form:
059 * <pre><xmp>
060 * public void setTerm(JAXBElement<? extends Operator>);
061 * public JAXBElement<? extends Operator> getTerm();
062 * </xmp></pre>
063 * <p>
064 * An element factory method annotated with {@link XmlElementDecl} is
065 * used to create a <tt>JAXBElement</tt> instance, containing an XML
066 * element name. The presence of @XmlElementRef annotation on an
067 * element property indicates that the element name from <tt>JAXBElement</tt>
068 * instance be used instead of deriving an XML element name from the
069 * JavaBean property name.
070 *
071 * <p>
072 * The usage is subject to the following constraints:
073 * <ul>
074 * <li> If the collection item type (for collection property) or
075 * property type (for single valued property) is
076 * {@link javax.xml.bind.JAXBElement}, then
077 * <tt>@XmlElementRef}.name()</tt> and <tt>@XmlElementRef.namespace()</tt> must
078 * point an element factory method with an @XmlElementDecl
079 * annotation in a class annotated with @XmlRegistry (usually
080 * ObjectFactory class generated by the schema compiler) :
081 * <ul>
082 * <li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li>
083 * <li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li>
084 * </ul>
085 * </li>
086 * <li> If the collection item type (for collection property) or
087 * property type (for single valued property) is not
088 * {@link javax.xml.bind.JAXBElement}, then the type referenced by the
089 * property or field must be annotated with {@link XmlRootElement}. </li>
090 * <li> This annotation can be used with the following annotations:
091 * {@link XmlElementWrapper}, {@link XmlJavaTypeAdapter}.
092 * </ul>
093 *
094 * <p>See "Package Specification" in javax.xml.bind.package javadoc for
095 * additional common information.</p>
096 *
097 * <p><b>Example 1: Ant Task Example</b></p>
098 * The following Java class hierarchy models an Ant build
099 * script. An Ant task corresponds to a class in the class
100 * hierarchy. The XML element name of an Ant task is indicated by the
101 * @XmlRootElement annotation on its corresponding class.
102 * <pre>
103 * @XmlRootElement(name="target")
104 * class Target {
105 * // The presence of @XmlElementRef indicates that the XML
106 * // element name will be derived from the @XmlRootElement
107 * // annotation on the type (for e.g. "jar" for JarTask).
108 * @XmlElementRef
109 * List<Task> tasks;
110 * }
111 *
112 * abstract class Task {
113 * }
114 *
115 * @XmlRootElement(name="jar")
116 * class JarTask extends Task {
117 * ...
118 * }
119 *
120 * @XmlRootElement(name="javac")
121 * class JavacTask extends Task {
122 * ...
123 * }
124 *
125 * <!-- XML Schema fragment -->
126 * <xs:element name="target" type="Target">
127 * <xs:complexType name="Target">
128 * <xs:sequence>
129 * <xs:choice maxOccurs="unbounded">
130 * <xs:element ref="jar">
131 * <xs:element ref="javac">
132 * </xs:choice>
133 * </xs:sequence>
134 * </xs:complexType>
135 *
136 * </pre>
137 * <p>
138 * Thus the following code fragment:
139 * <pre>
140 * Target target = new Target();
141 * target.tasks.add(new JarTask());
142 * target.tasks.add(new JavacTask());
143 * marshal(target);
144 * </pre>
145 * will produce the following XML output:
146 * <pre><xmp>
147 * <target>
148 * <jar>
149 * ....
150 * </jar>
151 * <javac>
152 * ....
153 * </javac>
154 * </target>
155 * </xmp></pre>
156 * <p>
157 * It is not an error to have a class that extends <tt>Task</tt>
158 * that doesn't have {@link XmlRootElement}. But they can't show up in an
159 * XML instance (because they don't have XML element names).
160 *
161 * <p><b>Example 2: XML Schema Susbstitution group support</b>
162 * <p> The following example shows the annotations for XML Schema
163 * substitution groups. The annotations and the ObjectFactory are
164 * derived from the schema.
165 *
166 * <pre>
167 * @XmlElement
168 * class Math {
169 * // The value of {@link #type()}is
170 * // JAXBElement.class , which indicates the XML
171 * // element name ObjectFactory - in general a class marked
172 * // with @XmlRegistry. (See ObjectFactory below)
173 * //
174 * // The {@link #name()} is "operator", a pointer to a
175 * // factory method annotated with a
176 * // {@link XmlElementDecl} with the name "operator". Since
177 * // "operator" is the head of a substitution group that
178 * // contains elements "add" and "sub" elements, "operator"
179 * // element can be substituted in an instance document by
180 * // elements "add" or "sub". At runtime, JAXBElement
181 * // instance contains the element name that has been
182 * // substituted in the XML document.
183 * //
184 * @XmlElementRef(type=JAXBElement.class,name="operator")
185 * JAXBElement<? extends Operator> term;
186 * }
187 *
188 * @XmlRegistry
189 * class ObjectFactory {
190 * @XmlElementDecl(name="operator")
191 * JAXBElement<Operator> createOperator(Operator o) {...}
192 * @XmlElementDecl(name="add",substitutionHeadName="operator")
193 * JAXBElement<Operator> createAdd(Operator o) {...}
194 * @XmlElementDecl(name="sub",substitutionHeadName="operator")
195 * JAXBElement<Operator> createSub(Operator o) {...}
196 * }
197 *
198 * class Operator {
199 * ...
200 * }
201 * </pre>
202 * <p>
203 * Thus, the following code fragment
204 * <pre>
205 * Math m = new Math();
206 * m.term = new ObjectFactory().createAdd(new Operator());
207 * marshal(m);
208 * </pre>
209 * will produce the following XML output:
210 * <pre>
211 * <math>
212 * <add>...</add>
213 * </math>
214 * </pre>
215 *
216 *
217 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems,Inc. </li><li>Sekhar Vajjhala, Sun Microsystems, Inc.</li></ul>
218 * @see XmlElementRefs
219 * @since JAXB2.0
220 */
221 @Retention(RUNTIME)
222 @Target({FIELD,METHOD})
223 public @interface XmlElementRef {
224 /**
225 * The Java type being referenced.
226 * <p>
227 * If the value is DEFAULT.class, the type is inferred from the
228 * the type of the JavaBean property.
229 */
230 Class type() default DEFAULT.class;
231
232 /**
233 * This parameter and {@link #name()} are used to determine the
234 * XML element for the JavaBean property.
235 *
236 * <p> If <tt>type()</tt> is <tt>JAXBElement.class</tt> , then
237 * <tt>namespace()</tt> and <tt>name()</tt>
238 * point to a factory method with {@link XmlElementDecl}. The XML
239 * element name is the element name from the factory method's
240 * {@link XmlElementDecl} annotation or if an element from its
241 * substitution group (of which it is a head element) has been
242 * substituted in the XML document, then the element name is from the
243 * {@link XmlElementDecl} on the substituted element.
244 *
245 * <p> If {@link #type()} is not <tt>JAXBElement.class</tt>, then
246 * the XML element name is the XML element name statically
247 * associated with the type using the annotation {@link
248 * XmlRootElement} on the type. If the type is not annotated with
249 * an {@link XmlElementDecl}, then it is an error.
250 *
251 * <p> If <tt>type()</tt> is not <tt>JAXBElement.class</tt>, then
252 * this value must be "".
253 *
254 */
255 String namespace() default "";
256
257 /**
258 *
259 * @see #namespace()
260 */
261 String name() default "##default";
262
263 /**
264 * Used in {@link XmlElementRef#type()} to
265 * signal that the type be inferred from the signature
266 * of the property.
267 */
268 static final class DEFAULT {
269 }
270 }
|