blob: 7e155e40046eda3a2543eac58aba1ebfb2088db7 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<title>XmlElementRef</title>
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../../../script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
try {
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="XmlElementRef";
}
}
catch(err) {
}
//-->
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../../../javax/xml/bind/annotation/XmlElementDecl.GLOBAL.html" title="class in javax.xml.bind.annotation"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../../../javax/xml/bind/annotation/XmlElementRef.DEFAULT.html" title="class in javax.xml.bind.annotation"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?javax/xml/bind/annotation/XmlElementRef.html" target="_top">Frames</a></li>
<li><a href="XmlElementRef.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_top");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Required&nbsp;|&nbsp;</li>
<li><a href="#annotation.type.optional.element.summary">Optional</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li><a href="#annotation.type.element.detail">Element</a></li>
</ul>
</div>
<a name="skip.navbar.top">
<!-- -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<div class="subTitle">javax.xml.bind.annotation</div>
<h2 title="Annotation Type XmlElementRef" class="title">Annotation Type XmlElementRef</h2>
</div>
<div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<hr>
<br>
<pre>@Retention(value=RUNTIME)
@Target(value={FIELD,METHOD})
public @interface <span class="memberNameLabel">XmlElementRef</span></pre>
<div class="block"><p>
Maps a JavaBean property to a XML element derived from property's type.
<p>
<b>Usage</b>
<p>
<code>@XmlElementRef</code> annotation can be used with a
JavaBean property or from within <a href="../../../../javax/xml/bind/annotation/XmlElementRefs.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementRefs</code></a>
<p>
This annotation dynamically associates an XML element name with the JavaBean
property. When a JavaBean property is annotated with <a href="../../../../javax/xml/bind/annotation/XmlElement.html" title="annotation in javax.xml.bind.annotation"><code>XmlElement</code></a>, the XML element name is statically derived from the
JavaBean property name. However, when this annotation is used, the
XML element name is derived from the instance of the type of the
JavaBean property at runtime.
<h3> XML Schema substitution group support </h3>
XML Schema allows a XML document author to use XML element names
that were not statically specified in the content model of a
schema using substitution groups. Schema derived code provides
support for substitution groups using an <i>element property</i>,
(section 5.5.5, "Element Property" of JAXB 2.0 specification). An
element property method signature is of the form:
<pre><code>
public void setTerm(JAXBElement&lt;? extends Operator&gt;);
public JAXBElement&lt;? extends Operator&gt; getTerm();
</code></pre>
<p>
An element factory method annotated with <a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a> is
used to create a <code>JAXBElement</code> instance, containing an XML
element name. The presence of <code>@XmlElementRef</code> annotation on an
element property indicates that the element name from <code>JAXBElement</code>
instance be used instead of deriving an XML element name from the
JavaBean property name.
<p>
The usage is subject to the following constraints:
<ul>
<li> If the collection item type (for collection property) or
property type (for single valued property) is
<a href="../../../../javax/xml/bind/JAXBElement.html" title="class in javax.xml.bind"><code>JAXBElement</code></a>, then
<code>@XmlElementRef.name()</code> and <code>@XmlElementRef.namespace()</code> must
point an element factory method with an @XmlElementDecl
annotation in a class annotated with @XmlRegistry (usually
ObjectFactory class generated by the schema compiler) :
<ul>
<li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li>
<li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li>
</ul>
</li>
<li> If the collection item type (for collection property) or
property type (for single valued property) is not
<a href="../../../../javax/xml/bind/JAXBElement.html" title="class in javax.xml.bind"><code>JAXBElement</code></a>, then the type referenced by the
property or field must be annotated with <a href="../../../../javax/xml/bind/annotation/XmlRootElement.html" title="annotation in javax.xml.bind.annotation"><code>XmlRootElement</code></a>. </li>
<li> This annotation can be used with the following annotations:
<a href="../../../../javax/xml/bind/annotation/XmlElementWrapper.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementWrapper</code></a>, <a href="../../../../javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html" title="annotation in javax.xml.bind.annotation.adapters"><code>XmlJavaTypeAdapter</code></a>.
</ul>
<p>See "Package Specification" in javax.xml.bind.package javadoc for
additional common information.</p>
<p><b>Example 1: Ant Task Example</b></p>
The following Java class hierarchy models an Ant build
script. An Ant task corresponds to a class in the class
hierarchy. The XML element name of an Ant task is indicated by the
&#64;XmlRootElement annotation on its corresponding class.
<pre>
&#64;XmlRootElement(name="target")
class Target {
// The presence of &#64;XmlElementRef indicates that the XML
// element name will be derived from the &#64;XmlRootElement
// annotation on the type (for e.g. "jar" for JarTask).
&#64;XmlElementRef
List&lt;Task&gt; tasks;
}
abstract class Task {
}
&#64;XmlRootElement(name="jar")
class JarTask extends Task {
...
}
&#64;XmlRootElement(name="javac")
class JavacTask extends Task {
...
}
<code>
&lt;!-- XML Schema fragment --&gt;
&lt;xs:element name="target" type="Target"&gt;
&lt;xs:complexType name="Target"&gt;
&lt;xs:sequence&gt;
&lt;xs:choice maxOccurs="unbounded"&gt;
&lt;xs:element ref="jar"&gt;
&lt;xs:element ref="javac"&gt;
&lt;/xs:choice&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
</code></pre>
<p>
Thus the following code fragment:
<pre>
Target target = new Target();
target.tasks.add(new JarTask());
target.tasks.add(new JavacTask());
marshal(target);
</pre>
will produce the following XML output:
<pre><code>
&lt;target&gt;
&lt;jar&gt;
....
&lt;/jar&gt;
&lt;javac&gt;
....
&lt;/javac&gt;
&lt;/target&gt;
</code></pre>
<p>
It is not an error to have a class that extends <code>Task</code>
that doesn't have <a href="../../../../javax/xml/bind/annotation/XmlRootElement.html" title="annotation in javax.xml.bind.annotation"><code>XmlRootElement</code></a>. But they can't show up in an
XML instance (because they don't have XML element names).
<p><b>Example 2: XML Schema Susbstitution group support</b>
<p> The following example shows the annotations for XML Schema
substitution groups. The annotations and the ObjectFactory are
derived from the schema.
<pre>
&#64;XmlElement
class Math {
// The value of <a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#type--"><code>type()</code></a>is
// JAXBElement.class , which indicates the XML
// element name ObjectFactory - in general a class marked
// with &#64;XmlRegistry. (See ObjectFactory below)
//
// The <a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> is "operator", a pointer to a
// factory method annotated with a
// <a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a> with the name "operator". Since
// "operator" is the head of a substitution group that
// contains elements "add" and "sub" elements, "operator"
// element can be substituted in an instance document by
// elements "add" or "sub". At runtime, JAXBElement
// instance contains the element name that has been
// substituted in the XML document.
//
&#64;XmlElementRef(type=JAXBElement.class,name="operator")
JAXBElement&lt;? extends Operator&gt; term;
}
&#64;XmlRegistry
class ObjectFactory {
&#64;XmlElementDecl(name="operator")
JAXBElement&lt;Operator&gt; createOperator(Operator o) {...}
&#64;XmlElementDecl(name="add",substitutionHeadName="operator")
JAXBElement&lt;Operator&gt; createAdd(Operator o) {...}
&#64;XmlElementDecl(name="sub",substitutionHeadName="operator")
JAXBElement&lt;Operator&gt; createSub(Operator o) {...}
}
class Operator {
...
}
</pre>
<p>
Thus, the following code fragment
<pre>
Math m = new Math();
m.term = new ObjectFactory().createAdd(new Operator());
marshal(m);
</pre>
will produce the following XML output:
<pre><code>
&lt;math&gt;
&lt;add&gt;...&lt;/add&gt;
&lt;/math&gt;
</code></pre></div>
<dl>
<dt><span class="simpleTagLabel">Since:</span></dt>
<dd>1.6, JAXB 2.0</dd>
<dt><span class="seeLabel">See Also:</span></dt>
<dd><a href="../../../../javax/xml/bind/annotation/XmlElementRefs.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementRefs</code></a></dd>
</dl>
</li>
</ul>
</div>
<div class="summary">
<ul class="blockList">
<li class="blockList">
<!-- =========== ANNOTATION TYPE OPTIONAL MEMBER SUMMARY =========== -->
<ul class="blockList">
<li class="blockList"><a name="annotation.type.optional.element.summary">
<!-- -->
</a>
<h3>Optional Element Summary</h3>
<table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Optional Element Summary table, listing optional elements, and an explanation">
<caption><span>Optional Elements</span><span class="tabEnd">&nbsp;</span></caption>
<tr>
<th class="colFirst" scope="col">Modifier and Type</th>
<th class="colLast" scope="col">Optional Element and Description</th>
</tr>
<tr class="altColor">
<td class="colFirst"><code>java.lang.String</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#name--">name</a></span></code>&nbsp;</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><code>java.lang.String</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#namespace--">namespace</a></span></code>
<div class="block">This parameter and <a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> are used to determine the
XML element for the JavaBean property.</div>
</td>
</tr>
<tr class="altColor">
<td class="colFirst"><code>boolean</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#required--">required</a></span></code>
<div class="block">Customize the element declaration to be required.</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><code>java.lang.Class</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#type--">type</a></span></code>
<div class="block">The Java type being referenced.</div>
</td>
</tr>
</table>
</li>
</ul>
</li>
</ul>
</div>
<div class="details">
<ul class="blockList">
<li class="blockList">
<!-- ============ ANNOTATION TYPE MEMBER DETAIL =========== -->
<ul class="blockList">
<li class="blockList"><a name="annotation.type.element.detail">
<!-- -->
</a>
<h3>Element Detail</h3>
<a name="type--">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>type</h4>
<pre>public abstract&nbsp;java.lang.Class&nbsp;type</pre>
<div class="block">The Java type being referenced.
<p>
If the value is DEFAULT.class, the type is inferred from the
the type of the JavaBean property.</div>
<dl>
<dt>Default:</dt>
<dd>javax.xml.bind.annotation.XmlElementRef.DEFAULT.class</dd>
</dl>
</li>
</ul>
</li>
</ul>
<ul class="blockList">
<li class="blockList"><a name="namespace--">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>namespace</h4>
<pre>public abstract&nbsp;java.lang.String&nbsp;namespace</pre>
<div class="block">This parameter and <a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> are used to determine the
XML element for the JavaBean property.
<p> If <code>type()</code> is <code>JAXBElement.class</code> , then
<code>namespace()</code> and <code>name()</code>
point to a factory method with <a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a>. The XML
element name is the element name from the factory method's
<a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a> annotation or if an element from its
substitution group (of which it is a head element) has been
substituted in the XML document, then the element name is from the
<a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a> on the substituted element.
<p> If <a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#type--"><code>type()</code></a> is not <code>JAXBElement.class</code>, then
the XML element name is the XML element name statically
associated with the type using the annotation <a href="../../../../javax/xml/bind/annotation/XmlRootElement.html" title="annotation in javax.xml.bind.annotation"><code>XmlRootElement</code></a> on the type. If the type is not annotated with
an <a href="../../../../javax/xml/bind/annotation/XmlElementDecl.html" title="annotation in javax.xml.bind.annotation"><code>XmlElementDecl</code></a>, then it is an error.
<p> If <code>type()</code> is not <code>JAXBElement.class</code>, then
this value must be "".</div>
<dl>
<dt>Default:</dt>
<dd>""</dd>
</dl>
</li>
</ul>
</li>
</ul>
<ul class="blockList">
<li class="blockList"><a name="name--">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>name</h4>
<pre>public abstract&nbsp;java.lang.String&nbsp;name</pre>
<dl>
<dt><span class="seeLabel">See Also:</span></dt>
<dd><a href="../../../../javax/xml/bind/annotation/XmlElementRef.html#namespace--"><code>namespace()</code></a></dd>
</dl>
<dl>
<dt>Default:</dt>
<dd>"##default"</dd>
</dl>
</li>
</ul>
</li>
</ul>
<ul class="blockList">
<li class="blockList"><a name="required--">
<!-- -->
</a>
<ul class="blockListLast">
<li class="blockList">
<h4>required</h4>
<pre>public abstract&nbsp;boolean&nbsp;required</pre>
<div class="block">Customize the element declaration to be required.
<p>
If required() is true, then Javabean property is mapped to
an XML schema element declaration with minOccurs="1".
maxOccurs is "1" for a single valued property and "unbounded"
for a multivalued property.
<p>
If required() is false, then the Javabean property is mapped
to XML Schema element declaration with minOccurs="0".
maxOccurs is "1" for a single valued property and "unbounded"
for a multivalued property.
<p>
For compatibility with JAXB 2.1, this property defaults to <code>true</code>,
despite the fact that <a href="../../../../javax/xml/bind/annotation/XmlElement.html#required--"><code>XmlElement.required()</code></a> defaults to false.</div>
<dl>
<dt><span class="simpleTagLabel">Since:</span></dt>
<dd>1.7, JAXB 2.2</dd>
</dl>
<dl>
<dt>Default:</dt>
<dd>true</dd>
</dl>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- ========= END OF CLASS DATA ========= -->
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../../../javax/xml/bind/annotation/XmlElementDecl.GLOBAL.html" title="class in javax.xml.bind.annotation"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../../../javax/xml/bind/annotation/XmlElementRef.DEFAULT.html" title="class in javax.xml.bind.annotation"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?javax/xml/bind/annotation/XmlElementRef.html" target="_top">Frames</a></li>
<li><a href="XmlElementRef.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_bottom");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Required&nbsp;|&nbsp;</li>
<li><a href="#annotation.type.optional.element.summary">Optional</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li><a href="#annotation.type.element.detail">Element</a></li>
</ul>
</div>
<a name="skip.navbar.bottom">
<!-- -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
</body>
</html>