{"id":1729,"date":"2012-02-13T14:42:29","date_gmt":"2012-02-13T14:42:29","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1729"},"modified":"2012-02-13T14:46:44","modified_gmt":"2012-02-13T14:46:44","slug":"cdiweld-fails-to-inject-a-reference-with-nested-parameterised-types","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1729","title":{"rendered":"CDI\/Weld fails to inject a reference with nested parameterised types"},"content":{"rendered":"<p>Weld 1.0 was failing with \u201cWELD-001408 Injection point has unsatisfied dependencies\u201d when nested parameterised types were present.<\/p>\n<p>The referenced bean was correctly present and annotated.<\/p>\n<p>I found that I could work around the problem by simplifying\/removing some of the generics. This allowed the beans to inject, but also gave warnings about the use of raw types. For example:-<\/p>\n<blockquote>\n<p>\/\/Original code     <br \/>&#160;&#160;&#160; private @Inject TableCtrl&lt;TreeNodePath&lt;TaxonomyNode&gt;, RowMetadata&gt; selectionTable;<\/p>\n<p>\/\/was replaced with the following     <br \/>&#160;&#160;&#160; private @Inject TableCtrl&lt;TreeNodePath, RowMetadata&gt; selectionTable;<\/p>\n<\/blockquote>\n<p>Attempts to use a CDI extension to find out more about what was happening did not reveal any more insight. <a href=\"http:\/\/salientsoft.co.uk\/?p=1354\"><strong>This post<\/strong><\/a> discusses Weld performance and debugging and refers to a Stack Overflow post on the subject.<\/p>\n<p>My solution to the problem (which I have also logged on Mantis) is&#160; as follows :-<\/p>\n<ol>\n<li>Injecting the bean into a raw type does not suffer from the problem.<\/li>\n<li>I therefore inject into a temporary raw type, and cast that to the correct type.<\/li>\n<li>Wrapping this in a method annotated with @Inject neatly solves the problem, as the method can take the raw types as arguments, and cast to the correctly parameterised fields in the method.<\/li>\n<li>As all this is done in a method, warnings can be suppressed for the method. This is a tidy solution, as the method only has this specific purpose, and no other warnings in the class are incorrectly suppressed.<\/li>\n<li>This is far better than the original workaround which involved hacking the generics back to non nested parameterised types throughout &#8211; this meant hacking a number of classes. The current solution entirely isolates the issue and allows the desired generics to be correctly used everywhere.<\/li>\n<\/ol>\n<p>An example of the correct generic declarations and the method used follows :-<\/p>\n<blockquote>\n<p>public abstract class TreeBrowser&lt;N extends TreeNode&lt;T&gt;, T extends Tree&lt;N&gt;, P extends TreeNodePath&lt;N&gt;&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; implements Serializable, BreadcrumbCtrlEvent&lt;N&gt; {      <br \/>&#8230;      <br \/>&#160;&#160;&#160; private CrumbComparator&lt;N&gt; crumbComparator;      <br \/>&#160;&#160;&#160; private BreadcrumbCtrl&lt;N&gt; breadcrumb;&#160;&#160;&#160; <br \/>&#160;&#160;&#160; private TableCtrl&lt;N, RowMetadata&gt; treeNodeTable;      <br \/>&#160;&#160;&#160; private TableCtrl&lt;P, RowMetadataStatusMap&gt; trayTable;      <br \/>&#8230;      <br \/>&#160;&#160;&#160; @Inject      <br \/>&#160;&#160;&#160; @SuppressWarnings({&quot;rawtypes&quot;, &quot;unchecked&quot;})      <br \/>&#160;&#160;&#160; void injectWeldUnsatisfiedGenericBeans(CrumbComparator crumbComparator, BreadcrumbCtrl breadcrumb,      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TableCtrl treeNodeTable, TableCtrl trayTable) {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.crumbComparator = crumbComparator;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.breadcrumb = breadcrumb;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.treeNodeTable = treeNodeTable;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.trayTable = trayTable;      <br \/>&#160;&#160;&#160; }<\/p>\n<\/blockquote>\n<p>&#160;<\/p>\n<p>This solved the problem.   <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Weld 1.0 was failing with \u201cWELD-001408 Injection point has unsatisfied dependencies\u201d when nested parameterised types were present. The referenced bean was correctly present and annotated. I found that I could work around the problem by simplifying\/removing some of the generics. This allowed the beans to inject, but also gave warnings about the use of raw [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[150],"tags":[13,110,40,16,111],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1729"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1729"}],"version-history":[{"count":3,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1729\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1729\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}