2 * JavaInspect - Utility to visualize java software
3 * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.javainspect.structure;
13 import java.io.IOException;
14 import java.io.PrintWriter;
15 import java.util.HashMap;
18 import eu.svjatoslav.commons.file.CommonPathResolver;
23 * Maps class fully qualified names to class descriptors.
25 Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
30 public Graph(final Class<? extends Object> clazz) {
34 public Graph(final Object root) {
35 addClass(root.getClass());
38 public ClassDescriptor addClass(final Class<? extends Object> clazz) {
43 final String className = clazz.getName();
45 if (nameToClassMap.containsKey(className))
46 return nameToClassMap.get(className);
48 return new ClassDescriptor(clazz, this);
51 public ClassDescriptor addObject(final Object object) {
52 return addClass(object.getClass());
55 public void generateGraph(final String graphName) {
56 generateGraph(graphName, false);
59 public void generateGraph(final String graphName, final boolean keepDotFile) {
61 final String desktopPath = CommonPathResolver.getDesktopDirectory()
62 .getAbsolutePath() + "/";
64 final String dotFilePath = desktopPath + graphName + ".dot";
65 final String imageFilePath = desktopPath + graphName + ".png";
67 System.out.println("Dot file path:" + dotFilePath);
70 // write DOT file to disk
71 final PrintWriter out = new PrintWriter(dotFilePath);
75 // execute GraphViz to visualize graph
78 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
79 imageFilePath }).waitFor();
80 } catch (final InterruptedException e) {
86 final File dotFile = new File(dotFilePath);
89 } catch (final IOException e) {
90 System.err.println(e);
94 private String getDot() {
95 final StringBuffer result = new StringBuffer();
97 result.append("digraph Java {\n");
98 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
100 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
102 result.append(entry.getValue().getDot());
104 result.append("}\n");
106 final String resultStr = result.toString();