added possibility to whitelist or blacklist classes of packages by wildcard pattern
[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 Filter filter = new Filter();
28
29         public ClassGraph() {
30         }
31
32         public ClassGraph(final Class<? extends Object> clazz) {
33                 addClass(clazz);
34         }
35
36         public ClassGraph(final Object root) {
37                 addClass(root.getClass());
38         }
39
40         public ClassDescriptor addClass(final Class<? extends Object> clazz) {
41
42                 if (clazz == null)
43                         return null;
44
45                 final String className = clazz.getName();
46
47                 if (nameToClassMap.containsKey(className))
48                         return nameToClassMap.get(className);
49
50                 return new ClassDescriptor(clazz, this);
51         }
52
53         public ClassDescriptor addObject(final Object object) {
54                 return addClass(object.getClass());
55         }
56
57         public void generateGraph(final String graphName) {
58                 generateGraph(graphName, false);
59         }
60
61         public void generateGraph(final String graphName, final boolean keepDotFile) {
62
63                 final String desktopPath = CommonPathResolver.getDesktopDirectory()
64                                 .getAbsolutePath() + "/";
65
66                 final String dotFilePath = desktopPath + graphName + ".dot";
67                 final String imageFilePath = desktopPath + graphName + ".png";
68
69                 System.out.println("Dot file path:" + dotFilePath);
70
71                 try {
72                         // write DOT file to disk
73                         final PrintWriter out = new PrintWriter(dotFilePath);
74                         out.write(getDot());
75                         out.close();
76
77                         // execute GraphViz to visualize graph
78                         try {
79                                 Runtime.getRuntime()
80                                                 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
81                                                                 imageFilePath }).waitFor();
82                         } catch (final InterruptedException e) {
83                         } finally {
84                         }
85
86                         if (!keepDotFile) {
87                                 // delete dot file
88                                 final File dotFile = new File(dotFilePath);
89                                 dotFile.delete();
90                         }
91                 } catch (final IOException e) {
92                         System.err.println(e);
93                 }
94         }
95
96         private String getDot() {
97                 final StringBuffer result = new StringBuffer();
98
99                 result.append("digraph Java {\n");
100                 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
101
102                 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
103                                 .entrySet())
104                         result.append(entry.getValue().getDot());
105
106                 result.append("}\n");
107
108                 final String resultStr = result.toString();
109                 return resultStr;
110         }
111
112 }