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