users@jaxb.java.net

Re: force to use primitive types

From: aka.fe2s <aka.fe2s_at_gmail.com>
Date: Fri, 27 May 2011 06:43:01 -0400

Thanks, but optionalProperty="primitive" doesn't work for me. I still
get wrappers.

I also tried to reproduce it with the sample project from JAXB 2.1.12
distributive.

So I

1) added <xs:element name="test" type="xs:int" minOccurs="0"/> to
Items type in samples/external-customize/po.xsd

2) added optionalProperty="primitive" to binding.xjb

3) ran xjc -b binding.xjb po.xsd

and it generated wrapper "protected Integer test;" in Item.java

Is it a bug or am I doing something wrong ?




On Fri, May 27, 2011 at 6:01 AM, Wolfgang Laun <wolfgang.laun_at_gmail.com> wrote:
> From the JAXB sepcification:
>
> <globalBindings
>    ...
>     [ optionalProperty = "wrapper" | "primitive" | "isSet" ]
>    ...
> </globalBindings>
>
> optionalProperty controls how a JAXB property with a primitive
> base type that represents an optional non-nillable element/
> attribute is bound. If the attribute has the value "wrapper", then
> the base type of the JAXB property is the wrapper class for the
> primitive type. A user can indicate that this optional property
> is not set by calling the setter with “null” value. If the
> attribute’s value is "primitive", it binds as it did in JAXB 1.0.
> If the attribute’s value is “isSet”, it binds the optional
> property using the primitive base type and also the isSet/unset
> methods are generated for the optional property. The attribute’s
> default value is “wrapper”.
>
> -W
>
> On 27 May 2011 11:19, aka.fe2s <aka.fe2s_at_gmail.com> wrote:
>>
>> Hello,
>>
>> I'm migrating application from JAXB 1 to JAXB 2. The problem I faced
>> is that previously JAXB 1 generated primitives types (int, boolean,
>> ..) and isSetXXX() methods everywhere, even though the element was
>> optional (minOccurs="0"). If the element was absent in XML, it set the
>> property to default value (0 for int, false for boolean, ...)
>>
>> The problem is that JAXB 2 generates wrappers (Integer, Boolean) in
>> such cases. It leads to NPE in my code.
>> I tried to set
>>  <jxb:globalBindings
>>        generateIsSetMethod="true" />
>>
>> and/or
>>
>> <jxb:javaType name="int" xmlType="xs:int"
>>                parseMethod="javax.xml.bind.DatatypeConverter.parseInt"
>>                printMethod="javax.xml.bind.DatatypeConverter.printInt" />
>>
>> but it doesn't help.
>>
>> Is there a way to force JAXB 2 behave like JAXB 1 ?
>>
>> Thanks, Oleksiy.
>
>