primitive types (int, double etc.) instead of class types

View: New views
7 Messages — Rating Filter:   Alert me  

primitive types (int, double etc.) instead of class types

by Tomasz Toczyski-2 :: Rate this Message:

| View Threaded | Show Only this Message

Hi!

I have elements of xs:int and xs:double types defined in my xsd file.
Previously I used Jaxb 1.x - it generated Java classes with fileds of
primitive types: int and double.

But Jaxb2 generates classes with fields of class types: Integer, Double.
But I don't want class types (These class types cause many unboxing
problems in my project). I need primitive types.

I know that I can force Jaxb to generate primitive types by setting
minOccurs="1" in elements definitions. But it is not a suitable solution
for me (I should not change my schema).

Do you know any solution for me?


Regards,
-tt.





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: primitive types (int, double etc.) instead of class types

by Felipe Gaucho :: Rate this Message:

| View Threaded | Show Only this Message

you can use JAXB customizations to generate any type from any element....

like this example:
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbbf.html#bnbci

On Wed, Dec 3, 2008 at 5:46 PM, Tomasz Toczyski <guf@...> wrote:

> Hi!
>
> I have elements of xs:int and xs:double types defined in my xsd file.
> Previously I used Jaxb 1.x - it generated Java classes with fileds of
> primitive types: int and double.
>
> But Jaxb2 generates classes with fields of class types: Integer, Double.
> But I don't want class types (These class types cause many unboxing
> problems in my project). I need primitive types.
>
> I know that I can force Jaxb to generate primitive types by setting
> minOccurs="1" in elements definitions. But it is not a suitable solution
> for me (I should not change my schema).
>
> Do you know any solution for me?
>
>
> Regards,
> -tt.
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: primitive types (int, double etc.) instead of class types

by Tomasz Toczyski-2 :: Rate this Message:

| View Threaded | Show Only this Message

> you can use JAXB customizations to generate any type from any element....
>
> like this example:
> http://java.sun.com/javaee/5/docs/tutorial/doc/bnbbf.html#bnbci

It does not solve my problem. If I declare minOccurs="0" Jaxb2 maps such
element to Integer type. But I want primitive int type.

Jaxb1 generated primitive int type even if I declare minOccurs="0". If
there were no such an element in my xml document a getter related to this
element returned 0 value. Can I force Jaxb2 to operate like Jaxb1 ?

-tt.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: primitive types (int, double etc.) instead of class types

by Colin Fleming :: Rate this Message:

| View Threaded | Show Only this Message

Hi Tomasz,

The problem is that if JAXB generates a primitive int in that case (with minOccurs=0) then you have no way to tell if the document actually contained that element/attribute (i.e. if your field contains 0, you have no way of telling if the field was not supplied, or if it was supplied and had the value 0). I'm not aware of any way to make JAXB2 work the way you want, although there may be one. You can might be able to use a global customisation which would not require touching your schemas.

Cheers,
Colin


2008/12/4 Tomasz Toczyski <guf@...>
> you can use JAXB customizations to generate any type from any element....
>
> like this example:
> http://java.sun.com/javaee/5/docs/tutorial/doc/bnbbf.html#bnbci

It does not solve my problem. If I declare minOccurs="0" Jaxb2 maps such
element to Integer type. But I want primitive int type.

Jaxb1 generated primitive int type even if I declare minOccurs="0". If
there were no such an element in my xml document a getter related to this
element returned 0 value. Can I force Jaxb2 to operate like Jaxb1 ?

-tt.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...



Re: primitive types (int, double etc.) instead of class types

by Aleksei Valikov :: Rate this Message:

| View Threaded | Show Only this Message

Hi.

> The problem is that if JAXB generates a primitive int in that case (with
> minOccurs=0) then you have no way to tell if the document actually contained
> that element/attribute (i.e. if your field contains 0, you have no way of
> telling if the field was not supplied, or if it was supplied and had the
> value 0).

You can generate "isSet" method for such cases.

> I'm not aware of any way to make JAXB2 work the way you want,
> although there may be one. You can might be able to use a global
> customisation which would not require touching your schemas.

Please see the globalBindings/@optionalProperty customization,
sections 5.5.1 and 5.5.4 in the spec.

Bye.
/lexi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: primitive types (int, double etc.) instead of class types

by Tomasz Toczyski-2 :: Rate this Message:

| View Threaded | Show Only this Message

Aleksei Valikov:
> Please see the globalBindings/@optionalProperty customization,
> sections 5.5.1 and 5.5.4 in the spec.

It seems reasonable. Alas, it doesn't work for me. I have tried
<jaxb:globalBindings optionalProperty="primitive">
and despite of it Jaxb generates getters of Integer values ( instead of
int values).

I also tried
<jaxb:globalBindings optionalProperty="isSet"> - The effect is the same
(Integers not ints)/

Maybe there is a bug in Jaxb?... I use Jaxb v.2.1.7

-tt.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: primitive types (int, double etc.) instead of class types

by jrj :: Rate this Message:

| View Threaded | Show Only this Message


Tomasz Toczyski-2 wrote:
Aleksei Valikov:
> Please see the globalBindings/@optionalProperty customization,
> sections 5.5.1 and 5.5.4 in the spec.

It seems reasonable. Alas, it doesn't work for me. I have tried
<jaxb:globalBindings optionalProperty="primitive">
and despite of it Jaxb generates getters of Integer values ( instead of
int values).

I also tried
<jaxb:globalBindings optionalProperty="isSet"> - The effect is the same
(Integers not ints)/

Maybe there is a bug in Jaxb?... I use Jaxb v.2.1.7

-tt.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@jaxb.dev.java.net
For additional commands, e-mail: users-help@jaxb.dev.java.net

Hi Tomasz,

Very late response, but this might be of use to anyone else coming this way. The JAXB spec defines the attribute generateIsSetMethod thus:
<jaxb:globalBindings generateIsSetMethod="true">
is the approach you'd have needed here. I discussed this in a blog post where I was experiencing the opposite problem http://reast.net/coding/java/jaxb-generating-primitive-type-getters/.

Jason