Guice: how to print module bindings?

I have been looking at a nasty bug in my code over the past 2 days, surrounding my Guice module and its associated bindings. I have about 20 bindings declared in my module, and only 1 of them does not work.

I have exhausted every option here, and I wonder if there is an-like module.print()method in the Guice library where I can print the String version of all module bindings; either, or somehow get Guice to log what he does when he reads in my bad binding.

+3
source share
3 answers
Injector injector = Guice.createInjector(myModuleInstance);
Map<Key<?>,Binding<?>> map = injector.getBindings();
for(Entry<Key<?>, Binding<?>> e : map.entrySet()) {
    System.out.println(e.getKey() + ": " + e.getValue());
}
+4
source

herpylderp . , (, , ), Guice SPI . , , ;)

: http://code.google.com/p/google-guice/wiki/ExtendingGuice#Examples

. , visit(Binding) ElementVisitor.

. : http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/spi/Elements.html

, Binding getSource(), .

. , , , .

+3

The same as for IAmYourFaja, but in a more modern style:

import static java.util.stream.Collectors.joining;
String stringBinder = injector.getAllBindings().entrySet().stream()
            .map(e ->  e.getKey() + ": " +  e.getValue())
            .collect(joining("\n"));
0
source

All Articles