google / google/guice

guice-grapher extension does not graph bindings in lower injector's modules

Open
#1,236 0 comments 0 reactions 0 assignees View on GitHub
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:
![image](https://user-images.githubusercontent.com/4064722/53776819-49413380-3ec5-11e9-8371-62de1ab0b26b.png)

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.