...
The clone()
method returns a copy of the original object that reflects the state of the original object at the moment of cloning. This new object can be used without exposing the original object. Because the caller holds the only reference to the newly cloned instance, the instance fields cannot be changed without the caller's cooperation. This use of the clone()
method allows the class to remain securely mutable. (See rule OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code.)
The Point
class is declared final
to prevent subclasses from overriding the clone()
method. This enables the class to be suitably used without any inadvertent modifications of the original object. This solution also complies with rule OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code.
...
As a result, the original array values cannot be modified by a client. Note that a manual deep copy could be required when dealing with arrays of objects. This generally happens when the objects do not export a clone()
method. Refer to rule OBJ06-J. Defensively copy mutable inputs and mutable internal components for more information.
...
Code Block | ||
---|---|---|
| ||
private static final String[] items = { ... }; public static final List<String> itemsList = Collections.unmodifiableList(Arrays.asList(items)); |
Wiki Markup |
---|
Neither the original array values nor the {{public}} list can be modified by a client. For more details about unmodifiable wrappers, refer to |
\[void SEC14-J. Provide sensitive mutable classes with unmodifiable wrappers\]. This solution still applies if the array contains mutable items instead of {{String}}. |
Risk Assessment
Incorrectly assuming that final
references cause the contents of the referenced object to remain mutable can result in an attacker modifying an object thought by the programmer to be immutable.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2cb2d87570c66020-ae01b03e-4f884b1d-ace4bfd7-f606d64deb4170fa8c9367e5"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. References#Bloch 08]] | Item 13: Minimize the accessibility of classes and members | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6a7bc4802f154351-8a517a1d-4be14be7-97a69056-8dfd03ce3b7f047a92e4f8db"><ac:plain-text-body><![CDATA[ | [[Core Java 2004 | AA. References#Core Java 04]] | Chapter 6 | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="17c5fa4d9a4d7797-e56c53e4-4ede4d35-bcc2a206-fe325a7fdccba3e5296f46bd"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#JLS 05]] | [§4.12.4 "final Variables" | http://java.sun.com/docs/books /jls/third_edition/html/typesValues.html#4.12.4] ]]></ac:plain-text-body></ac:structured-macro> |
| ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c223d45c7abe1e0b-5d04b287-44ca4565-96cdb598-9a66546d7e4f83f03c2db1b2"><ac:plain-text-body><![CDATA[ | [[Mettler 2010B | AA. References#Mettler 2010B]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...