Serialization
Peter Levart
peter.levart at marand.si
Wed Dec 16 08:43:15 PST 2009
On Wednesday 16 December 2009 16:46:14 Joshua Bloch wrote:
> Folks,
>
> Associating a class name with a closure is only half the battle. Loosely
> speaking, it takes you from serializing an anonymous class instance to a
> named *non-static* nested class. But even the latter is generally
> considered a bad idea, because you end up serializing the enclosing
> instance, which is rarely what you want. The serializable SAM interfaces
> that we use in MapReduce are typically nested *static *classes (hence no
> enclosing instance). I'd argue that this is a reasonable restriction for
> serializable function objects (the static part, not the nested part).
>
> Josh
>
>
Compiler can generate static class if closure doesn't capture any instance variables of the enclosing class and doesn't call any instance methods of the enclosing class and doesn't use "this" keyword. Capturing local variables does not prevent compiler from generating static class. I wouldn't prevent serializability of non-static closures though. If one should be worried about that, the compiler could issue a special warning that is opt-in type (accompanied with annotation for the same purpose).
What is important I think is that generated closure class uses the same names for it's fields as the names of corresponding captured local variables. This way the serialization format stays the same if the set of capcured local variables does not change (regardles of the inner rearrangements of closure's code). Renaming a captured local variable then has the same effect for serialization format of the closure as renaming an instance variable has for a normal class.
Peter
More information about the lambda-dev
mailing list