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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial