properly render fields of arrays of primitive types
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / ClassGraph.java
old mode 100644 (file)
new mode 100755 (executable)
index 036d583..46cbc69
@@ -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;
@@ -30,7 +30,7 @@ public class ClassGraph {
        /**
         * Maps class fully qualified names to class descriptors.
         */
-       Map<String, ClassDescriptor> nameToClassMap = new HashMap<String, ClassDescriptor>();
+       private final Map<String, ClassDescriptor> fullyQualifiedNameToClassMap = new HashMap<String, ClassDescriptor>();
 
        private Filter filter = new Filter();
 
@@ -51,7 +51,7 @@ public class ClassGraph {
         *            objects that shall be added to graph
         */
        public ClassGraph(final Object... objects) {
-               for (Object object : objects)
+               for (final Object object : objects)
                        addClass(object.getClass());
        }
 
@@ -66,8 +66,8 @@ public class ClassGraph {
 
                final String className = clazz.getName();
 
-               if (nameToClassMap.containsKey(className))
-                       return nameToClassMap.get(className);
+               if (fullyQualifiedNameToClassMap.containsKey(className))
+                       return fullyQualifiedNameToClassMap.get(className);
 
                return new ClassDescriptor(clazz, this);
        }
@@ -112,7 +112,7 @@ public class ClassGraph {
         *            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 <code>true</code> then intermediary GraphViz DOT
         *            file will be kept.
@@ -124,8 +124,31 @@ public class ClassGraph {
                final String desktopPath = CommonPathResolver.getDesktopDirectory()
                                .getAbsolutePath() + "/";
 
-               final String dotFilePath = desktopPath + resultFileName + ".dot";
-               final String imageFilePath = desktopPath + resultFileName + ".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 <code>true</code> 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);
 
@@ -138,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 {
                        }
@@ -152,6 +175,7 @@ public class ClassGraph {
                } catch (final IOException e) {
                        System.err.println(e);
                }
+
        }
 
        private String getDot() {
@@ -160,7 +184,7 @@ public class ClassGraph {
                result.append("digraph Java {\n");
                result.append("graph [rankdir=LR, overlap = false, concentrate=true];\n");
 
-               for (final Map.Entry<String, ClassDescriptor> entry : nameToClassMap
+               for (final Map.Entry<String, ClassDescriptor> entry : fullyQualifiedNameToClassMap
                                .entrySet())
                        result.append(entry.getValue().getDot());
 
@@ -170,21 +194,28 @@ 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())
+               for (final ClassDescriptor classDescriptor : fullyQualifiedNameToClassMap
+                               .values())
                        classDescriptor.hideClassIfNoReferences();
 
        }
 
-       public Filter getFilter() {
-               return filter;
+       public void registerClass(final String classFullyQualifiedName,
+                       final ClassDescriptor classDescriptor) {
+               fullyQualifiedNameToClassMap.put(classFullyQualifiedName,
+                               classDescriptor);
        }
 
-       public void setFilter(Filter filter) {
+       public void setFilter(final Filter filter) {
                this.filter = filter;
        }