Comments
link to Github.
package rule74; import java.awt.Component; /** * * * @author gwon * @history * 2019. 5. 25. initial creation */ public class Rule74 { // Implements Serializable to pass server exceptions that occur during RMI. Throwable throwable = new Throwable(); // Implements Serializable to send, keep, and restore GUI. Component component = new Component() {}; }
package rule74; import java.io.Serializable; /** * * * @author gwon * @history * 2019. 5. 25. initial creation */ public class SerialVersionUidClass implements Serializable { private static final long serialVersionUID = 177705176L; }
package rule74.test2; /** * * * @author gwon * @history * 2019. 5. 25. initial creation */ public class NotSerializableParent { /*************************************************** * During an object's de-serialization, it starts with parent class. Since the parent class is * non-serializable, it can retrieve the property information of the parent classes through the * default constructor. If there is no default constructor, the object becomes invalid. * * This NotSerializableParent class non-serializable and has no default constructor, so an error * occurs when de-serializing a Child class that inherits from this class. * * public NotSerializableParent(){ * * } * * ****************************************************/ public NotSerializableParent(String str) { System.out.println("Constructor with param"); } }
package rule74.test2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * * * @author gwon * @history * 2019. 5. 25. initial creation */ public class Child extends NotSerializableParent implements Serializable { public Child(String str) { super(str); } private static final long serialVersionUID = 1L; public static void main(String[] args) throws IOException, ClassNotFoundException { Child child = new Child("test"); // Serialization byte[] serializedMember; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(child); serializedMember = baos.toByteArray(); } } // De-Serialization try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedMember)) { try (ObjectInputStream ois = new ObjectInputStream(bais)) { Object objectMember = ois.readObject(); // InvalidClassException occurs with "no valid constructor" message Child deChild = (Child) objectMember; } } } }