users@jaxb.java.net

RE: Polymorphism problems

From: Andrew Ferguson <Andrew.Ferguson_at_arm.com>
Date: Tue, 29 Jun 2004 10:52:00 +0100

hi Matt,

 I think your expected output may be wrong, and the actual output be
correct eg consider this code:

import java.util.*;

class Person {
        String name;
        public String toString() { return "Person("+name+")"; }
}

class Employee extends Person {
        String title;
        public String toString() { return "Employee("+name+",
"+title+")"; }
}

public class JBTest {
        public static void main(String[]arg) {
                List l = new ArrayList();
                
                Person p = new Person();
                p.name="Joe Sample";
                
                Employee q = new Employee();
                q.name="Jane Sample Employee";
                q.title="Engineer";
                
                l.add(p);
                l.add(q);
        
                System.out.println(l);
        }
}

which outputs

        [Person(Joe Sample), Employee(Jane Sample Employee, Engineer)]

So although the List is untyped (am not keeping up with generics..
sorry!) in the above code, even if it were a Person list it would still
be legitimate to add an Employee to the list (since an Employee is-a
Person)

saying that, am not sure how you could achieve what you are trying? are
you hoping for the object tree you construct not to be validated?

thanks,
Andrew

-----Original Message-----
From: Matt Munz [mailto:mmunz_at_apelon.com]
Sent: 27 June 2004 19:39
To: users_at_jaxb.dev.java.net
Subject: Polymorphism problems

Hi all,

  I'm new to this list and JAXB, so please bear with me. I searched the
archives, but didn't see a clear answer to this question. The following
was also posted to the Java Technology & XML web forum.

 I am finding little use in JAXB's polymorphism. As such, I think I'm
going to need to switch to another framework that has a more useful
polymorphism feature (or else none at all).

As indicated below, I have a schema that includes a "Person" complex
type, and an "Employee" complex type that extends it. There is also an
element "people" that is a sequence of "Person" elements. I have used
JAXB-generated classes to add instances of Person and Employee
(generated classes) to an instance of the People class. The marshaller
then erroneously outputs the extensions to Person found in Employee (see
below).

Having seen this, I wonder what use polymorphism (in the classes
generated by JAXB) has, if it is not acceptable to use a subclass
wherever its superclass is allowed. Perhaps there is some other way to
do polymorphism that I'm missing?

I really want to have a functionality such as the code example below
implies. Does JAXB have the feature I'm looking for, or do I need to
switch to another framework? If I need to switch, which one should I
use?

Thanks for any help that can be offered.

schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="people">
<xs:annotation>
<xs:documentation>Comment describing your root
element</xs:documentation> </xs:annotation> <xs:complexType>
<xs:sequence> <xs:element name="person" type="Person"
maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element>
<xs:complexType name="Person"> <xs:sequence> <xs:element name="name"
type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType
name="Employee"> <xs:complexContent> <xs:extension base="Person">
<xs:sequence> <xs:element name="title" type="xs:string"/> </xs:sequence>
</xs:extension> </xs:complexContent> </xs:complexType> </xs:schema>

expected JAXB output:

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
</person>
</people>

actual JAXB output (invalid):

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
<title>Engineer</title>
</person>
</people>

Java code:

Person person = factory.createPerson();
person.setName("Joe Sample");
Employee employee = factory.createEmployee(); employee.setName("Jane
Sample Employee"); employee.setTitle("Engineer"); People people =
factory.createPeople(); people.getPerson().add(person);
people.getPerson().add(employee);

- Matt Munz



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net