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 public Filter filter = new Filter();
43 public ClassGraph(final Class<? extends Object> clazz) {
47 public ClassGraph(final Object root) {
48 addClass(root.getClass());
51 public ClassDescriptor addClass(final Class<? extends Object> clazz) {
56 final String className = clazz.getName();
58 if (nameToClassMap.containsKey(className))
59 return nameToClassMap.get(className);
61 return new ClassDescriptor(clazz, this);
64 public ClassDescriptor addObject(final Object object) {
65 return addClass(object.getClass());
68 public void addProject(final String path) {
69 final ProjectScanner projectScanner = new ProjectScanner(new File(path));
70 for (final Clazz clazz : projectScanner.getAllClasses())
72 System.out.println("Class full name: " + clazz.getFullName());
73 final Class c = this.getClass().forName(clazz.getFullName());
75 } catch (final Exception exception) {
76 System.out.println("cannot add class: "
77 + exception.getMessage());
81 public void generateGraph(final String graphName) {
82 generateGraph(graphName, false);
85 public void generateGraph(final String graphName, final boolean keepDotFile) {
87 final String desktopPath = CommonPathResolver.getDesktopDirectory()
88 .getAbsolutePath() + "/";
90 final String dotFilePath = desktopPath + graphName + ".dot";
91 final String imageFilePath = desktopPath + graphName + ".png";
93 System.out.println("Dot file path:" + dotFilePath);
96 // write DOT file to disk
97 final PrintWriter out = new PrintWriter(dotFilePath);
101 // execute GraphViz to visualize graph
104 .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
105 imageFilePath }).waitFor();
106 } catch (final InterruptedException e) {
112 final File dotFile = new File(dotFilePath);
115 } catch (final IOException e) {
116 System.err.println(e);
120 private String getDot() {
121 final StringBuffer result = new StringBuffer();
123 result.append("digraph Java {\n");
124 result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
126 for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
128 result.append(entry.getValue().getDot());
130 result.append("}\n");
132 final String resultStr = result.toString();
136 public void hideClassesWithoutReferences() {
138 for (final ClassDescriptor classDescriptor : nameToClassMap.values())
139 classDescriptor.hideClassIfNoReferences();