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.inspector.java.structure;
13 import java.io.IOException;
14 import java.io.PrintWriter;
15 import java.util.HashMap;
18 import eu.svjatoslav.commons.file.CommonPathResolver;
19 import eu.svjatoslav.inspector.java.methods.Clazz;
20 import eu.svjatoslav.inspector.java.methods.ProjectScanner;
22 public class ClassGraph {
24 public static void render(final String graphName, final Class... classes) {
25 final ClassGraph classGraph = new ClassGraph();
27 for (final Class clazz : classes)
28 classGraph.addClass(clazz);
30 classGraph.generateGraph(graphName);
34 * Maps class fully qualified names to class descriptors.
36 Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
38 private Filter filter = new Filter();
45 * classes that shall be added to graph
47 public ClassGraph(final Class<? extends Object>... classes) {
48 for (final Class<? extends Object> clazz : classes)
54 * objects that shall be added to graph
56 public ClassGraph(final Object... objects) {
57 for (Object object : objects)
58 addClass(object.getClass());
63 * class that shall be added to graph
65 public ClassDescriptor addClass(final Class<? extends Object> clazz) {
70 final String className = clazz.getName();
72 if (nameToClassMap.containsKey(className))
73 return nameToClassMap.get(className);
75 return new ClassDescriptor(clazz, this);
80 * object that shall be added to graph
82 public ClassDescriptor addObject(final Object object) {
83 return addClass(object.getClass());
88 * path to recursively scan for java source code could be
89 * relative to current project or absolute
91 public void addProject(final String path) {
92 final ProjectScanner projectScanner = new ProjectScanner(new File(path));
93 for (final Clazz clazz : projectScanner.getAllClasses())
95 System.out.println("Class full name: " + clazz.getFullName());
96 final Class c = this.getClass().forName(clazz.getFullName());
98 } catch (final Exception exception) {
99 System.out.println("cannot add class: "
100 + exception.getMessage());
105 * @param resultFileName
106 * file name for the generated graph. Existing file with the same
107 * name will be overwritten.
109 public void generateGraph(final String resultFileName) {
110 generateGraph(resultFileName, false);
114 * @param resultFileName
115 * file name for the generated graph. File extension will be
116 * added automatically. Existing file with the same name will be
120 * if set to <code>true</code> then intermediary GraphViz DOT
124 public void generateGraph(final String resultFileName,
125 final boolean keepDotFile) {
127 final String desktopPath = CommonPathResolver.getDesktopDirectory()
128 .getAbsolutePath() + "/";
130 final String dotFilePath = desktopPath + resultFileName + ".dot";
131 final String imageFilePath = desktopPath + resultFileName + ".png";
133 System.out.println("Dot file path:" + dotFilePath);
136 // write DOT file to disk
137 final PrintWriter out = new PrintWriter(dotFilePath);
141 // execute GraphViz to visualize graph
144 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
145 imageFilePath }).waitFor();
146 } catch (final InterruptedException e) {
152 final File dotFile = new File(dotFilePath);
155 } catch (final IOException e) {
156 System.err.println(e);
160 private String getDot() {
161 final StringBuffer result = new StringBuffer();
163 result.append("digraph Java {\n");
164 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
166 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
168 result.append(entry.getValue().getDot());
170 result.append("}\n");
172 final String resultStr = result.toString();
177 * Hide orphaned class that have no references
179 public void hideOrphanedClasses() {
181 for (final ClassDescriptor classDescriptor : nameToClassMap.values())
182 classDescriptor.hideClassIfNoReferences();
186 public Filter getFilter() {
190 public void setFilter(Filter filter) {
191 this.filter = filter;