Note that there are some explanatory texts on larger screens.

plurals
  1. POSkipping over newly-transient fields when deserializing
    primarykey
    data
    text
    <p>I inherited the following code (and data stored using it, i.e. serialized instances of A):</p> <pre><code>class A implements Serializable { private static final long serialVersionUID = 1L; int someField; B b; } class B implements Serializable { private static final long serialVersionUID = 1L; int someField; } </code></pre> <p>At some point I realized that the <code>b</code> field in <code>A</code> should not actually be persisted (and <code>B</code> shouldn't be serializable at all), so I changed things to:</p> <pre><code>class A implements Serializable { private static final long serialVersionUID = 1L; int someField; transient B b; } class B { int someField; } </code></pre> <p>If I make a new instance of <code>A</code> and serialize it, I have no trouble deserializing it. However, if I try to deserialize instances of <code>A</code> that were stored with the old code, I get an exception of the form:</p> <pre><code>java.io.InvalidClassException: B; B; class invalid for deserialization at java.io.ObjectStreamClass.checkDeserialize(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.defaultReadFields(Unknown Source) at java.io.ObjectInputStream.readSerialData(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) </code></pre> <p>I believe this is because the persisted data also has <code>A</code>'s and <code>B</code>'s class descriptions stored, and those still think that <code>b</code> is persisted, even if in the current version they no longer are (see lines 600 to 606 in <a href="http://hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/80266a3303b2/src/share/classes/java/io/ObjectStreamClass.java" rel="nofollow noreferrer">ObjectStreamClass</a>)</p> <p>Is there a way to force A's deserialization to skip over fields that are now transient? For example, is there a way to override the class description that the deserialization code reads in <code>ObjectInputStream</code> and update its definition of <code>b</code> so that it knows its transient?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload