java - Explicitly add binder dependency when binding to Guice provider instance -


say want create general provider returns first item in collection. like:

class p extends provider<t> {   private final provider<collection<t>> provider;    firstitemprovider(provider<collection<t>> provider) {     this.provider = provider;   }    @override   public t get() {     collection<t> input = provider.get();     iterator<t> iter = input.iterator();     return iter.hasnext() ? iter.next() : null;   } } 

now have various such collections different annotations , want add bindings provider, but, want able traverse bindings using defaultbindingtargetvisitor , getdependencies().

i've tried like:

bind(key.get(type, annotation))   .toprovider(new p(      getprovider(        key.get(types.collectionof(type.gettype()), annotation)      )   )); 

this works fine in terms of binding, traversal doesn't wok guice doesn't detect dependency.

i couldn't figure out other way bind annotated producer since doesn't have annotation on it.

is there way i'm trying do?

thanks yaron

this works me:

import java.util.arrays; import java.util.collection; import java.util.collections; import java.util.list; import java.util.set;  import javax.inject.provider;  import com.google.inject.abstractmodule; import com.google.inject.binder; import com.google.inject.guice; import com.google.inject.injector; import com.google.inject.key; import com.google.inject.name.names; import com.google.inject.spi.dependency; import com.google.inject.spi.providerinstancebinding; import com.google.inject.spi.providerwithdependencies;  class firstitemprovider<t> implements providerwithdependencies<t> {     private final provider<? extends collection<? extends t>> collectionprovider;     private final key<? extends collection<? extends t>> key;      public firstitemprovider(binder binder, key<? extends collection<? extends t>> key) {         this.collectionprovider = binder.getprovider(key);         this.key = key;     }      @override     public t get() {         return collectionprovider.get().iterator().next();     }      @override     public set<dependency<?>> getdependencies() {         return collections.<dependency<?>>singleton(dependency.get(key));     } }  class foomodule extends abstractmodule {     @override     protected void configure() {         key<list<string>> listkey = new key<list<string>>(names.named("foo")){};         key<string> stringkey = new key<string>(names.named("foo")){};          bind(listkey).toinstance(arrays.aslist("foo", "bar"));         bind(stringkey).toprovider(new firstitemprovider<>(binder(), listkey));     } }  public class foo {     public static void main(string[] args) {         injector injector = guice.createinjector(new foomodule());          key<string> stringkey = new key<string>(names.named("foo")){};         providerinstancebinding<string> binding = (providerinstancebinding<string>)injector.getbinding(stringkey);         system.out.println(binding.getdependencies());     } } 

the output is

[key[type=java.util.list<java.lang.string>, annotation=@com.google.inject.name.named(value=foo)]] 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -