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