X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fstructure%2FClassGraph.java;h=bf34841ea459d9d89c7bf9e4af4e25a5492563c2;hb=033fa99bac62adc18414cb4cb97c4be02abcb7e2;hp=b8f9c2cd084801557fe6dbedcfad2e559fb5a09d;hpb=79db54fde2069b536c95e9da810efb27f2e4efb5;p=javainspect.git diff --git a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java old mode 100644 new mode 100755 index b8f9c2c..bf34841 --- a/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java +++ b/src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java @@ -1,10 +1,10 @@ /* * JavaInspect - Utility to visualize java software - * Copyright (C) 2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public License - * as published by the Free Software Foundation. + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. */ package eu.svjatoslav.inspector.java.structure; @@ -16,27 +16,49 @@ import java.util.HashMap; import java.util.Map; import eu.svjatoslav.commons.file.CommonPathResolver; +import eu.svjatoslav.inspector.java.methods.Clazz; +import eu.svjatoslav.inspector.java.methods.ProjectScanner; public class ClassGraph { + public static void render(final String graphName, final Class... classes) { + final ClassGraph classGraph = new ClassGraph(classes); + + classGraph.generateGraph(graphName); + } + /** * Maps class fully qualified names to class descriptors. */ Map nameToClassMap = new HashMap(); - public Filter filter = new Filter(); + private Filter filter = new Filter(); public ClassGraph() { } - public ClassGraph(final Class clazz) { - addClass(clazz); + /** + * @param classes + * classes that shall be added to graph + */ + public ClassGraph(final Class... classes) { + for (final Class clazz : classes) + addClass(clazz); } - public ClassGraph(final Object root) { - addClass(root.getClass()); + /** + * @param objects + * objects that shall be added to graph + */ + public ClassGraph(final Object... objects) { + for (final Object object : objects) + addClass(object.getClass()); } + /** + * @param clazz + * class that shall be added to graph + */ public ClassDescriptor addClass(final Class clazz) { if (clazz == null) @@ -50,21 +72,83 @@ public class ClassGraph { return new ClassDescriptor(clazz, this); } + /** + * @param object + * object that shall be added to graph + */ public ClassDescriptor addObject(final Object object) { return addClass(object.getClass()); } - public void generateGraph(final String graphName) { - generateGraph(graphName, false); + /** + * @param path + * path to recursively scan for java source code could be + * relative to current project or absolute + */ + public void addProject(final String path) { + final ProjectScanner projectScanner = new ProjectScanner(new File(path)); + for (final Clazz clazz : projectScanner.getAllClasses()) + try { + System.out.println("Class full name: " + clazz.getFullName()); + final Class c = this.getClass().forName(clazz.getFullName()); + addClass(c); + } catch (final Exception exception) { + System.out.println("cannot add class: " + + exception.getMessage()); + } + } + + /** + * @param resultFileName + * file name for the generated graph. Existing file with the same + * name will be overwritten. + */ + public void generateGraph(final String resultFileName) { + generateGraph(resultFileName, false); } - public void generateGraph(final String graphName, final boolean keepDotFile) { + /** + * @param resultFileName + * file name for the generated graph. File extension will be + * added automatically. Existing file with the same name will be + * overwritten. + * + * @param keepDotFile + * if set to true then intermediary GraphViz DOT + * file will be kept. + */ + + public void generateGraph(final String resultFileName, + final boolean keepDotFile) { final String desktopPath = CommonPathResolver.getDesktopDirectory() .getAbsolutePath() + "/"; - final String dotFilePath = desktopPath + graphName + ".dot"; - final String imageFilePath = desktopPath + graphName + ".png"; + generateGraph(desktopPath, resultFileName, keepDotFile); + } + + /** + * @param targetDirectory + * target directory name + * + * @param resultFileName + * file name for the generated graph. File extension will be + * added automatically. Existing file with the same name will be + * overwritten. + * + * @param keepDotFile + * if set to true then intermediary GraphViz DOT + * file will be kept. + */ + + public void generateGraph(String targetDirectory, + final String resultFileName, final boolean keepDotFile) { + + if (!targetDirectory.endsWith("/")) + targetDirectory += "/"; + + final String dotFilePath = targetDirectory + resultFileName + ".dot"; + final String imageFilePath = targetDirectory + resultFileName + ".png"; System.out.println("Dot file path:" + dotFilePath); @@ -77,8 +161,8 @@ public class ClassGraph { // execute GraphViz to visualize graph try { Runtime.getRuntime() - .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o", - imageFilePath }).waitFor(); + .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o", + imageFilePath }).waitFor(); } catch (final InterruptedException e) { } finally { } @@ -91,6 +175,7 @@ public class ClassGraph { } catch (final IOException e) { System.err.println(e); } + } private String getDot() { @@ -109,4 +194,22 @@ public class ClassGraph { return resultStr; } + public Filter getFilter() { + return filter; + } + + /** + * Hide orphaned class that have no references + */ + public void hideOrphanedClasses() { + + for (final ClassDescriptor classDescriptor : nameToClassMap.values()) + classDescriptor.hideClassIfNoReferences(); + + } + + public void setFilter(final Filter filter) { + this.filter = filter; + } + }