Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to Serialize Value based objects if the application never relies on its object identity?

Sonar shows

Make this value-based field transient so it is not included in the serialization of this class.

This is a future-proof bug when value-based class will be released.

So, if the application never relies on its object identity can I make value-based objects non-transient?

like image 526
sagar Avatar asked Dec 21 '17 10:12

sagar


Video Answer


1 Answers

To make a field of a value-based class non-transient, the value based class must be serializable. So it’s actually a design decision not made by you.

If the designer declares a class to be value-based and implementing Serializable, they assume that value based classes and Serialization are compatible and will stay so.

We don’t know, how the final value type implementation will look like, but the migration path offered by the JRE developers, e.g. when introducing the immutable lists, being value based and serializable, should be taken, rather than assuming that there are additional rules and constraints beyond the specification.

After all, there is no reason to assume that Serialization won’t work with value types. It supports primitive values as well and has been adapted in the past too, e.g. when enum support was added. It’s not clear whether it will always store the values then or still support back references like with ordinary objects or perform an entirely different canonicalization, but as long as you don’t rely on the object identity, as was your premise, you’re on the safe side, as either strategy would work with your code.

like image 169
Holger Avatar answered Nov 15 '22 07:11

Holger