http://wiki.apidesign.org/index.php?title=Serialization&feed=atom&action=historySerialization - Revision history2024-03-28T08:59:39ZRevision history for this page on the wikiMediaWiki 1.12.0rc1http://wiki.apidesign.org/index.php?title=Serialization&diff=6936&oldid=prevJaroslavTulach: /* Can One Change Value of a final field? */2013-09-23T16:59:45Z<p><span class="autocomment">Can One Change Value of a final field?</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 16:59, 23 September 2013</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 3:</td>
<td colspan="2" class="diff-lineno">Line 3:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Can One Change Value of a '''final''' field? ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Can One Change Value of a '''final''' field? ==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>Today, at [[JavaOne2013]] I've noticed a question whether custom deserialization can modify '''final'' field. So here is the answer:</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>Today, at [[JavaOne2013]] I've noticed a question whether custom deserialization can modify '''final<ins style="color: red; font-weight: bold; text-decoration: none;">'</ins>'' field. So here is the answer:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=Serialization&diff=6933&oldid=prevJaroslavTulach at 16:54, 23 September 20132013-09-23T16:54:54Z<p></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 16:54, 23 September 2013</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 17:</td>
<td colspan="2" class="diff-lineno">Line 17:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> </div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div> <ins style="color: red; font-weight: bold; text-decoration: none;">private final </ins>void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> try {</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> try {</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> ObjectInputStream.GetField fields = ois.readFields();</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> ObjectInputStream.GetField fields = ois.readFields();</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Line 23:</td>
<td colspan="2" class="diff-lineno">Line 23:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> Field f = App.class.getDeclaredField("value");</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> Field f = App.class.getDeclaredField("value");</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> f.setAccessible(true);</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> f.setAccessible(true);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> f.setInt(this, <del style="color: red; font-weight: bold; text-decoration: none;">value</del>);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div> f.setInt(this, <ins style="color: red; font-weight: bold; text-decoration: none;">newValue</ins>);</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> } catch (Exception ex) {</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> } catch (Exception ex) {</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> throw new IOException(ex);</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> throw new IOException(ex);</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Line 29:</td>
<td colspan="2" class="diff-lineno">Line 29:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>}</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>}</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>public class AppTest {</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>public class AppTest {</div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=Serialization&diff=6931&oldid=prevJaroslavTulach: New page: Java has built in serialization mechanism based around {{JDK|java/io|Serialization}} which NetBeans used to use a lot at the end of last century. We moved away from it as it slowed...2013-09-23T16:20:41Z<p>New page: <a href="/wiki/Java" class="mw-redirect" title="Java">Java</a> has built in serialization mechanism based around {{JDK|java/io|Serialization}} which <a href="/wiki/NetBeans" title="NetBeans">NetBeans</a> used to use a lot at the end of last century. We moved away from it as it slowed...</p>
<p><b>New page</b></p><div>[[Java]] has built in serialization mechanism based around {{JDK|java/io|Serialization}} which [[NetBeans]] used to use a lot at the end of last century. We moved away from it as it slowed down start (a lot of reflection) and was ''too automatic'' (many developers just could not understand what they are doing and keep [[BackwardCompatibility]]).<br />
<br />
== Can One Change Value of a '''final''' field? ==<br />
<br />
Today, at [[JavaOne2013]] I've noticed a question whether custom deserialization can modify '''final'' field. So here is the answer:<br />
<br />
<source lang="java"><br />
public final class App implements Serializable {<br />
private final int value;<br />
<br />
public App(int value) {<br />
this.value = value;<br />
}<br />
<br />
public int getValue() {<br />
return value;<br />
}<br />
<br />
void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {<br />
try {<br />
ObjectInputStream.GetField fields = ois.readFields();<br />
int newValue = fields.get("value", 0);<br />
Field f = App.class.getDeclaredField("value");<br />
f.setAccessible(true);<br />
f.setInt(this, value);<br />
} catch (Exception ex) {<br />
throw new IOException(ex);<br />
}<br />
}<br />
}<br />
<br />
<br />
public class AppTest {<br />
@Test public void deserialize() throws Exception {<br />
ByteArrayOutputStream os = new ByteArrayOutputStream();<br />
ObjectOutputStream oos = new ObjectOutputStream(os);<br />
oos.writeObject(new App(10));<br />
oos.close();<br />
<br />
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());<br />
ObjectInputStream ois = new ObjectInputStream(is);<br />
App app = (App) ois.readObject();<br />
ois.close();<br />
<br />
assertEquals(app.getValue(), 10);<br />
}<br />
}<br />
<br />
</source></div>JaroslavTulach