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 [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/aa19617c-ce44-40ca-87b8-8ac0bd95d50d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to