[seam-dev] CDI with generic class and the hell of type erasure.

xsalefter xsalefter at yahoo.com
Tue Dec 6 16:51:50 EST 2011


Hi all..

I'm not sure whether my comment is help you or not, but I ever thinking about this some time ago, and may be could be archived by something like:

public class Foo<T> {
  private Class<T> clazz;
  
  @Inject
  public Foo(Class<T> c) {
    this.clazz = c;
  }
}

And by using custom qualifier, creating instance of Foo is just like:
@InjectConstructor(Blah.class)

private Foo<Blah> foo;

Am I missing something?


Thanks,
xsalefter



>
>Hey guys, how you doing?
>
>
>I'm trying to achieve something that might be impossible, but before concluding that, I'd like to ask you, CDI gurus!
>
>
>I have the following class:
>
>
>public class Foo<T> {
>
>
>    public TypedQuery<T> getQuery(){
>
>
>    }
>
>
>}
>
>
>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.
>
>
>so I'd have to build a private field to hold the t.class for me.
>
>
>public class Foo<T> {
>
>
>    private Class<T> klass;
>
>
>    public TypedQuery<T> getQuery(){
>
>
>    }
>    
>    public void setKlass(Class<T> klass){
>        this.klass = klass;
>    }
>}
>
>
>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.
>
>
>So I had an Idea: force the setKlass inside the constructor:
>
>
>public class Foo<T> {
>
>
>    private Class<T> klass;
>    
>    public Foo(Class<T> klass){
>        this.klass = klass;
>    }
>
>
>    public TypedQuery<T> getQuery(){
>
>
>    }
>
>
>}
>
>
>Unfortunatelly, this breaks cdi, since it cannot inject it anymore. At least AFAIK.
>
>
>So, is there a way out of this? maybe using a secret solder feature?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/seam-dev/attachments/20111206/50ada5cb/attachment.html 


More information about the seam-dev mailing list