You could use Guice’s Types class to generate the parameterized type: https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/util/Types.html#newParameterizedType-java.lang.reflect.Type-java.lang.reflect.Type…- (https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/util/Types.html#newParameterizedType-java.lang.reflect.Type-java.lang.reflect.Type...-)
For example: Types.newParameterizedType( MongoCollection.class, MyDocument.class ); On Sunday, 13 September 2015 at 23:19, Tim Boudreau wrote: > I'm working on Guice bindings for MongoDB's new async driver. The goal is to > be able to call, e.g. > > mongoModule.bindCollection("myCollection", MyDocument.class) > > and then be able to inject > @Named("myCollection") MongoCollection<MyDocument> > > I have this working just fine, but I'm actually implementing the JDK's > ParameterizedType to do it (code below). > > Any reason this is a bad way to go about it, or that this approach might > explode on some future version of Guice? It appears to work fine - things > are injected as they should be - but implementing ParameterizedType is a > little out-there. > > I did this for the older MongoDB driver by forcing the caller to pass a > TypeLiteral, which is a bit ugly since they're also passing the type > parameter as a Class object. So this results in a nicer API. > > Thanks, > > Tim > > > MongoTypedCollectionProvider<T> typedProvider = new > MongoTypedCollectionProvider<T>(dbProvider, collection, type, knownProvider, > opts, inits); > Type t = new FakeType<>(type); > Key<MongoCollection<T>> key = (Key<MongoCollection<T>>) > Key.get(t, Names.named(bindingName)); > binder.bind(key).toProvider(typedProvider); > > > > static class FakeType<T> implements ParameterizedType { > > private final Class<T> genericType; > > public FakeType(Class<T> genericType) { > this.genericType = genericType; > } > > @Override > public String getTypeName() { > return MongoCollection.class.getName(); > } > > @Override > public Type[] getActualTypeArguments() { > return new Type[]{genericType}; > } > > @Override > public Type getRawType() { > return MongoCollection.class; > } > > @Override > public Type getOwnerType() { > return null; > } > } > > > -- > 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 google-guice+unsubscr...@googlegroups.com > (mailto:google-guice+unsubscr...@googlegroups.com). > To post to this group, send email to google-guice@googlegroups.com > (mailto:google-guice@googlegroups.com). > 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/aa19617c-ce44-40ca-87b8-8ac0bd95d50d%40googlegroups.com > > (https://groups.google.com/d/msgid/google-guice/aa19617c-ce44-40ca-87b8-8ac0bd95d50d%40googlegroups.com?utm_medium=email&utm_source=footer). > For more options, visit https://groups.google.com/d/optout. -- 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 google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. 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/C29F9D7163B748F7A0A4FC171FB5AE12%40gmail.com. For more options, visit https://groups.google.com/d/optout.