4

GWT-RPC requires that transfer objects to be serialized must have a default (zero-argument) constructor. Similarly, final fields will not be serialized (see issue 1054).

On the other hand, I know I am supposed to "minimize mutability". My tendency is to want my TOs to be immutable, with final fields, no default constructor, and no mutators.

How can I use GWT-RPC while respecting the immutable paradigm as much as possible. Do I have to convert to a mutable object to marshall, and then back to an immutable one? Is this even worthwhile?

Ray
  • 4,829
  • 4
  • 28
  • 55

1 Answers1

4

Item 13 in Effective Java (item 15 in second edition) gives strategies on how to minimize mutability or to favor immutability.

Suppose we remove mutators but retain non-final fields and a default constructor. The effect will be a theoretically mutable object, but a practically immutable one. Yes, one could mutate the object via reflection with a bit of effort, but by simply closing off the exposed methods we can at least discourage mutating it in cases like this where it's impractical to make the object truly immutable.

Ray
  • 4,829
  • 4
  • 28
  • 55
  • 1
    you can change final fields using reflection, so the real lose is in the self documentation of the code... – gerferra Sep 08 '11 at 02:53