The Java API [API 2006] for the Serializable
inteface declares:
Classes that require special handling during
...
object serialization and deserialization
...
must implement special methods with
...
exactly the following signatures [API 2006]:
Code Block |
---|
private void writeObject(java.io.ObjectOutputStream out) |
...
throws IOException |
...
private void readObject(java.io.ObjectInputStream in) |
...
throws IOException, ClassNotFoundException; |
...
private void readObjectNoData() |
...
throws ObjectStreamException; |
Wiki Markup |
---|
According to the Serialization Specification \[[Sun 2006|AA. Bibliography#Sun 06]\], {{readResolve()}} and {{writeReplace()}} method documentation: |
For Serializable and Externalizable classes, the
readResolve
method allows a class to replace/resolve the object read from the stream before it is returned to the caller. By implementing thereadResolve
method, a class can directly control the types and instances of its own instances being deserialized.For Serializable and Externalizable classes, the
writeReplace
method allows a class of an object to nominate its own replacement in the stream before the object is written. By implementing thewriteReplace
method, a class can directly control the types and instances of its own instances being serialized.
It is possible to add any access-specifier to the readResolve()
and writeReplace()
methods. However, if they are declared private
, extending classes cannot invoke or override them. Similarly, if either of these methods is declared static
, extending classes cannot override the method, they can only hide it.
Deviating from these method signatures produces a method that will, in fact, is not be invoked during object serialization or deserialization. Such a method will clearly not have the expected behavior. Such methods, especially if declared public
, might be accessible to untrusted code.
Be aware that, unlike Unlike most interfaces, Serializable
does not define the method signatures it requires . It cannot, because readObject()
and writeObject()
are private
. Consequently, the Java compiler will not identify a deviant an incorrect method signature.
Noncompliant Code Example
This noncompliant code example shows a class Ser
with a private
constructor, indicating that code external to the class should not be unable able to create instances of it. The class implements java.io.Serializable
and defines public readObject()
and writeObject()
methods. Consequently, untrusted code can obtain the reconstituted objects by using readObject()
, and can write to the stream by using writeObject()
.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e08a1f62ba59c9e4-a65b0fd5-4b54454a-b0569908-62002e9e13481df11deecb89"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2c5184abd9addd2b-ba467e08-416243b0-8b14aa59-aa3eef2eb1e137ea3f20fb26"><ac:plain-text-body><![CDATA[ | [[Sun 2006 | AA. Bibliography#Sun 06]] | "Serialization specification" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dd29dc9a25b43861-b264f160-4be94f72-a1b5905b-2fd9b9bc599156b21cdc9606"><ac:plain-text-body><![CDATA[ | [[Ware 2008 | AA. Bibliography#Ware 08]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...