users@jaxb.java.net

Re: More JAXBElement

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 16 Feb 2005 12:20:29 -0800

Brian Pontarelli wrote:
> I see how the substitution groups throw a wrench in the works, but I
> think my main concern is consistency and the early access release
> doesn't seem to have it. If it always returns JAXBElement that would be
> fine. But it mostly returns the objects themselves and sometimes (I
> haven't figured out exactly when) JAXBElement.

The actual logic is that if the class has @XmlRootElement annotation, it
returns the object itself, where as if it doesn't, it's returned as
wrapped in JAXBElement.

> I feel that it should
> always return the objects themselves and when the client needs the name,
> a second Unmarshaller method can be provided. That way it is always
> consistent and the developer doesn't have to guess when it will return
> JAXBElement or the object. Here's what I was thinking for the new method:

Thanks for the suggestion. I'm curious what others think.


> JAXBContext c = JAXBContext.newInstance(ObjectFactory.class);
> Unmarshaller um = c.createUnmarshaller();
> MyRequest mr = um.umarhsal(inputStream); // object itself
> JAXBElement<MyRequest> mr = um.unmarhsalElement(inputStream); // JAXBElement
>
> (of course there would be casts in the code).
>
> This would allow users to get at the JAXBElement when it is needed, but
> also be able to just deal with the Objects directly. As for substitution
> groups, those that need the name of the element can use the second
> method and receive the JAXBElement.

The suggest unmarshal method is really just:

        Object unmarshal( INPUT ) {
            Object o = currentUnmarshalMethod(INPUT);
            if(o instanceof JAXBElement)
                return ((JAXBElement)o).getValue();
            else
                return o;
        }

right?


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com