Automatic addition of Serializable trait


#1

Is there a way either to tell the scala compiler to make all generated objects serializable - or at least all objects which are referenced as members of a serializable object, or at a minimum to check that if an object extends Serializable that all member objects (right down the tree) are also serializable?

Currently you do not find out until run time which is less than ideal…

Is this something that a macro could enforce? If so has anyone written such a macro?

This will become more important as with Java 9 Field.setAccessible is being deprecated and by all accounts it is due to be removed in Java 10. This will make rolling your own serialization much more difficult, and many of the existing external serialization methods rely on using Field.setAccessible. One of the reasons why people use non-native serialization is to avoid having to decorate all their objects.


#2

Don’t know about automatic addition, but you should be able to check it at compile time, I’d think.

In the Java world, FindBugs has some checks around serialization, see http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD and below.

As FindBugs operates on the bytecode, not the source code, you should be able to run it on your Scala classes as well (disclaimer: haven’t tried it, don’t know how/if it integrates with sbt, etc.).

Or you could look at the code for the Findbugs check and use it as instruction to create your own check for http://www.scalastyle.org or http://www.wartremover.org