Unfortunately, this won't work due to the bug I referenced earlier
(sorry, I must have cut it out of the quoted section).

This will compile, but will throw a ClassCastException due to a known
bug (http://code.google.com/p/google-web-toolkit/issues/detail?
id=1822):
new MySerializableObject<Integer[]>(new Integer[] {1})

As suggested in the issue comments, it's possible to work around this
by creating another class, which is a reasonable way to solve the
problem for arrays of most types:

class MySerializableArrayObject<T extends Serializable> extends
MySerializableObject<T[]>
{
         private Serializable serializableArrayField[];

         void MySerializableObject(T[] value)
         {
               serializableArrayField= value;
         }

         T[] getField()
         {
               return serializableArrayField;
         }

}

The class above makes this work (for Integer, or any other non-
primitive type):
new MySerializableArrayObject<Integer>(new Integer[] {1});

. . .But won't fix the problem for primitive types, since it is not
possible to type the array version of the object for use with
primitives:
new MySerializableArrayObject<int>(new int[] {1}); //This won't
compile, of course
new MySerializableArrayObject<Integer>(new int[] {1}); //Nor will
this, since int[] can't be autoboxed to Integer[]

If you're suggesting that I can replace all uses of int[] with Integer
[] in the existing code (at least the parts that need to interact with
this class), you are technically correct. This will, however, leave a
significant maintenance hazard for anyone else working on the app. I
can document the problem, but given the context in which these classes
are used, it would be tough to do so in enough places that anyone
wanting to use primitive arrays would notice it. That's why I'm
looking for a better work-around.

I'm currently planning to make variations on MySerializableArrayObject
to specifically handle each type of primitive array. I don't think
this will cover some edge-cases, such as multidimensional arrays, but
it looks like it may be the best solution available for now. If
someone can suggest a better one, I'm still very interested.

On Feb 9, 3:53 pm, Dan Ox <[email protected]> wrote:
> Have you tried:
> new MySerializableObject<Integer[]>(new Integer[] {1});
>
> ?
>
> On Feb 10, 4:13 am, jsegal <[email protected]> wrote:
>
> > You're correct that primitives themselves will not work, but they can
> > be autoboxed using their Object versions. My problem is with *arrays*
> > of primitive types, which *can* be used as parameters for for generic
> > classes, and should (according to everything I've found on the
> > subject) be serializable.
>
> > You're right that this will not compile:
> > new MySerializableObject<int>(1);
>
> > This will compile, however:
> > new MySerializableObject<int[]>(new int[]{1});
>
> > As will this (due to autoboxing):
> > new MySerializableObject<Integer>(1);
>
> > On the other hand, arrays cannot be autoboxed, so this will not
> > compile:
> > new MySerializableObject<Integer[]>(new int[]{1});
>
> > On Feb 6, 6:10 pm, Ben Tilford <[email protected]> wrote:
>
> > > Primitives do not extend Object and cannot implement Serializable. You
> > > should use the Object versions of primitives (i.e. use Integer instead of
> > > int)
> > > I don't think the code would even compile if you tried to use a primitive
> > > with generics.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to