users@jaxb.java.net

Re: xjc:javaType doesn't handle generics

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Sun, 14 Feb 2010 11:22:33 +0100

On Sat, Feb 13, 2010 at 10:06 PM, Joshua Royalty
<royalpeasantry_at_gmail.com>wrote:

> Just demonstrating, my case is more complicated than this so I simplified
> it.
>
> So you'll get an answer for the simplified case:

If you use
   <xjc:javaType adapter="foo.ListAdapter" name="foo.ListOfString" />

with ListOfString.java like this:

package foo;
import java.util.*;
public class ListOfString extends ArrayList<String> {
    public ListOfString( Collection<String> c ){
        super( c );
    }
    public ListOfString(){
        super();
    }
}

you compile with full type checking. Notice that you can use e.g.

   ListOfString los = new ListOfString();
   doc.setItems( los );
   los.add( "one" );
   los.add( "two" );
   for( String s: doc.getItems() ){
        System.out.println( s );
   }

so all the wrapper has to do is map the constructors, as you need them.


Given that I am using the maven-jaxb2-plugin.. does anyone know how to
> figure out what version of jaxb I am using?
>
>
Look at the line 2 of the generated Java source:

// This file was generated by the JavaTM Architecture for XML Binding(JAXB)
Reference Implementation, XXX

XXX is not always JAXB 2.y.x, but one can figure out the xjc version from
s.th. like vhudson-jaxb-ri-... as well.

-W



> Thank You,
> -Josh
>
>
>
> On Sat, Feb 13, 2010 at 2:06 AM, Wolfgang Laun <wolfgang.laun_at_gmail.com>wrote:
>
>> Are you trying to create a binding for a list of String values to a simple
>> xs:element value, or are you just demonstrating xjc's (non)handling of
>> generics?
>>
>> For the former, see
>> https://jaxb.dev.java.net/tutorial/section_2_2_8-Defining-Other-Lists.html#Defining%20Other%20Listswhere you'll see that using xs:list solves the problem nicely.
>>
>> -W
>>
>>
>>
>> On Sat, Feb 13, 2010 at 9:39 AM, Joshua Royalty <joshua.royalty_at_gmail.com
>> > wrote:
>>
>>> ####### Schema #########
>>>
>>> <?xml version="1.0" encoding="utf-8"?>
>>> <xs:schema
>>> elementFormDefault="qualified"
>>> targetNamespace="http://test"
>>> xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>> xmlns="http://test"
>>> >
>>> <xs:element name="root" type="rootType" />
>>>
>>> <xs:complexType name="rootType">
>>> <xs:sequence>
>>> <xs:element name="items" type="xs:string"/>
>>> </xs:sequence>
>>> </xs:complexType>
>>>
>>> </xs:schema>
>>>
>>> ######## Bindings File #############
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <bindings version="2.1"
>>> xmlns="http://java.sun.com/xml/ns/jaxb"
>>> xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
>>> xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>> >
>>> <bindings schemaLocation="test.xsd" node="/xs:schema">
>>> <bindings node="//xs:complexType[@name='rootType']">
>>> <bindings node="//xs:element[@name='items']">
>>> <property>
>>> <baseType>
>>> <xjc:javaType adapter="ListAdapter"
>>> name="java.util.List&lt;String&gt;" />
>>> </baseType>
>>> </property>
>>> </bindings>
>>> </bindings>
>>> </bindings>
>>> </bindings>
>>>
>>> ###### Output #####
>>>
>>> import java.util.List<String>; <--- Problem
>>> import javax.xml.bind.annotation.XmlAccessType;
>>> import javax.xml.bind.annotation.XmlAccessorType;
>>> import javax.xml.bind.annotation.XmlElement;
>>> *
>>> *
>>> *
>>> public class RootType {
>>>
>>> @XmlElement(required = true, type = String.class)
>>> @XmlJavaTypeAdapter(ListAdapter.class)
>>> protected List<String>items;
>>> *
>>> *
>>> *
>>>
>>>
>>>
>>> So.. The problem is pretty obvious, when XJC adds the import for the
>>> external type it doesn't remove the generic part. If I switch to
>>>
>>> <xjc:javaType adapter="ListAdapter" name="java.util.List" />
>>>
>>> it compiles, but has raw type warnings.
>>>
>>> This is even a rather trivial example since String is imported by
>>> default. What about java.util.List<java.util.Map<String,
>>> java.utli.Map<java.util.Set, java.util.List<String>>>> or similar?
>>>
>>>
>>>
>>> So... am I missing something? Its similar to
>>> https://jaxb.dev.java.net/issues/show_bug.cgi?id=234 .. but not quite,
>>> this is a bit more basic than that.
>>>
>>> I am using the maven-jaxb2-plugin
>>> <groupId>org.jvnet.jaxb2.maven2</groupId>
>>> <artifactId>maven-jaxb2-plugin</artifactId>
>>>
>>> Thank You,
>>> -Josh
>>>
>>
>>
>