{"id":11,"date":"2011-07-16T22:09:00","date_gmt":"2011-07-16T12:09:00","guid":{"rendered":"http:\/\/www.lazydungeon.com\/blog\/strange-generics-in-java\/"},"modified":"2011-07-16T22:09:00","modified_gmt":"2011-07-16T12:09:00","slug":"strange-generics-in-java","status":"publish","type":"post","link":"https:\/\/www.lazydungeon.com\/blog\/strange-generics-in-java\/","title":{"rendered":"Strange Generics in Java"},"content":{"rendered":"<p>So I had an interesting issue in my new project today.<\/p>\n<p>Th cause of the bug is not that strange itself, what essentially happened was that a Hibernate mapping was missing in one of the project&#8217;s shared libraries. This caused the query that was suppose to return a collection of the domain objects to instead return a collection of map objects.<\/p>\n<p>So far, totally understandable behavior. But it&#8217;s how I found out about this bug that was unusual.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nCollection&lt;Domain&gt; objs = daoImpl.getDomains(); \/\/ was actually returning a collection of maps\nif (objs != null) {\n  for (Domain d : objs) {\n    \/\/do something with d    \n  }\n}\n\n<\/pre>\n<p>This is roughly the code I had that used the share library. I had expected the InvalidCastException to be thrown at line 1. However it was not until line 3 when the for loop was executing before Java detected that something was wrong.<\/p>\n<p>To be sure of this I even stepped the code through the debugger, yep, I can clearly see that it was a Collection of maps, and it is definitely not picked up until the for loop.<\/p>\n<p>It was driving me crazy, I even entertained the thought that maybe java.util.Map secretly inherited my domain object somehow.<\/p>\n<p>Then it suddenly hit me, Java generic type checking is enforced only at compile time. Since Hibernate is creating the domain objects by reflection, the compiler couldn&#8217;t possibly have known the class mismatch.<\/p>\n<p>Lesson to remember, beware when pulling in new shared library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I had an interesting issue in my new project today. Th cause of the bug is not that strange itself, what essentially happened was that a Hibernate mapping was missing in one of the project&#8217;s shared libraries. This caused the query that was suppose to return a collection of the domain objects to instead [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[26,21],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-story","tag-debug","tag-java-2"],"_links":{"self":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":0,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}