Nice solution Ales, wish I'd thought of it :)<br><br><div class="gmail_quote">On Tue, Dec 6, 2011 at 15:45, Ales Justin <span dir="ltr"><<a href="mailto:ales.justin@gmail.com">ales.justin@gmail.com</a>></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> @SuppressWarnings({"unchecked"})</div>
<div> @Produces</div><div> public GenericQuery injectClass(InjectionPoint ip) {</div><div> Annotated annotated = ip.getAnnotated();</div><div> Class clazz = Object.class;</div><div> Type type = annotated.getBaseType();</div>
<div> if (type instanceof ParameterizedType) {</div><div> ParameterizedType pt = (ParameterizedType) type;</div><div> clazz = (Class) pt.getActualTypeArguments()[0];</div><div> }</div>
<div>
return new GenericQuery(clazz);</div><div> }</div><div><br></div><div>--</div><div><br></div><div><div> @Inject</div><div> GenericQuery<String> gqs;</div><div> @Inject</div><div> GenericQuery<Number> gqn;</div>
</div><div><br></div><div>--</div><div><br></div><div><div><div> System.out.println("GQS: " + gqs.getClazz());</div><div> System.out.println("GQN: " + gqn.getClazz());</div></div></div><span class="HOEnZb"><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 class="h5">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<T> {</div><div><br></div><div> public TypedQuery<T> 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'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<T> {</div><div><br></div><div> private Class<T> klass;</div><div><br>
</div>
<div> public TypedQuery<T> getQuery(){</div><div><br></div><div> }</div><div> </div><div> public void setKlass(Class<T> klass){</div><div> this.klass = klass;</div><div> }</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<T> {</div><div><br></div><div> private Class<T> klass;</div><div> </div><div> public Foo(Class<T> klass){</div>
<div> this.klass = klass;</div><div> }</div><div><br></div><div> public TypedQuery<T> 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></div></div><div class="im">
_______________________________________________<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">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>-- <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>