possibility to hide orphaned classes from graph
[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 import eu.svjatoslav.inspector.java.methods.Clazz;
20 import eu.svjatoslav.inspector.java.methods.ProjectScanner;
21
22 public class ClassGraph {
23
24         public static void render(final String graphName, final Class... classes) {
25                 final ClassGraph classGraph = new ClassGraph();
26
27                 for (final Class clazz : classes)
28                         classGraph.addClass(clazz);
29
30                 classGraph.generateGraph(graphName);
31         }
32
33         /**
34          * Maps class fully qualified names to class descriptors.
35          */
36         Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
37
38         public Filter filter = new Filter();
39
40         public ClassGraph() {
41         }
42
43         public ClassGraph(final Class<? extends Object> clazz) {
44                 addClass(clazz);
45         }
46
47         public ClassGraph(final Object root) {
48                 addClass(root.getClass());
49         }
50
51         public ClassDescriptor addClass(final Class<? extends Object> clazz) {
52
53                 if (clazz == null)
54                         return null;
55
56                 final String className = clazz.getName();
57
58                 if (nameToClassMap.containsKey(className))
59                         return nameToClassMap.get(className);
60
61                 return new ClassDescriptor(clazz, this);
62         }
63
64         public ClassDescriptor addObject(final Object object) {
65                 return addClass(object.getClass());
66         }
67
68         public void addProject(final String path) {
69                 final ProjectScanner projectScanner = new ProjectScanner(new File(path));
70                 for (final Clazz clazz : projectScanner.getAllClasses())
71                         try {
72                                 System.out.println("Class full name: " + clazz.getFullName());
73                                 final Class c = this.getClass().forName(clazz.getFullName());
74                                 addClass(c);
75                         } catch (final Exception exception) {
76                                 System.out.println("cannot add class: "
77                                                 + exception.getMessage());
78                         }
79         }
80
81         public void generateGraph(final String graphName) {
82                 generateGraph(graphName, false);
83         }
84
85         public void generateGraph(final String graphName, final boolean keepDotFile) {
86
87                 final String desktopPath = CommonPathResolver.getDesktopDirectory()
88                                 .getAbsolutePath() + "/";
89
90                 final String dotFilePath = desktopPath + graphName + ".dot";
91                 final String imageFilePath = desktopPath + graphName + ".png";
92
93                 System.out.println("Dot file path:" + dotFilePath);
94
95                 try {
96                         // write DOT file to disk
97                         final PrintWriter out = new PrintWriter(dotFilePath);
98                         out.write(getDot());
99                         out.close();
100
101                         // execute GraphViz to visualize graph
102                         try {
103                                 Runtime.getRuntime()
104                                                 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
105                                                                 imageFilePath }).waitFor();
106                         } catch (final InterruptedException e) {
107                         } finally {
108                         }
109
110                         if (!keepDotFile) {
111                                 // delete dot file
112                                 final File dotFile = new File(dotFilePath);
113                                 dotFile.delete();
114                         }
115                 } catch (final IOException e) {
116                         System.err.println(e);
117                 }
118         }
119
120         private String getDot() {
121                 final StringBuffer result = new StringBuffer();
122
123                 result.append("digraph Java {\n");
124                 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
125
126                 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
127                                 .entrySet())
128                         result.append(entry.getValue().getDot());
129
130                 result.append("}\n");
131
132                 final String resultStr = result.toString();
133                 return resultStr;
134         }
135
136         public void hideClassesWithoutReferences() {
137
138                 for (final ClassDescriptor classDescriptor : nameToClassMap.values())
139                         classDescriptor.hideClassIfNoReferences();
140
141         }
142
143 }