Sunday, September 26, 2010

Re: GWT Custom Serialization

On 25 sep, 18:31, Lucas Charron <raijinse...@gmail.com> wrote:
> I am having a slight problem with a framework I am making. Here is an
> example class:
> public class Example implements IsSerializable
> {
> private final int a;
>
> private final int b;
>
> public Example(int a,int b)
> {
> this.a=a;
> this.b=b;
>
> }
>
> // getters ...
>
> }
>
> A very simple class. However, there is no way to serialize it using
> GWT. Although the simple work-around is to remove the "final"
> attribute from the fields, that does not "jive" with, what I consider,
> solid programming. The "final" attribute means that no where, in any
> instance of the class or any instance of a descendant of this class,
> will the values of "a" and "b" be modified in any way (exception:
> using Java reflection). More than the optimization that many JVMs will
> do, there is also a type of safety - the programmer cannot
> accidentally put the reference on the left hand side of an assignment
> operator.
>
> The GWT compiler will emit a warning that the final fields will not be
> serialized. Using the "_CustomFieldSerializer" is also impossible
> because the deserialize method takes an instance of the class you are
> deserializing as a parameter - instead it should have the return value
> as an instance of the class.

You can use an instantiate method in _CustomFieldSerializer. It works
the same as deserialize but returns a newly created instance (and then
deserialize is called with that instance).

public static Example instantiate(SerializationStreamReader reader)
throws SerializationException {
return new Example(reader.readInt(), reader.readInt());
}

Even if you don't need it, the deserialize method is required,
contrary to the instantiate method.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment