<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">To reply, what we already discussed on irc.<div><br></div><div>This will do it:</div><div><br></div><div><pre style="color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; white-space: pre-wrap; ">         Class&lt;RequestHandler&gt; clazz = (Class&lt;RequestHandler&gt;) tmp;

         InjectionTarget&lt;RequestHandler&gt; it = manager.createInjectionTarget(manager.createAnnotatedType(clazz));
         CreationalContext&lt;RequestHandler&gt; cc = manager.createCreationalContext(null);
         // handler = it.produce(cc); // no need for this, if you already have an instance
         it.inject(handler, cc);</pre><div><br></div></div><div><br></div><div><a href="https://github.com/capedwarf/capedwarf-green/blob/master/server-api/src/main/java/org/jboss/capedwarf/server/api/servlet/WeldServlet.java">https://github.com/capedwarf/capedwarf-green/blob/master/server-api/src/main/java/org/jboss/capedwarf/server/api/servlet/WeldServlet.java</a></div><div><br><div><div>On Dec 7, 2011, at 1:21 PM, José Rodolfo Freitas wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Ales and All,<div><br></div><div>when</div><div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); "><br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
&nbsp;@Inject</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">&nbsp;GenericQuery&lt;String&gt; gqs;</div></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
<br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">my producer do:</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
<br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">&nbsp;new GenericQuery(clazz)</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
<br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">And then @Inject inside GenericQuery does not work (since I'm using new).</div>
<div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); "><br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
What's the best approach in this situation?</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); "><br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
Using BeanManagerProvider.getInstance() inside GenericQuery constructor to provide instances for the objects I want to inject?</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; background-color: rgba(255, 255, 255, 0.917969); ">
<br></div><div><br><div class="gmail_quote">On Tue, Dec 6, 2011 at 9:29 PM, José Rodolfo Freitas <span dir="ltr">&lt;<a href="mailto:joserodolfo.freitas@gmail.com">joserodolfo.freitas@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
that´s an awesome solution Ales!<div>I didn´t thought that I could obtain the parametizedType that way!</div><div>Testing this will be my first task tomorrow morning.<br><br>thank you all for your ideas.<div><div class="h5">
<br><br><div class="gmail_quote">
On Tue, Dec 6, 2011 at 8:55 PM, Jason Porter <span dir="ltr">&lt;<a href="mailto:lightguard.jp@gmail.com" target="_blank">lightguard.jp@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Nice solution Ales, wish I'd thought of it :)<div><div><br><br><div class="gmail_quote">On Tue, Dec 6, 2011 at 15:45, Ales Justin <span dir="ltr">&lt;<a href="mailto:ales.justin@gmail.com" target="_blank">ales.justin@gmail.com</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>You could do something like this:</div><div><br></div><div><div>&nbsp; &nbsp; @SuppressWarnings({"unchecked"})</div>



<div>&nbsp; &nbsp; @Produces</div><div>&nbsp; &nbsp; public GenericQuery injectClass(InjectionPoint ip) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Annotated annotated = ip.getAnnotated();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Class clazz = Object.class;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Type type = annotated.getBaseType();</div>



<div>&nbsp; &nbsp; &nbsp; &nbsp; if (type instanceof ParameterizedType) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ParameterizedType pt = (ParameterizedType) type;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clazz = (Class) pt.getActualTypeArguments()[0];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div>


<div>
&nbsp; &nbsp; &nbsp; &nbsp; return new GenericQuery(clazz);</div><div>&nbsp; &nbsp; }</div><div><br></div><div>--</div><div><br></div><div><div>&nbsp; &nbsp; @Inject</div><div>&nbsp; &nbsp; GenericQuery&lt;String&gt; gqs;</div><div>&nbsp; &nbsp; @Inject</div><div>&nbsp; &nbsp; GenericQuery&lt;Number&gt; gqn;</div>



</div><div><br></div><div>--</div><div><br></div><div><div><div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("GQS: " + gqs.getClazz());</div><div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("GQN: " + gqn.getClazz());</div></div></div><span><font color="#888888"><div>



<br></div><div>--</div><div><br></div><div><div>GQS: class java.lang.String</div><div>GQN: class java.lang.Number</div></div><div><br></div><div>--</div><div><br></div><div><a href="https://github.com/alesj/cdi-arq-workshop/commit/64860f15197be40a7714dfd8a7da931c1db11411" target="_blank">https://github.com/alesj/cdi-arq-workshop/commit/64860f15197be40a7714dfd8a7da931c1db11411</a></div>



<div><br></div><div>HTH</div><div><br></div><div>-Ales</div></font></span></div><div><br><blockquote type="cite"><div><div>Hey guys, how you doing?<div><br></div><div><div>I'm trying to achieve something that might be impossible, but before concluding that, I'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>&nbsp; &nbsp; public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>&nbsp; &nbsp; }</div><div><br></div><div>}</div><div><br></div><div>As you can Imagine, inside my getQuery method, I'd have to use "T.class" to make it TypedQuery. which is impossible due java generics type erasure.</div>




<div><br></div><div>so I'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>&nbsp; &nbsp; private Class&lt;T&gt; klass;</div><div><br>



</div>
<div>&nbsp; &nbsp; public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; public void setKlass(Class&lt;T&gt; klass){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; this.klass = klass;</div><div>&nbsp; &nbsp; }</div><div>}</div>




</div><div><br></div><div>The problem is that forcing this 'setKlass' feels very ugly to the api, and it'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>&nbsp; &nbsp; private Class&lt;T&gt; klass;</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; public Foo(Class&lt;T&gt; klass){</div>




<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.klass = klass;</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; public TypedQuery&lt;T&gt; getQuery(){</div><div><br></div><div>&nbsp; &nbsp; }</div><div><br></div><div>}</div></div><div><br></div><div>Unfortunatelly, this breaks cdi, since it cannot inject it anymore. At least&nbsp;AFAIK.</div>




<div><br></div><div>So, is there a way out of this? maybe using a secret solder feature?</div></div></div></div><div>
_______________________________________________<br>seam-dev mailing list<br><a href="mailto:seam-dev@lists.jboss.org" target="_blank">seam-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/seam-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/seam-dev</a><br>



</div></blockquote></div><br></div><br>_______________________________________________<br>
seam-dev mailing list<br>
<a href="mailto:seam-dev@lists.jboss.org" target="_blank">seam-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/seam-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/seam-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><div><div>-- <br>Jason Porter<br><a href="http://lightguard-jp.blogspot.com/" target="_blank">http://lightguard-jp.blogspot.com</a><br>
<a href="http://twitter.com/lightguardjp" target="_blank">http://twitter.com/lightguardjp</a><br>

<br>Software Engineer<br>Open Source Advocate<br>Author of Seam Catch - Next Generation Java Exception Handling<br><br>PGP key id: 926CCFF5<br>PGP key available at: <a href="http://keyserver.net/" target="_blank">keyserver.net</a>, <a href="http://pgp.mit.edu/" target="_blank">pgp.mit.edu</a><br>




</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div>
</blockquote></div><br></div></body></html>