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(classes);
27 classGraph.generateGraph(graphName);
31 * Maps class fully qualified names to class descriptors.
33 Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
35 private Filter filter = new Filter();
42 * classes that shall be added to graph
44 public ClassGraph(final Class<? extends Object>... classes) {
45 for (final Class<? extends Object> clazz : classes)
51 * objects that shall be added to graph
53 public ClassGraph(final Object... objects) {
54 for (Object object : objects)
55 addClass(object.getClass());
60 * class that shall be added to graph
62 public ClassDescriptor addClass(final Class<? extends Object> clazz) {
67 final String className = clazz.getName();
69 if (nameToClassMap.containsKey(className))
70 return nameToClassMap.get(className);
72 return new ClassDescriptor(clazz, this);
77 * object that shall be added to graph
79 public ClassDescriptor addObject(final Object object) {
80 return addClass(object.getClass());
85 * path to recursively scan for java source code could be
86 * relative to current project or absolute
88 public void addProject(final String path) {
89 final ProjectScanner projectScanner = new ProjectScanner(new File(path));
90 for (final Clazz clazz : projectScanner.getAllClasses())
92 System.out.println("Class full name: " + clazz.getFullName());
93 final Class c = this.getClass().forName(clazz.getFullName());
95 } catch (final Exception exception) {
96 System.out.println("cannot add class: "
97 + exception.getMessage());
102 * @param resultFileName
103 * file name for the generated graph. Existing file with the same
104 * name will be overwritten.
106 public void generateGraph(final String resultFileName) {
107 generateGraph(resultFileName, false);
111 * @param resultFileName
112 * file name for the generated graph. File extension will be
113 * added automatically. Existing file with the same name will be
117 * if set to <code>true</code> then intermediary GraphViz DOT
121 public void generateGraph(final String resultFileName,
122 final boolean keepDotFile) {
124 final String desktopPath = CommonPathResolver.getDesktopDirectory()
125 .getAbsolutePath() + "/";
127 final String dotFilePath = desktopPath + resultFileName + ".dot";
128 final String imageFilePath = desktopPath + resultFileName + ".png";
130 System.out.println("Dot file path:" + dotFilePath);
133 // write DOT file to disk
134 final PrintWriter out = new PrintWriter(dotFilePath);
138 // execute GraphViz to visualize graph
141 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
142 imageFilePath }).waitFor();
143 } catch (final InterruptedException e) {
149 final File dotFile = new File(dotFilePath);
152 } catch (final IOException e) {
153 System.err.println(e);
157 private String getDot() {
158 final StringBuffer result = new StringBuffer();
160 result.append("digraph Java {\n");
161 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
163 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
165 result.append(entry.getValue().getDot());
167 result.append("}\n");
169 final String resultStr = result.toString();
174 * Hide orphaned class that have no references
176 public void hideOrphanedClasses() {
178 for (final ClassDescriptor classDescriptor : nameToClassMap.values())
179 classDescriptor.hideClassIfNoReferences();
183 public Filter getFilter() {
187 public void setFilter(Filter filter) {
188 this.filter = filter;