-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissingVoidConstructorsOnSerializable.qhelp
More file actions
51 lines (39 loc) · 1.87 KB
/
MissingVoidConstructorsOnSerializable.qhelp
File metadata and controls
51 lines (39 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A serializable class that is a subclass of a non-serializable class cannot be deserialized if its
superclass does not declare a no-argument constructor. The Java serialization framework uses the no-argument constructor when it initializes the
object instance that is created during deserialization. Deserialization fails with an <code>InvalidClassException</code> if
its superclass does not declare a no-argument constructor.
</p>
<p>The Java Development Kit API documentation states:</p>
<blockquote>
<p>To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of
the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it
extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class <code>Serializable</code> if this
is not the case. The error will be detected at runtime. </p>
</blockquote>
</overview>
<recommendation>
<p>Make sure that every non-serializable class that is extended by a serializable class has a no-argument constructor.</p>
</recommendation>
<example>
<p>In the following example, the class <code>WrongSubItem</code> cannot be deserialized because its
superclass <code>WrongItem</code> does not declare a no-argument constructor. However, the class
<code>SubItem</code> <em>can</em> be serialized because it declares a no-argument constructor.</p>
<sample src="MissingVoidConstructorsOnSerializable.java" />
</example>
<references>
<li>
Java API Documentation:
<a href="http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html">Serializable</a>.
</li>
<li>
J. Bloch, <em>Effective Java (second edition)</em>, Item 74.
Addison-Wesley, 2008.
</li>
</references>
</qhelp>