guice-grapher extension does not graph bindings in lower injector's modules
- Dominant language
- Java
- Stars
- 12.7k
- Forks
- 1.7k
- Avg merge
- 11m
- Merged PRs (30d)
- 2
Description
The `guice-grapher` extension can graph the bindings for a child injector fine but does not graph any bindings for the injector the child injector was created from. To illustrate this point, look at the following code to reproduce the problem:
```kotlin
import com.google.inject.Guice
import com.neuronrobotics.bowlerbuilder.view.cad.cadengine.util.GuiceGrapher
import org.jlleitschuh.guice.module
import org.junit.jupiter.api.Test
interface TestInterfaceA
class TestClassA : TestInterfaceA
interface TestInterfaceB
class TestClassB : TestInterfaceB
internal class TestGrapher {
@Test
fun `test grapher`() {
val injectorA = Guice.createInjector(module {
bind().to()
})
val injectorB = injectorA.createChildInjector(module {
bind().to()
})
GuiceGrapher.graph("test.dot", injectorB)
}
}
```
The graph this produces is:

The code for `GuiceGrapher` is:
```java
public class GuiceGrapher {
public static void graph(final String filename, final Injector inj) throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
final Injector injector = Guice.createInjector(new GraphvizModule());
final GraphvizGrapher renderer = injector.getInstance(GraphvizGrapher.class);
renderer.setOut(out);
renderer.setRankdir("TB");
renderer.graph(inj);
out = new PrintWriter(new File(filename), "UTF-8");
String s = baos.toString("UTF-8");
s = fixGrapherBug(s);
s = hideClassPaths(s);
out.write(s);
out.close();
}
private static String hideClassPaths(String s) {
s = s.replaceAll("\\w[a-z\\d_\\.]+\\.([A-Z][A-Za-z\\d_\\$]*)", "$1");
s = s.replaceAll("value=[\\w-]+", "random");
return s;
}
private static String fixGrapherBug(String s) {
s = s.replaceAll("style=invis", "style=solid");
s = s.replaceAll(" margin=(\\S+), ", " margin=\"$1\", ");
return s;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.