How can I inject a bounded-wildcard class?

40 views
Skip to first unread message

glenviewjeff

unread,
Jan 4, 2012, 5:33:39 PM1/4/12
to google...@googlegroups.com
Also posted on Stack Overflow.

Using Guice, I want to inject a bounded-wildcard class. To be clear, I don't want to inject an object, but inject a class type. I think the constructor should look something like this:

class A {
   
class<?> a;
   
@Inject A(TypeLiteral<Class<? extends SuperClass>> a) {
     
this.a = a.getRawType();
   
}
}

Is the above code is correct? How can I bind the TypeLiteral?

Thanks!
Jeff

glenviewjeff

unread,
Jan 4, 2012, 5:41:00 PM1/4/12
to google...@googlegroups.com

Thanks to MForster at Stack Overflow for his answer:

Guice doesn't allow binding TypeLiteral. But it's unclear why you would want to inject a TypeLiteral in the first place.

Maybe this is what you want:

class A {
 
Class<? extends SuperClass> a;
 
@Inject A(Class<? extends SuperClass> a) {
   
this.a = a;
 
}
}

together with this binding:

bind(new TypeLiteral<Class<? extends SuperClass>>() {})
   
.toInstance(SubClass.class);
Reply all
Reply to author
Forward
0 new messages