{"id":5420,"date":"2014-03-30T21:44:06","date_gmt":"2014-03-30T21:44:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/access-static-method-on-generic-class-collection-of-common-programming-errors\/"},"modified":"2014-03-30T21:44:06","modified_gmt":"2014-03-30T21:44:06","slug":"access-static-method-on-generic-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/access-static-method-on-generic-class-collection-of-common-programming-errors\/","title":{"rendered":"Access static method on generic class-Collection of common programming errors"},"content":{"rendered":"<p>You can&#8217;t do that without reflection, because the type <code>T<\/code> is erased at runtime (meaning it will be reduced to its lower bound, which is <code>Base<\/code>).<\/p>\n<p>Since you <strong>do<\/strong> have access to a <code>Class<\/code> you can do it with reflection, however:<\/p>\n<pre><code>return (String) clazz.getMethod(\"getStaticName\").invoke(null);\n<\/code><\/pre>\n<p>Note that I&#8217;d consider such code to be code smell and that it is pretty fragile. Could you tell us why you need that?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t do that without reflection, because the type T is erased at runtime (meaning it will be reduced to its lower bound, which is Base). Since you do have access to a Class you can do it with reflection, however: return (String) clazz.getMethod(&#8220;getStaticName&#8221;).invoke(null); Note that I&#8217;d consider such code to be code smell and [&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-5420","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5420","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=5420"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5420\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}