...
Also see the related rule MET06-J. Do not invoke overridable methods in clone().
Noncompliant Code Example
This noncompliant code example invokes an overridable method from the readObject()
method.
Code Block | ||
---|---|---|
| ||
private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException { overridableMethod(); stream.defaultReadObject(); } public void overridableMethod() { // ... } |
Compliant Solution
This compliant solution removes the call to the overridable method. When removing such calls is infeasible, ensure that the overridable method is declared private
or final
.
Code Block | ||
---|---|---|
| ||
private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); } |
Exceptions
Wiki Markup |
---|
*SER11-EX1:* "The {{readObject}} methods will often call {{java.io.ObjectInputStream.defaultReadObject}}, which is an overridable method" \[[SCG 2009|AA. Bibliography#SCG 09]\]. Such calls are permitted. |
Risk Assessment
Invoking overridable methods from the readObject()
method can lead to initialization errors.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SER11-J | low | probable | medium | P4 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6f60b0176fd3d33b-4a96251e-47354889-899ca37d-ae49a9e5a2488fe7b077858c"><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="df9fdc7322caa2e9-804638de-44ab4379-b155b3cc-6171d98ce85ec40e9d0cbfc2"><ac:plain-text-body><![CDATA[ | [[SCG 2009 | AA. Bibliography#SCG 09]] | Guideline 4-4 Prevent constructors from calling methods that can be overridden | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="38d247af7b3b0ba2-74324b21-4b2f4a55-916aac9f-5d1fe3cb5c168f9226e607ca"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 17: "Design and document for inheritance or else prohibit it" | ]]></ac:plain-text-body></ac:structured-macro> |
...