bda0882713bb0588d2f1835822fb1b9384ffaca5
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / ClassGraph.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.inspector.java.structure;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.PrintWriter;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import eu.svjatoslav.commons.file.CommonPathResolver;
19
20 public class ClassGraph {
21
22         /**
23          * Maps class fully qualified names to class descriptors.
24          */
25         Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
26
27         public ClassGraph() {
28         }
29
30         public ClassGraph(final Class<? extends Object> clazz) {
31                 addClass(clazz);
32         }
33
34         public ClassGraph(final Object root) {
35                 addClass(root.getClass());
36         }
37
38         public ClassDescriptor addClass(final Class<? extends Object> clazz) {
39
40                 if (clazz == null)
41                         return null;
42
43                 final String className = clazz.getName();
44
45                 if (nameToClassMap.containsKey(className))
46                         return nameToClassMap.get(className);
47
48                 return new ClassDescriptor(clazz, this);
49         }
50
51         public ClassDescriptor addObject(final Object object) {
52                 return addClass(object.getClass());
53         }
54
55         public void generateGraph(final String graphName) {
56                 generateGraph(graphName, false);
57         }
58
59         public void generateGraph(final String graphName, final boolean keepDotFile) {
60
61                 final String desktopPath = CommonPathResolver.getDesktopDirectory()
62                                 .getAbsolutePath() + "/";
63
64                 final String dotFilePath = desktopPath + graphName + ".dot";
65                 final String imageFilePath = desktopPath + graphName + ".png";
66
67                 System.out.println("Dot file path:" + dotFilePath);
68
69                 try {
70                         // write DOT file to disk
71                         final PrintWriter out = new PrintWriter(dotFilePath);
72                         out.write(getDot());
73                         out.close();
74
75                         // execute GraphViz to visualize graph
76                         try {
77                                 Runtime.getRuntime()
78                                                 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
79                                                                 imageFilePath }).waitFor();
80                         } catch (final InterruptedException e) {
81                         } finally {
82                         }
83
84                         if (!keepDotFile) {
85                                 // delete dot file
86                                 final File dotFile = new File(dotFilePath);
87                                 dotFile.delete();
88                         }
89                 } catch (final IOException e) {
90                         System.err.println(e);
91                 }
92         }
93
94         private String getDot() {
95                 final StringBuffer result = new StringBuffer();
96
97                 result.append("digraph Java {\n");
98                 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
99
100                 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
101                                 .entrySet())
102                         result.append(entry.getValue().getDot());
103
104                 result.append("}\n");
105
106                 final String resultStr = result.toString();
107                 return resultStr;
108         }
109
110 }