2
0

Serializable.hx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package java.io;
  2. /*
  3. * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
  4. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5. *
  6. * This code is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 only, as
  8. * published by the Free Software Foundation. Oracle designates this
  9. * particular file as subject to the "Classpath" exception as provided
  10. * by Oracle in the LICENSE file that accompanied this code.
  11. *
  12. * This code is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * version 2 for more details (a copy is included in the LICENSE file that
  16. * accompanied this code).
  17. *
  18. * You should have received a copy of the GNU General Public License version
  19. * 2 along with this work; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23. * or visit www.oracle.com if you need additional information or have any
  24. * questions.
  25. */
  26. /**
  27. * Serializability of a class is enabled by the class implementing the
  28. * java.io.Serializable interface. Classes that do not implement this
  29. * interface will not have any of their state serialized or
  30. * deserialized. All subtypes of a serializable class are themselves
  31. * serializable. The serialization interface has no methods or fields
  32. * and serves only to identify the semantics of being serializable. <p>
  33. *
  34. * To allow subtypes of non-serializable classes to be serialized, the
  35. * subtype may assume responsibility for saving and restoring the
  36. * state of the supertype's public, protected, and (if accessible)
  37. * package fields. The subtype may assume this responsibility only if
  38. * the class it extends has an accessible no-arg constructor to
  39. * initialize the class's state. It is an error to declare a class
  40. * Serializable if this is not the case. The error will be detected at
  41. * runtime. <p>
  42. *
  43. * During deserialization, the fields of non-serializable classes will
  44. * be initialized using the public or protected no-arg constructor of
  45. * the class. A no-arg constructor must be accessible to the subclass
  46. * that is serializable. The fields of serializable subclasses will
  47. * be restored from the stream. <p>
  48. *
  49. * When traversing a graph, an object may be encountered that does not
  50. * support the Serializable interface. In this case the
  51. * NotSerializableException will be thrown and will identify the class
  52. * of the non-serializable object. <p>
  53. *
  54. * Classes that require special handling during the serialization and
  55. * deserialization process must implement special methods with these exact
  56. * signatures: <p>
  57. *
  58. * <PRE>
  59. * private void writeObject(java.io.ObjectOutputStream out)
  60. * throws IOException
  61. * private void readObject(java.io.ObjectInputStream in)
  62. * throws IOException, ClassNotFoundException;
  63. * private void readObjectNoData()
  64. * throws ObjectStreamException;
  65. * </PRE>
  66. *
  67. * <p>The writeObject method is responsible for writing the state of the
  68. * object for its particular class so that the corresponding
  69. * readObject method can restore it. The default mechanism for saving
  70. * the Object's fields can be invoked by calling
  71. * out.defaultWriteObject. The method does not need to concern
  72. * itself with the state belonging to its superclasses or subclasses.
  73. * State is saved by writing the individual fields to the
  74. * ObjectOutputStream using the writeObject method or by using the
  75. * methods for primitive data types supported by DataOutput.
  76. *
  77. * <p>The readObject method is responsible for reading from the stream and
  78. * restoring the classes fields. It may call in.defaultReadObject to invoke
  79. * the default mechanism for restoring the object's non-static and
  80. * non-transient fields. The defaultReadObject method uses information in
  81. * the stream to assign the fields of the object saved in the stream with the
  82. * correspondingly named fields in the current object. This handles the case
  83. * when the class has evolved to add new fields. The method does not need to
  84. * concern itself with the state belonging to its superclasses or subclasses.
  85. * State is saved by writing the individual fields to the
  86. * ObjectOutputStream using the writeObject method or by using the
  87. * methods for primitive data types supported by DataOutput.
  88. *
  89. * <p>The readObjectNoData method is responsible for initializing the state of
  90. * the object for its particular class in the event that the serialization
  91. * stream does not list the given class as a superclass of the object being
  92. * deserialized. This may occur in cases where the receiving party uses a
  93. * different version of the deserialized instance's class than the sending
  94. * party, and the receiver's version extends classes that are not extended by
  95. * the sender's version. This may also occur if the serialization stream has
  96. * been tampered; hence, readObjectNoData is useful for initializing
  97. * deserialized objects properly despite a "hostile" or incomplete source
  98. * stream.
  99. *
  100. * <p>Serializable classes that need to designate an alternative object to be
  101. * used when writing an object to the stream should implement this
  102. * special method with the exact signature: <p>
  103. *
  104. * <PRE>
  105. * ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
  106. * </PRE><p>
  107. *
  108. * This writeReplace method is invoked by serialization if the method
  109. * exists and it would be accessible from a method defined within the
  110. * class of the object being serialized. Thus, the method can have private,
  111. * protected and package-private access. Subclass access to this method
  112. * follows java accessibility rules. <p>
  113. *
  114. * Classes that need to designate a replacement when an instance of it
  115. * is read from the stream should implement this special method with the
  116. * exact signature.<p>
  117. *
  118. * <PRE>
  119. * ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
  120. * </PRE><p>
  121. *
  122. * This readResolve method follows the same invocation rules and
  123. * accessibility rules as writeReplace.<p>
  124. *
  125. * The serialization runtime associates with each serializable class a version
  126. * number, called a serialVersionUID, which is used during deserialization to
  127. * verify that the sender and receiver of a serialized object have loaded
  128. * classes for that object that are compatible with respect to serialization.
  129. * If the receiver has loaded a class for the object that has a different
  130. * serialVersionUID than that of the corresponding sender's class, then
  131. * deserialization will result in an {@link InvalidClassException}. A
  132. * serializable class can declare its own serialVersionUID explicitly by
  133. * declaring a field named <code>"serialVersionUID"</code> that must be static,
  134. * final, and of type <code>long</code>:<p>
  135. *
  136. * <PRE>
  137. * ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
  138. * </PRE>
  139. *
  140. * If a serializable class does not explicitly declare a serialVersionUID, then
  141. * the serialization runtime will calculate a default serialVersionUID value
  142. * for that class based on various aspects of the class, as described in the
  143. * Java(TM) Object Serialization Specification. However, it is <em>strongly
  144. * recommended</em> that all serializable classes explicitly declare
  145. * serialVersionUID values, since the default serialVersionUID computation is
  146. * highly sensitive to class details that may vary depending on compiler
  147. * implementations, and can thus result in unexpected
  148. * <code>InvalidClassException</code>s during deserialization. Therefore, to
  149. * guarantee a consistent serialVersionUID value across different java compiler
  150. * implementations, a serializable class must declare an explicit
  151. * serialVersionUID value. It is also strongly advised that explicit
  152. * serialVersionUID declarations use the <code>private</code> modifier where
  153. * possible, since such declarations apply only to the immediately declaring
  154. * class--serialVersionUID fields are not useful as inherited members. Array
  155. * classes cannot declare an explicit serialVersionUID, so they always have
  156. * the default computed value, but the requirement for matching
  157. * serialVersionUID values is waived for array classes.
  158. *
  159. * @author unascribed
  160. * @see java.io.ObjectOutputStream
  161. * @see java.io.ObjectInputStream
  162. * @see java.io.ObjectOutput
  163. * @see java.io.ObjectInput
  164. * @see java.io.Externalizable
  165. * @since JDK1.1
  166. */
  167. extern interface Serializable
  168. {
  169. }