optional provider for arbitrary binding

69 views
Skip to first unread message

Daniel Murphy

unread,
May 10, 2012, 5:15:42 PM5/10/12
to google...@googlegroups.com
Is there a way to get an optional provider from a binder (same functionality of @Inject(optional = true))?  

I have a case where I'm given an arbitrary binding annotation (I'm given the class), and I need to grab a provider for an optionally bound type with that binding annotation.

Thanks,
Daniel

Daniel Murphy

unread,
May 10, 2012, 5:36:08 PM5/10/12
to google...@googlegroups.com
Just solved this by having the provider have an Injector dependency for HasDependencies, and then made an @Inject void initialize(Injector inj) method that uses inj.getExistingBinding(Key), which I then could then check if it was null.   Ends up not being a static binding, but that makes sense because you wouldn't know if it's available or not until the whole graph is built.

It would be nice to grab an optional provider from the binder, is that a feasible feature request?

- Daniel
Reply all
Reply to author
Forward
0 new messages