Hi! I've recently filed 2 JIRAs (HARMONY-1920 and HARMONY 1921) related to the object replacement in ObjectInputStream by means of resolveObject() method and during experimenting with the tests, created the test which replaces the object according to some rule (see the code at the bottom of the message). Unexpectedly it fails on RI with the output:
TestObjectInputStream.resolveClass() is called. 1-st read passed. 2-nd read failed with exception: java.lang.ClassCastException: b.TestClass This output indicates that RI performs caching for object replacements and second read just did not call resolveClass() method from TestObjectInputStream. I did not find any info about this case in serialization specification and not quite sure if this behaviour is correct. Is this a bug in RI? If not then this case should be also taken into account while fixing HARMONY-1921 JIRA. ------------------------- Test.java ----------------------------- import java.io.*; public class Test { public static void main(String[] args) throws Exception { a.TestClass to1 = new a.TestClass(); to1.i = 555; a.TestClass to2 = new a.TestClass(); to2.i = 777; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(to1); oos.writeObject(to2); oos.flush(); byte[] bytes = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new TestObjectInputStream(bais); try { b.TestClass to3 = (b.TestClass) ois.readObject(); if (to3.i != to1.i) { System.out.println("1-st read failed. Expected: " + to1.i + ", got: " + to3.i); } else { System.out.println("1-st read passed."); } } catch (Exception ex) { System.out.println("1-st read failed with exception: " + ex); } try { a.TestClass to4 = (a.TestClass) ois.readObject(); if (to4.i != to2.i) { System.out.println("2-nd read failed. Expected: " + to2.i + ", got: " + to4.i); } else { System.out.println("2-nd read passed."); } } catch (Exception ex) { System.out.println("2-nd read failed with exception: " + ex); } } static class TestObjectInputStream extends ObjectInputStream { private boolean replaced = false; public TestObjectInputStream(InputStream in) throws IOException { super(in); } protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { if (desc.getName().equals("a.TestClass")) { System.out.println("TestObjectInputStream.resolveClass() is called."); if (!replaced) { replaced = true; return b.TestClass.class; } else { return a.TestClass.class; } } return super.resolveClass(desc); } } } ------------------------- a/TestClass.java ----------------------------- package a; import java.io.Serializable; public class TestClass implements Serializable { private static final long serialVersionUID = 11111L; public int i = 0; } ------------------------- b/TestClass.java ----------------------------- package b; import java.io.Serializable; public class TestClass implements Serializable { private static final long serialVersionUID = 11111L; public int i = 0; } ----------------------------------------------------------------- Mikhail Markov Intell Middleware Products Division