{"id":4755,"date":"2014-03-30T15:08:12","date_gmt":"2014-03-30T15:08:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/howto-deserialize-class-if-class-version-was-changed-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:08:12","modified_gmt":"2014-03-30T15:08:12","slug":"howto-deserialize-class-if-class-version-was-changed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/howto-deserialize-class-if-class-version-was-changed-collection-of-common-programming-errors\/","title":{"rendered":"Howto deserialize class, if class version was changed-Collection of common programming errors"},"content":{"rendered":"<p>You won&#8217;t be able to perform the cast in the last line, because it&#8217;s of a different class (which is not castable) &#8211; you might get a confusing message such as <code>test.SerObj is not an instance of test.SerObj<\/code>.<\/p>\n<p>This arises from the fact that a class is essentially a <code>java.lang.Class<\/code> instance, which critically <em>is unique within a given classloader<\/em>. Even if you referenced the exact same <code>*.class<\/code> file, an instance of <code>SerObj<\/code> loaded by the default classloader is a different class from an instance of <code>SerObj<\/code> loaded by <code>clazLader<\/code>. Neither class could be cast to the other one.<\/p>\n<p>Thus it is not going to be possible for the method to return <code>SerObj<\/code> if you need to use multiple classloaders to deserialise. (Besides &#8211; how could it, when the two class files could have arbitrary differences?)<\/p>\n<p>One possible workaround is to define an interface that defines the behaviour which is fixed between versions of the class. If you set up the temporary classloader such that it can <em>only<\/em> load <code>SerObj<\/code> from the oldversion class file, and that it has the right parent classloader, then your old <code>SerObj<\/code> should still implement the interface class as defined by the root classloader.<\/p>\n<p>In this was, both the new <code>SerObj<\/code> and old <code>SerObj<\/code> instances would be castable to the <code>SerObjIface<\/code> interface, and so you could declare your method to return <em>that<\/em>. And in fact if you want callers to deal with different yet similar classes (which is what different versions of &#8220;the same class&#8221; are), you should arguably be returning an interface for this anyway.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You won&#8217;t be able to perform the cast in the last line, because it&#8217;s of a different class (which is not castable) &#8211; you might get a confusing message such as test.SerObj is not an instance of test.SerObj. This arises from the fact that a class is essentially a java.lang.Class instance, which critically is unique [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4755","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4755"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4755\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}