{"id":4209,"date":"2014-03-30T09:17:00","date_gmt":"2014-03-30T09:17:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/constructing-a-java-generic-with-an-unknown-subtype-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:17:00","modified_gmt":"2014-03-30T09:17:00","slug":"constructing-a-java-generic-with-an-unknown-subtype-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/constructing-a-java-generic-with-an-unknown-subtype-collection-of-common-programming-errors\/","title":{"rendered":"Constructing a Java generic with an unknown subtype-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>The code that interacts with the Blah makes use of the T class to do some reflection. It needs a concrete T instead of some parent class. &#8211; evilfred<\/p>\n<\/blockquote>\n<p>No, the code can&#8217;t interact with T at runtime, because it never gets to know what T is. Generics are ONLY relevant at compile time. What happens is that generics are preprocessed BEFORE the Java code is compiled into byte code. Byte code knows nothing about generics, ever.<\/p>\n<p>In:<\/p>\n<pre><code>protected  void addThing(T obj) {\n    process(new Blah(obj));\n}\n<\/code><\/pre>\n<p>Blah[Animal] is fixed at compile-time and you cannot turn the <em>process<\/em> method&#8217;s signature into Blah[Giraffe]. Not an option.<\/p>\n<p>What the code deals with at runtime is Blah[Animal], not T (or Blah&lt; T &gt;). Ever. What ever the subtype of what you create with new Blah&lt; T &gt; is, byte code will consider it as Blah[Animal]. You can&#8217;t create new types at runtime.<\/p>\n<p>Your idea of a solution:<\/p>\n<pre><code>Blah(Object obj) {\n    \/\/ Undesirable downcast.\n    this.thing = (T)obj;\n}\n<\/code><\/pre>\n<p>is not going to work, because T will be resolved at compile time.<\/p>\n<p>Now if you want to create Blah[Giraffe], don&#8217;t delegate the creation of new Blah to the addThing method. Do something like this:<\/p>\n<pre><code>private class Blah { }\nprivate class Giraffe { }\n\npublic Test_1() {\n    Blah f = new Blah();\n    addThing2(f);\n}\n\npublic  void addThing2(Blah obj) {\n    process(obj);\n}\n\npublic void process(Blah obj) { }\n<\/code><\/pre>\n<p>If you can&#8217;t modify addThing, then you probably don&#8217;t need to worry about creating a <em>generic with an unknown subtype at runtime<\/em> in the first place (because is it is impossible in Java). Your issue is\/would actually be a non-problem (for the process method at least).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code that interacts with the Blah makes use of the T class to do some reflection. It needs a concrete T instead of some parent class. &#8211; evilfred No, the code can&#8217;t interact with T at runtime, because it never gets to know what T is. Generics are ONLY relevant at compile time. What [&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-4209","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4209","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=4209"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4209\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}