On Thu, Feb 5, 2009 at 12:33 AM, Dirk Olmes <[email protected]> wrote: > > On Feb 2, 7:57 pm, Michael Wölm <[email protected]> wrote: >> Hi Group, >> >> for my study project, I use Guice to apply Dependency Injection in a >> component-oriented system. >> For example, I have following binding set in the module: >> >> bind(IModule.class).to(Implementation.class); >> >> Ok, now there are these code snippets: >> >> ... >> Injector injector = Guice.createInjector(new MyModule()); >> ... >> >> ... somewhere else.... >> Class foo = ...; (esp. foo is now set to Implementation.class) >> // Is foo explicit described in the module description? (Which in this >> context would mean it is an inplementation of a module interface.) >> ... >> >> I want to know if the class foo is defined in any module used by the >> injector. Is that possible in Guice? > > If I understand you correctly, you want to check whether IModule is > bound inthe injector or not. This is what I use in such situations: > > Key<IModule> key = Key.get(IModule.class); > if (injector.getBinding(key) != null) > { > System.out.println("Hooray, IModule is bound"); > } >
Note that as of Guice 2.0 you should use injector.getBindings().get(key) otherwise in your example, a missing binding will throw an exception. Dhanji. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
