Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: REM cost reform

Wiki Markup"An inner class is a nested class that is not explicitly or implicitly declared static" \ [[JLS 2005|AA. Bibliography#JLS 05]\]. Serialization of inner classes (including local and anonymous classes) is error prone. According to the Serialization Specification \[[Sun 2006|AA. Bibliography#Sun 06]\],JLS 2015]. Serialization of inner classes (including local and anonymous classes) is error prone. According to the Serialization Specification [Sun 2006]:

  • Serializing an inner class declared in a non-static context that contains implicit non-transient references to enclosing class instances results in serialization of its associated outer class instance.
  • Synthetic fields generated by

...

  • Java compilers to implement inner classes are implementation dependent and may vary between compilers; differences in such fields can disrupt compatibility as well as result in conflicting default serialVersionUID values. The names assigned to local and anonymous inner classes are also implementation dependent and may differ between compilers.
  • Because inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields.
  • Because inner classes associated with outer instances do not have zero-argument constructors (constructors of such inner classes implicitly accept the enclosing instance as a prepended parameter), they cannot implement Externalizable. The Externalizable interface requires the implementing object to manually save and restore its state using the writeExternal() and readExternal() methods.

Consequently, programs must not serialize inner classes.

Because none of these issues apply to static member classes, serialization of these static member classes is permitted.

Noncompliant Code Example

In this noncompliant code example, the fields contained within the outer class are also serialized when the inner class is serialized. :

Code Block
bgColor#FFcccc

public class OuterSer implements Serializable {
  private int rank;
  class InnerSer implements Serializable {
    protected String name;
    // ...
  }
}

Compliant Solution

This The InnerSer class of this compliant solution omits implementation of deliberately fails to implement the Serializable interface in the InnerSer class.:

Code Block
bgColor#ccccff

public class OuterSer implements Serializable {
  private int rank;
  class InnerSer {
    protected String name;
    // ...
  }
}

Compliant Solution

It is allowable to declare If an inner and outer class must both be Serializable, the inner class as can be declared static to prevent its serialization. It is also permissible for a static inner class to implement Serializablea serialized inner class from also serializing its outer class.

Code Block
bgColor#ccccff

public class OuterSer implements Serializable {
  private int rank;
  static class InnerSer implements Serializable {
    protected String name;
    // ...
  }
}

Risk Assessment

Serialization of inner classes can introduce platform dependencies and can cause serialization of instances of the outer class.

Rule

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

SER05-J

medium

Medium

Likely

likely

Yes

medium

No

P18

P12

L1

Automated Detection

Detection of inner classes that implement serialization is straightforward.

ToolVersionCheckerDescription
Klocwork

Include Page
Klocwork_V
Klocwork_V

JAVA.SERIALIZE.INNER
SonarQube
Include Page
SonarQube_V
SonarQube_V
S2066
S2059
"Serializable" inner classes of non-serializable classes should be "static"
"Serializable" inner classes of "Serializable" classes should be static


Related Guidelines

MITRE CWE

CWE-499,

"

Serializable Class Containing Sensitive Data

"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3863f26d-1384-4418-adfb-7590c234bc30"><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="64c2e084-6fab-4e0d-9e01-9af311f82412"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 74: "Implement serialization judiciously"

]]></ac:plain-text-body></ac:structured-macro>

API 2014]

Interface Externalizable
Interface Serializable

[Bloch 2008]

Item 74, "Implement Serialization Judiciously"

[JLS 2015]

§8

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0e8df44d-2f1c-425a-9acb-cbffc2e69ad4"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[Section 8

.1.3, "Inner Classes and Enclosing Instances

http://java.sun.com/docs/books/jls/third_edition/html/classes.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id=

"

f8c296d1-93b7-402e-866e-219e6712d22d"><ac:plain-text-body><![CDATA[

[

[

Sun 2006]

AA. Bibliography#Sun 06]]

"Serialization specification"

]]></ac:plain-text-body></ac:structured-macro>

Serialization Specification, Section 1.10, "The Serializable Interface"


...

Image Added Image Added Image Removed      13. Serialization (SER)