
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
79 lines
2.1 KiB
Plaintext
79 lines
2.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When a ``++Serializable++`` object has a non-serializable ancestor in its inheritance chain, object deserialization (re-instantiating the object from file) starts at the first non-serializable class, and proceeds down the chain, adding the properties of each subsequent child class, until the final object has been instantiated.
|
|
|
|
|
|
In order to create the non-serializable ancestor, its no-argument constructor is called. Therefore the non-serializable ancestor of a ``++Serializable++`` class must have a no-arg constructor. Otherwise the class is ``++Serializable++`` but not deserializable.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public class Fruit {
|
|
private Season ripe;
|
|
|
|
public Fruit (Season ripe) {...}
|
|
public void setRipe(Season ripe) {...}
|
|
public Season getRipe() {...}
|
|
}
|
|
|
|
public class Raspberry extends Fruit
|
|
implements Serializable { // Noncompliant; nonserializable ancestor doesn't have no-arg constructor
|
|
private static final long serialVersionUID = 1;
|
|
|
|
private String variety;
|
|
|
|
public Raspberry(Season ripe, String variety) { ...}
|
|
public void setVariety(String variety) {...}
|
|
public String getVarity() {...}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public class Fruit {
|
|
private Season ripe;
|
|
|
|
public Fruit () {...}; // Compliant; no-arg constructor added to ancestor
|
|
public Fruit (Season ripe) {...}
|
|
public void setRipe(Season ripe) {...}
|
|
public Season getRipe() {...}
|
|
}
|
|
|
|
public class Raspberry extends Fruit
|
|
implements Serializable {
|
|
private static final long serialVersionUID = 1;
|
|
|
|
private String variety;
|
|
|
|
public Raspberry(Season ripe, String variety) {...}
|
|
public void setVariety(String variety) {...}
|
|
public String getVarity() {...}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Add a no-arg constructor to "xxx".
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 25 Sep 2014, 08:16:23 Ann Campbell wrote:
|
|
Implementation note: see References tab for FB rule this replaces
|
|
|
|
endif::env-github,rspecator-view[]
|