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