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)?