How to get a Provider from an Element of type Binding in Guice?

19 Views Asked by At

Binding interface contains a method getProvider() that should do the conversion to a Provider. However most of implementations are based on BindingImpl that allows for getting a Provider only when an injector is available or when the binding is defined using a provider. Otherwise UnsupportedOperationException is throw.

How to reliably obtain a Provider corresponding to a Binding before an injector in created?

Or more generally how to use a Binding element as a source of value in my custom Provider?

Context: A library that I'd like to uses, exposes a Guice Module through which it integrates with the main app. I'd like to conditionally re-map selected services of that library. Methods of Elements class allow to cherry-pick bindings. I'd like to convert a selected Binding to a Provider so that it can be conditionally use it in my own provider.

0

There are 0 best solutions below