...
Compliant Solution
This compliant solution uses an enum
and adds a custom readResolve()
method that replaces the deserialized instance with a reference to the appropriate singleton from the current execution. More complicated cases may also require custom writeObject()
or readObject()
methods in addition to (or instead of) the custom readResolve()
method.
Code Block | ||
---|---|---|
| ||
public enum NumberEnum { INSTANCE; class NumberData numberextends = new NumberData();Number { // ... protected final Object readResolve() throws NotSerializableException { return INSTANCE; } } public class NumberData extends Number { // ... } |
This compliant solution uses composition rather than extension of the Number
class. More information on singleton classes is available in rule MSC07-J. Prevent multiple instantiations of singleton objects.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6af76100cfe5bc2f-96f6ca53-449847b2-b505aabc-6fa1cc5fda051f8149c00f66"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. References#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f72c119f95d6f101-acd507f5-43cc474f-92d9a608-f813819ee97c7eba717af7ed"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. References#Bloch 08]] | Item 75, Consider using a custom serialized form | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3bbf8d210c1871f9-7d7c179e-427b42ae-8e5981f1-bac01c027d6741160f690782"><ac:plain-text-body><![CDATA[ | [[Greanier 2000 | AA. References#Greanier 00]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3c6aa87648299655-48291736-49c94d2f-884b87d8-2e6e5efb9c9d905d72852bfe"><ac:plain-text-body><![CDATA[ | [[Harold 1999 | AA. References#Harold 99]] | Chapter 11, Object Serialization, Validation | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ee0f1f33f6758d4c-d013dab5-44c24bea-8d519bca-6223b9981f7d45291a21803a"><ac:plain-text-body><![CDATA[ | [[Hawtin 2008 | AA. References#Hawtin 08]] | Antipattern 8. Believing deserialisation is unrelated to construction | ]]></ac:plain-text-body></ac:structured-macro> |
...