Hey guys, how you doing?<div><br></div><div><div>I&#39;m trying to achieve something that might be impossible, but before concluding that, I&#39;d like to ask you, CDI gurus!</div><div><br></div><div>I have the following class:</div>
<div><br></div><div>public class Foo&lt;T&gt; {</div><div><br></div><div>    public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>    }</div><div><br></div><div>}</div><div><br></div><div>As you can Imagine, inside my getQuery method, I&#39;d have to use &quot;T.class&quot; to make it TypedQuery. which is impossible due java generics type erasure.</div>
<div><br></div><div>so I&#39;d have to build a private field to hold the t.class for me.</div><div><br></div><div><div>public class Foo&lt;T&gt; {</div><div><br></div><div>    private Class&lt;T&gt; klass;</div><div><br></div>
<div>    public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>    }</div><div>    </div><div>    public void setKlass(Class&lt;T&gt; klass){</div><div>        this.klass = klass;</div><div>    }</div><div>}</div>
</div><div><br></div><div>The problem is that forcing this &#39;setKlass&#39; feels very ugly to the api, and it&#39;s not very error prone, since one could easily forget to set this configuration.</div><div><br></div><div>
So I had an Idea: force the setKlass inside the constructor:</div><div><br></div><div><div>public class Foo&lt;T&gt; {</div><div><br></div><div>    private Class&lt;T&gt; klass;</div><div>    </div><div>    public Foo(Class&lt;T&gt; klass){</div>
<div>        this.klass = klass;</div><div>    }</div><div><br></div><div>    public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>    }</div><div><br></div><div>}</div></div><div><br></div><div>Unfortunatelly, this breaks cdi, since it cannot inject it anymore. At least AFAIK.</div>
<div><br></div><div>So, is there a way out of this? maybe using a secret solder feature?</div></div>