Guice can't do that automatically because no such thing as 
Class<List<String>>, for example.

You could accomplish MyCustomTypeTokenClass<T> with a just-in-time 
bindings.  Just let it take a TypeToken<T> in its constructor and do the 
conversion there.

On Tuesday, 1 July 2014 18:41:00 UTC-4, [email protected] wrote:
>
> Suppose I have the following:
>
> public class Foo<T> {
>     private final Class<T> tClass;
>     @Inject public Foo(Class<T> tClass) { this.tClass = tClass; }
> }
>
> I've heard that if tClass was instead a TypeLiteral, then I'd basically 
> have Foo<Integer>, Foo<String>, etc. for free.  But suppose Foo is a 
> third-party class, and I wish to duplicate its effects.  Since Class has no 
> automatic binding, I'm pretty much stuck with
>
> bind(new TypeLiteral<Class<Integer>>(){}).toInstance(Integer.class);
>
> Which has the drawback of, well, having to bind types that I know will be 
> used, leaving out unintended uses and such.
>
> Now, it's probably possible, and even trivial, to implement a 
> Provider<Class<T>> for which, given a TypeLiteral<T> (where T is a 
> non-generic type), returns a Class<T>.  However, I'm still stuck with 
> binding it in a non-generic manner.
>
> Is there a workaround for this, so that I essentially have Foo<Integer>, 
> Foo<String>, etc., for free?
>
> Also, would it be the same if instead tClass was MyCustomTypeTokenClass 
> (ie. not a TypeLiteral, but convertible from it)?
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/830fc4dc-8a40-49ba-b167-f4d2ccf7411c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to