src/share/jaxws_classes/javax/xml/bind/annotation/XmlElementRef.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.annotation;
ohair@286 27
ohair@286 28 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
ohair@286 29 import java.lang.annotation.Retention;
ohair@286 30 import java.lang.annotation.Target;
ohair@286 31
ohair@286 32 import static java.lang.annotation.RetentionPolicy.RUNTIME;
ohair@286 33 import static java.lang.annotation.ElementType.FIELD;
ohair@286 34 import static java.lang.annotation.ElementType.METHOD;
ohair@286 35
ohair@286 36 /**
ohair@286 37 * <p>
ohair@286 38 * Maps a JavaBean property to a XML element derived from property's type.
ohair@286 39 * <p>
ohair@286 40 * <b>Usage</b>
ohair@286 41 * <p>
ohair@286 42 * <tt>&#64;XmlElementRef</tt> annotation can be used with a
ohair@286 43 * JavaBean property or from within {@link XmlElementRefs}
ohair@286 44 * <p>
ohair@286 45 * This annotation dynamically associates an XML element name with the JavaBean
ohair@286 46 * property. When a JavaBean property is annotated with {@link
ohair@286 47 * XmlElement}, the XML element name is statically derived from the
ohair@286 48 * JavaBean property name. However, when this annotation is used, the
ohair@286 49 * XML element name is derived from the instance of the type of the
ohair@286 50 * JavaBean property at runtime.
ohair@286 51 *
ohair@286 52 * <h3> XML Schema substitution group support </h3>
ohair@286 53 * XML Schema allows a XML document author to use XML element names
ohair@286 54 * that were not statically specified in the content model of a
ohair@286 55 * schema using substitution groups. Schema derived code provides
ohair@286 56 * support for substitution groups using an <i>element property</i>,
ohair@286 57 * (section 5.5.5, "Element Property" of JAXB 2.0 specification). An
ohair@286 58 * element property method signature is of the form:
ohair@286 59 * <pre>
ohair@286 60 * public void setTerm(JAXBElement<? extends Operator>);
ohair@286 61 * public JAXBElement<? extends Operator> getTerm();
ohair@286 62 * </pre>
ohair@286 63 * <p>
ohair@286 64 * An element factory method annotated with {@link XmlElementDecl} is
ohair@286 65 * used to create a <tt>JAXBElement</tt> instance, containing an XML
ohair@286 66 * element name. The presence of &#64;XmlElementRef annotation on an
ohair@286 67 * element property indicates that the element name from <tt>JAXBElement</tt>
ohair@286 68 * instance be used instead of deriving an XML element name from the
ohair@286 69 * JavaBean property name.
ohair@286 70 *
ohair@286 71 * <p>
ohair@286 72 * The usage is subject to the following constraints:
ohair@286 73 * <ul>
ohair@286 74 * <li> If the collection item type (for collection property) or
ohair@286 75 * property type (for single valued property) is
ohair@286 76 * {@link javax.xml.bind.JAXBElement}, then
ohair@286 77 * <tt>&#64;XmlElementRef}.name()</tt> and <tt>&#64;XmlElementRef.namespace()</tt> must
ohair@286 78 * point an element factory method with an @XmlElementDecl
ohair@286 79 * annotation in a class annotated with @XmlRegistry (usually
ohair@286 80 * ObjectFactory class generated by the schema compiler) :
ohair@286 81 * <ul>
ohair@286 82 * <li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li>
ohair@286 83 * <li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li>
ohair@286 84 * </ul>
ohair@286 85 * </li>
ohair@286 86 * <li> If the collection item type (for collection property) or
ohair@286 87 * property type (for single valued property) is not
ohair@286 88 * {@link javax.xml.bind.JAXBElement}, then the type referenced by the
ohair@286 89 * property or field must be annotated with {@link XmlRootElement}. </li>
ohair@286 90 * <li> This annotation can be used with the following annotations:
ohair@286 91 * {@link XmlElementWrapper}, {@link XmlJavaTypeAdapter}.
ohair@286 92 * </ul>
ohair@286 93 *
ohair@286 94 * <p>See "Package Specification" in javax.xml.bind.package javadoc for
ohair@286 95 * additional common information.</p>
ohair@286 96 *
ohair@286 97 * <p><b>Example 1: Ant Task Example</b></p>
ohair@286 98 * The following Java class hierarchy models an Ant build
ohair@286 99 * script. An Ant task corresponds to a class in the class
ohair@286 100 * hierarchy. The XML element name of an Ant task is indicated by the
ohair@286 101 * &#64;XmlRootElement annotation on its corresponding class.
ohair@286 102 * <pre>
ohair@286 103 * &#64;XmlRootElement(name="target")
ohair@286 104 * class Target {
ohair@286 105 * // The presence of &#64;XmlElementRef indicates that the XML
ohair@286 106 * // element name will be derived from the &#64;XmlRootElement
ohair@286 107 * // annotation on the type (for e.g. "jar" for JarTask).
ohair@286 108 * &#64;XmlElementRef
ohair@286 109 * List&lt;Task> tasks;
ohair@286 110 * }
ohair@286 111 *
ohair@286 112 * abstract class Task {
ohair@286 113 * }
ohair@286 114 *
ohair@286 115 * &#64;XmlRootElement(name="jar")
ohair@286 116 * class JarTask extends Task {
ohair@286 117 * ...
ohair@286 118 * }
ohair@286 119 *
ohair@286 120 * &#64;XmlRootElement(name="javac")
ohair@286 121 * class JavacTask extends Task {
ohair@286 122 * ...
ohair@286 123 * }
ohair@286 124 *
ohair@286 125 * &lt;!-- XML Schema fragment -->
ohair@286 126 * &lt;xs:element name="target" type="Target">
ohair@286 127 * &lt;xs:complexType name="Target">
ohair@286 128 * &lt;xs:sequence>
ohair@286 129 * &lt;xs:choice maxOccurs="unbounded">
ohair@286 130 * &lt;xs:element ref="jar">
ohair@286 131 * &lt;xs:element ref="javac">
ohair@286 132 * &lt;/xs:choice>
ohair@286 133 * &lt;/xs:sequence>
ohair@286 134 * &lt;/xs:complexType>
ohair@286 135 *
ohair@286 136 * </pre>
ohair@286 137 * <p>
ohair@286 138 * Thus the following code fragment:
ohair@286 139 * <pre>
ohair@286 140 * Target target = new Target();
ohair@286 141 * target.tasks.add(new JarTask());
ohair@286 142 * target.tasks.add(new JavacTask());
ohair@286 143 * marshal(target);
ohair@286 144 * </pre>
ohair@286 145 * will produce the following XML output:
ohair@286 146 * <pre>
ohair@286 147 * &lt;target>
ohair@286 148 * &lt;jar>
ohair@286 149 * ....
ohair@286 150 * &lt;/jar>
ohair@286 151 * &lt;javac>
ohair@286 152 * ....
ohair@286 153 * &lt;/javac>
ohair@286 154 * &lt;/target>
ohair@286 155 * </pre>
ohair@286 156 * <p>
ohair@286 157 * It is not an error to have a class that extends <tt>Task</tt>
ohair@286 158 * that doesn't have {@link XmlRootElement}. But they can't show up in an
ohair@286 159 * XML instance (because they don't have XML element names).
ohair@286 160 *
ohair@286 161 * <p><b>Example 2: XML Schema Susbstitution group support</b>
ohair@286 162 * <p> The following example shows the annotations for XML Schema
ohair@286 163 * substitution groups. The annotations and the ObjectFactory are
ohair@286 164 * derived from the schema.
ohair@286 165 *
ohair@286 166 * <pre>
ohair@286 167 * &#64;XmlElement
ohair@286 168 * class Math {
ohair@286 169 * // The value of {@link #type()}is
ohair@286 170 * // JAXBElement.class , which indicates the XML
ohair@286 171 * // element name ObjectFactory - in general a class marked
ohair@286 172 * // with &#64;XmlRegistry. (See ObjectFactory below)
ohair@286 173 * //
ohair@286 174 * // The {@link #name()} is "operator", a pointer to a
ohair@286 175 * // factory method annotated with a
ohair@286 176 * // {@link XmlElementDecl} with the name "operator". Since
ohair@286 177 * // "operator" is the head of a substitution group that
ohair@286 178 * // contains elements "add" and "sub" elements, "operator"
ohair@286 179 * // element can be substituted in an instance document by
ohair@286 180 * // elements "add" or "sub". At runtime, JAXBElement
ohair@286 181 * // instance contains the element name that has been
ohair@286 182 * // substituted in the XML document.
ohair@286 183 * //
ohair@286 184 * &#64;XmlElementRef(type=JAXBElement.class,name="operator")
ohair@286 185 * JAXBElement&lt;? extends Operator> term;
ohair@286 186 * }
ohair@286 187 *
ohair@286 188 * &#64;XmlRegistry
ohair@286 189 * class ObjectFactory {
ohair@286 190 * &#64;XmlElementDecl(name="operator")
ohair@286 191 * JAXBElement&lt;Operator> createOperator(Operator o) {...}
ohair@286 192 * &#64;XmlElementDecl(name="add",substitutionHeadName="operator")
ohair@286 193 * JAXBElement&lt;Operator> createAdd(Operator o) {...}
ohair@286 194 * &#64;XmlElementDecl(name="sub",substitutionHeadName="operator")
ohair@286 195 * JAXBElement&lt;Operator> createSub(Operator o) {...}
ohair@286 196 * }
ohair@286 197 *
ohair@286 198 * class Operator {
ohair@286 199 * ...
ohair@286 200 * }
ohair@286 201 * </pre>
ohair@286 202 * <p>
ohair@286 203 * Thus, the following code fragment
ohair@286 204 * <pre>
ohair@286 205 * Math m = new Math();
ohair@286 206 * m.term = new ObjectFactory().createAdd(new Operator());
ohair@286 207 * marshal(m);
ohair@286 208 * </pre>
ohair@286 209 * will produce the following XML output:
ohair@286 210 * <pre>
ohair@286 211 * &lt;math>
ohair@286 212 * &lt;add>...&lt;/add>
ohair@286 213 * &lt;/math>
ohair@286 214 * </pre>
ohair@286 215 *
ohair@286 216 *
ohair@286 217 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems,Inc. </li><li>Sekhar Vajjhala, Sun Microsystems, Inc.</li></ul>
ohair@286 218 * @see XmlElementRefs
ohair@286 219 * @since JAXB2.0
ohair@286 220 */
ohair@286 221 @Retention(RUNTIME)
ohair@286 222 @Target({FIELD,METHOD})
ohair@286 223 public @interface XmlElementRef {
ohair@286 224 /**
ohair@286 225 * The Java type being referenced.
ohair@286 226 * <p>
ohair@286 227 * If the value is DEFAULT.class, the type is inferred from the
ohair@286 228 * the type of the JavaBean property.
ohair@286 229 */
ohair@286 230 Class type() default DEFAULT.class;
ohair@286 231
ohair@286 232 /**
ohair@286 233 * This parameter and {@link #name()} are used to determine the
ohair@286 234 * XML element for the JavaBean property.
ohair@286 235 *
ohair@286 236 * <p> If <tt>type()</tt> is <tt>JAXBElement.class</tt> , then
ohair@286 237 * <tt>namespace()</tt> and <tt>name()</tt>
ohair@286 238 * point to a factory method with {@link XmlElementDecl}. The XML
ohair@286 239 * element name is the element name from the factory method's
ohair@286 240 * {@link XmlElementDecl} annotation or if an element from its
ohair@286 241 * substitution group (of which it is a head element) has been
ohair@286 242 * substituted in the XML document, then the element name is from the
ohair@286 243 * {@link XmlElementDecl} on the substituted element.
ohair@286 244 *
ohair@286 245 * <p> If {@link #type()} is not <tt>JAXBElement.class</tt>, then
ohair@286 246 * the XML element name is the XML element name statically
ohair@286 247 * associated with the type using the annotation {@link
ohair@286 248 * XmlRootElement} on the type. If the type is not annotated with
ohair@286 249 * an {@link XmlElementDecl}, then it is an error.
ohair@286 250 *
ohair@286 251 * <p> If <tt>type()</tt> is not <tt>JAXBElement.class</tt>, then
ohair@286 252 * this value must be "".
ohair@286 253 *
ohair@286 254 */
ohair@286 255 String namespace() default "";
ohair@286 256 /**
ohair@286 257 *
ohair@286 258 * @see #namespace()
ohair@286 259 */
ohair@286 260 String name() default "##default";
ohair@286 261
ohair@286 262 /**
ohair@286 263 * Used in {@link XmlElementRef#type()} to
ohair@286 264 * signal that the type be inferred from the signature
ohair@286 265 * of the property.
ohair@286 266 */
ohair@286 267 static final class DEFAULT {}
ohair@286 268
ohair@286 269 /**
ohair@286 270 * Customize the element declaration to be required.
ohair@286 271 * <p>
ohair@286 272 * If required() is true, then Javabean property is mapped to
ohair@286 273 * an XML schema element declaration with minOccurs="1".
ohair@286 274 * maxOccurs is "1" for a single valued property and "unbounded"
ohair@286 275 * for a multivalued property.
ohair@286 276 *
ohair@286 277 * <p>
ohair@286 278 * If required() is false, then the Javabean property is mapped
ohair@286 279 * to XML Schema element declaration with minOccurs="0".
ohair@286 280 * maxOccurs is "1" for a single valued property and "unbounded"
ohair@286 281 * for a multivalued property.
ohair@286 282 *
ohair@286 283 * <p>
ohair@286 284 * For compatibility with JAXB 2.1, this property defaults to <tt>true</tt>,
ohair@286 285 * despite the fact that {@link XmlElement#required()} defaults to false.
ohair@286 286 *
ohair@286 287 * @since 2.2
ohair@286 288 */
ohair@286 289 boolean required() default true;
ohair@286 290 }

mercurial