Jeff Axelrod Jeff Axelrod - 1 year ago 116
Java Question

Is there any way to get Guice Grapher to work?

There's a bug in the Guice grapher utility that causes most or all graphs to render corrupted. Is there a workaround or fix for this?

Answer Source

I modified @wuppi's answer slightly to also hide class paths and long random name annotations to make the graph much more compact and readable. His answer with edited code follows:

I find this utility method pretty useful and it never pritned incorrect graphs for me.

Regarding the style=invis bug: The Guice grapher plugin generates a dot file, which styles some of the clases as invisible. The replaceAll() in the below posted method works around that. The rest of the code is nearly the same from the Guice example.

I've incorporated Scot's fix for Guice 4.x, which included Tim's answer as well:

public class Grapher {
    public static void main(String[] args) throws Exception {
        Grapher.graph4("", Guice.createInjector(new MyModule()));
    public static void graph4(String filename, Injector inj) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);

        Injector injector = Guice.createInjector(new GraphvizModule());
        GraphvizGrapher renderer = injector.getInstance(GraphvizGrapher.class);

        out = new PrintWriter(new File(filename), "UTF-8");
        String s = baos.toString("UTF-8");
        s = fixGrapherBug(s);
        s = hideClassPaths(s);

    public 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;

    public static String fixGrapherBug(String s) {
        s = s.replaceAll("style=invis", "style=solid");
        s = s.replaceAll("margin=(\\S+), ", " margin=\"$1\", ");
        return s;

Of course you are free to generate any other Filename :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download