{"id":52,"date":"2007-12-19T21:33:47","date_gmt":"2007-12-19T20:33:47","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=52"},"modified":"2021-03-11T09:43:20","modified_gmt":"2021-03-11T08:43:20","slug":"java-generics-an-google-juice-tricks","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2007\/12\/java-generics-an-google-juice-tricks\/","title":{"rendered":"Java Generics an Google Juice Tricks"},"content":{"rendered":"<p>Since Java-5 generics are supported. Sadly, there&#8217;s no way to find out\/specify the parameters at runtime. Normally you can life without this, but in the case of the dependency-injection libraries, this can get difficult.<\/p>\n<p>Either you &#8220;ignore&#8221; the parameterization or you find a hack to deal with it. Ignoring works often, but sometime you really want a specific implementation which depends on the parameters:<\/p>\n<p><code>public class DemoOne{<br \/>\n\/\/ A string-list constructor.<br \/>\npublic DemoOne(List&lt;String&gt; strings){){\/*constructor*\/}<br \/>\n}public class DemoTwo{\/\/ A user-list constructor. If it is a user-list, I want a super-special-implementation injected.<br \/>\npublic DemoTwo(List&lt;User&gt; users){\/*constructor*\/}<br \/>\n} <\/code><\/p>\n<p><code> <\/code>A List with strings may have complete other needs than a list with users. The &#8220;User&#8221;-list is maybe a list which loads users from the database whereas the other is just an array. Of course, the list-example is a bit stupid, in a real project this is more complex.<\/p>\n<p>So\u00a0 &#8220;List&lt;User&gt;&#8221; and the &#8220;List&lt;String&gt;&#8221; shouldn&#8217;t be a the same. But how request\/specify a parameterized class? You can&#8217;t give a parameterized &#8220;Class&#8221;-instance as parameter to a method. Google <a TARGET=\"_blank\" HREF=\"http:\/\/code.google.com\/p\/google-guice\/\">Guice<\/a> has a simple trick to handle it. The create an anonymous inner class, which extends a generic type. By doing this, they can read out the type via Reflection:<\/p>\n<p><code>binder.bind(new TypeLiteral&lt;List&lt;String&gt;&gt;(){}).to(ArrayList.class);<br \/>\nbinder.bind(new TypeLiteral&lt;List&lt;User&gt;&gt;(){}).to(LinkedList.class);<br \/>\n<\/code><br \/>\nIn my opinion, this is a really simple and smart trick.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Java-5 generics are supported. Sadly, there&#8217;s no way to find out\/specify the parameters at runtime. Normally you can life without this, but in the case of the dependency-injection libraries,&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[15],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":1,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":3769,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/52\/revisions\/3769"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}