Note that there are some explanatory texts on larger screens.

plurals
  1. POOverwriting attributes in subclasses with jaxb
    primarykey
    data
    text
    <p>I have the following XML code: </p> <pre><code>&lt;stereotypes&gt; &lt;stereotype1/&gt; &lt;stereotype2/&gt; &lt;/stereotypes&gt; </code></pre> <p>The problem is that I need a general attribute for each stereotype that has a different value for each stereotype type.<br> Actualy I'm not sure this is even posible or if I can implement such a thing. I tried this using the following schema fragments (setting the path attribute). What I would like is to give this attribute a fixed value for each stereotype type. The goal would be to have the getPath generated on the AbstractStereotype class and to use it in a generic way. The problem is that I can't seem to find a way of defining the attribute value in the specific Stereotypes.</p> <pre><code>&lt;xs:element name="stereotypes" minOccurs="0" maxOccurs="1"&gt; &lt;xs:complexType&gt; &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt; &lt;xs:element name="stereotype1" type="Stereotype1" /&gt; &lt;xs:element name="stereotype2" type="Stereotype2"/&gt; &lt;/xs:choice&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:complexType name="AbstractStereotype" abstract="true"&gt; &lt;xs:attribute name="path" type="amf-base:FQN" use="required"&gt;&lt;/xs:attribute&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Stereotype1"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="AbstractStereotype"&gt; &lt;!-- &lt;xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class1"/&gt; --&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Stereotype2"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="AbstractStereotype"&gt; &lt;!-- &lt;xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class2"/&gt; --&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; </code></pre> <p>Any other sugestion that would let me to "have a getPath method generated on the AbstractStereotype class and to use it in a generic way" would be much appreciated.</p> <p><strong>EDIT:</strong> Maybe to be more clear of the outcome I need.</p> <pre><code>public abstract class AbstractStereotype { public String getPath(); } public class Stereotype1 extends AbstractStereotype { public String getPath() { return "Path1"; } } public class Stereotype2 extends AbstractStereotype { public String getPath() { return "Path2"; } } </code></pre> <p>I need this because I want to treat these Stereotypes the same way:</p> <pre><code>public void someMethod() { for(AbstractStereotype stereotype: getStereotypes()) { System.out.println(stereotype.getPath()); } } </code></pre> <p>As I said before not even sure this is possible using this approach. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload