serialization - Experiment with incompatibility of Java serilization -


i understand java serialization conceptually, pretty bewildered version compatibility of serializable object. when "version" referred, mean:

either 2 classes of hierarchical relationship, , 1 of has addition/deduction of attributes?

or 2 classes complied different compilation versions?

or both?

my understanding have keep suid consistent, checked jvm version compatibility.

as result devised experiment trying test version incompatibility of java serialization, code snippet below, in made suid different:

    public static class incompatible implements serializable {     private static final long serialversionuid = 1l;     private int i;     public incompatible(int i){         this.i = i;     }     public int geti(){         return i;     } }  public static class incompfoo extends incompatible {     private static final long serialversionuid = 2l;     public incompfoo(int i){         super(i);     } }  @test public void incompatibletest() throws ioexception, classnotfoundexception{     fileoutputstream fos = new fileoutputstream(new file("foo.ser"));     objectoutputstream oos = new objectoutputstream(fos);     oos.writeobject(new incompfoo(10));     oos.close();      fileinputstream fis = new fileinputstream(new file("foo.ser"));     objectinputstream ois = new objectinputstream(fis);     incompatible foo = (incompatible)ois.readobject();     ois.close();     assert.assertequals(10, foo.geti());  } 

i expected invalidclassexception these 2 classes have different suid. surprise test went through right.

which part wrong? have simple way create invalidclassexception?

your example writes object, reads , casts superclass. nothing interesting happens there, though hoped cast somehow break things (it doesn't, it's cast , valid one).

write instance of class file, change it's serialversionuid, recompile class , try read serialized object back. forget super/subclass shenanigans.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -