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.
On Fri, Feb 6, 2009 at 4:25 PM, jsegal <[email protected]> wrote: > > I've been having some trouble with using arrays of primitive type > where an array of type Serializable is expected. > > I have an object similar to: > > class MySerializableObject<T extends Serializable> implements > Serializable > { > private Serializable serializableField; > > void MySerializableObject(T value) > { > serializableField= value; > } > > T getField(T value) > { > return serializableField; > } > } > > All of these cases can be serialized and deserialized successfully: > - new MySerializableObject<Serializable>(new Integer(0)); > - new MySerializableObject<Serializable>(1); > - new MySerializableObject<Serializable>(true); > - new MySerializableObject<Serializable>("Test"); > > These cases case produces an exception when deserialized (Note that > these will *not* cause a class cast exception as seen in > http://code.google.com/p/google-web-toolkit/issues/detail?id=1822, > although an array of non-primitive type will cause it): > - new MySerializableObject<(Serializable)>((Serializable)new int[0]); > - new MySerializableObject<int[]>(new int[0]); > > The exception's message is: > "com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: > Type '[I' was not included in the set of types which can be > deserialized by this SerializationPolicy or its Class object could not > be loaded. For security purposes, this type will not be deserialized." > > The exception goes way if I add this type to my project, even if I > don't actually create an instance of it anywhere: > > class MySerializableIntArrayObject extends MySerializableObject<Int[]> > { > } > > I'm aware that the GWT compiler has to identify sub-types that can be > used with parameterized serializable objects during compilation in > order to build its serialization mechanism for those objects. I'm > guessing this problem means that arrays of primitive types are not > taken into account when it enumerates subtypes of Serializable, > despite the fact that they are effectively subtypes of Serializable. > > If I'm correct, what is the proper way to fix this? I suppose it's > possible to create fixed-type objects like > "MySerializableIntArrayObject" for all primitives, but I'd like to > avoid that (especially since I would end up having to do that for any > other typed serializable objects I add in the future). If I understand > the typeArgs annotation, using it would actually restrict the set of > values I can use with this object, not add to the set (I can enumerate > the primitive array types easily enough, but not all the other types > that may be passed). > > If I'm not correct about the cause of the problem, I'd appreciate it > if someone could explain it and suggest an appropriate remedy. > > Thanks, > -jsegal > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
