X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finspector%2Fjava%2Fstructure%2FClassGraph.java;h=cee1ee41f125069db0c48f597313f903d7043a41;hb=ffba53aac03e4d107545116ddcfff0c965bd5970;hp=bda0882713bb0588d2f1835822fb1b9384ffaca5;hpb=d717e90f4c46e26f9f54ba5638aade6688527bf4;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 bda0882..cee1ee4 --- 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-2015, 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; @@ -12,57 +12,123 @@ package eu.svjatoslav.inspector.java.structure; import java.io.File; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import eu.svjatoslav.commons.file.CommonPathResolver; +import eu.svjatoslav.commons.string.WildCardMatcher; +import eu.svjatoslav.inspector.java.methods.Clazz; +import eu.svjatoslav.inspector.java.methods.ProjectScanner; public class ClassGraph { /** * Maps class fully qualified names to class descriptors. */ - Map nameToClassMap = new HashMap(); + private final Map fullyQualifiedNameToClassMap = new HashMap(); - public ClassGraph() { - } + private final List blacklistClassPatterns = new ArrayList(); - public ClassGraph(final Class clazz) { - addClass(clazz); - } + private final List whitelistClassPatterns = new ArrayList(); - public ClassGraph(final Object root) { - addClass(root.getClass()); + public ClassGraph() { } - public ClassDescriptor addClass(final Class clazz) { + /** + * @param objects + * objects that shall be added to graph + */ + public ClassGraph add(final Object... objects) { - if (clazz == null) - return null; + if (objects != null) + for (final Object object : objects) + addObject(object); - final String className = clazz.getName(); + return this; + } - if (nameToClassMap.containsKey(className)) - return nameToClassMap.get(className); + private void addObject(final Object object) { + if (object instanceof Class) + getOrCreateClassDescriptor((Class) object); + else + getOrCreateClassDescriptor(object.getClass()); + } - return new ClassDescriptor(clazz, this); + /** + * @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()); + addObject(c); + } catch (final Exception exception) { + System.out.println("cannot add class: " + + exception.getMessage()); + } } - public ClassDescriptor addObject(final Object object) { - return addClass(object.getClass()); + public void blacklistClassPattern(final String pattern) { + blacklistClassPatterns.add(pattern); } - public void generateGraph(final String graphName) { - generateGraph(graphName, false); + /** + * @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); @@ -75,8 +141,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 { } @@ -89,6 +155,7 @@ public class ClassGraph { } catch (final IOException e) { System.err.println(e); } + } private String getDot() { @@ -97,7 +164,7 @@ public class ClassGraph { result.append("digraph Java {\n"); result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n"); - for (final Map.Entry entry : nameToClassMap + for (final Map.Entry entry : fullyQualifiedNameToClassMap .entrySet()) result.append(entry.getValue().getDot()); @@ -107,4 +174,59 @@ public class ClassGraph { return resultStr; } + /** + * @param clazz + * class that shall be added to graph + */ + protected ClassDescriptor getOrCreateClassDescriptor(final Class clazz) { + + if (clazz == null) + return null; + + final String classFullyQualifiedName = clazz.getName(); + + // reuse existing instance if possible + if (fullyQualifiedNameToClassMap.containsKey(classFullyQualifiedName)) + return fullyQualifiedNameToClassMap.get(classFullyQualifiedName); + + // create new class descriptor + final ClassDescriptor newClassDescriptor = new ClassDescriptor(this); + fullyQualifiedNameToClassMap.put(classFullyQualifiedName, + newClassDescriptor); + + newClassDescriptor.analyzeClass(clazz); + + return newClassDescriptor; + } + + /** + * Hide orphaned class that have no references + */ + public void hideOrphanedClasses() { + + for (final ClassDescriptor classDescriptor : fullyQualifiedNameToClassMap + .values()) + classDescriptor.hideClassIfNoReferences(); + + } + + public boolean isClassShown(final String className) { + for (final String pattern : blacklistClassPatterns) + if (WildCardMatcher.match(className, pattern)) + return false; + + if (!whitelistClassPatterns.isEmpty()) { + for (final String pattern : whitelistClassPatterns) + if (WildCardMatcher.match(className, pattern)) + return true; + return false; + } + + return true; + } + + public void whitelistClassPattern(final String pattern) { + whitelistClassPatterns.add(pattern); + } + }