Removed half-baked java source code parser.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / ClassGraph.java
index 1747614..65b8d4d 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * JavaInspect - Utility to visualize java software
- * Copyright (C) 2013-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2013-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 3 of the GNU Lesser General Public License
@@ -9,10 +9,7 @@
 
 package eu.svjatoslav.inspector.java.structure;
 
-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;
+import eu.svjatoslav.commons.string.GlobMatcher;
 
 import java.io.File;
 import java.io.IOException;
@@ -22,8 +19,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static eu.svjatoslav.inspector.java.methods.JavaFile.UTF_8;
 import static java.io.File.separator;
+import static java.lang.System.getProperty;
 
 public class ClassGraph {
 
@@ -36,10 +33,13 @@ public class ClassGraph {
 
     private final List<String> whitelistClassGlobs = new ArrayList<>();
     TargetImageType targetImageType = TargetImageType.SVG;
-    private String targetDirectoryPath = CommonPathResolver.getDesktopDirectory()
-            .getAbsolutePath() + separator;
     private boolean keepDotFile;
 
+    /**
+     * Default to current directory
+     */
+    private File targetDirectory = new File(getProperty("user.dir") + separator);
+
     public ClassGraph() {
     }
 
@@ -63,23 +63,6 @@ public class ClassGraph {
             getOrCreateClassDescriptor(object.getClass());
     }
 
-    /**
-     * @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 = Class.forName(clazz.getFullName());
-                addObject(c);
-            } catch (final Exception exception) {
-                System.out.println("cannot add class: "
-                        + exception.getMessage());
-            }
-    }
-
     public void blacklistClassGlob(final String glob) {
         blacklistClassGlobs.add(glob);
     }
@@ -96,27 +79,30 @@ public class ClassGraph {
 
     public void generateGraph(final String resultFileName) {
 
-        final String dotFilePath = targetDirectoryPath + resultFileName + ".dot";
-        final String imageFilePath = targetDirectoryPath + resultFileName + "." + targetImageType.fileExtension;
+        final File dotFile = new File(targetDirectory, resultFileName + ".dot");
+        final File imageFile = new File(targetDirectory, resultFileName + "." + targetImageType.fileExtension);
 
         try {
             // write DOT file to disk
-            final PrintWriter out = new PrintWriter(dotFilePath, UTF_8);
+            final PrintWriter out = new PrintWriter(dotFile, "UTF-8");
             out.write(getDot());
             out.close();
 
             // execute GraphViz to visualize graph
             try {
                 Runtime.getRuntime()
-                        .exec(new String[]{"dot", "-T" + targetImageType.fileExtension, dotFilePath, "-o",
-                                imageFilePath}).waitFor();
+                        .exec(new String[]{"dot",
+                                "-T" + targetImageType.fileExtension,
+                                dotFile.getAbsolutePath(),
+                                "-o",
+                                imageFile.getAbsolutePath()}).waitFor();
             } catch (final InterruptedException ignored) {
             }
 
             if (!keepDotFile)
                 // delete dot file
-                if (!new File(dotFilePath).delete())
-                    throw new RuntimeException("Cannot delete file: " + dotFilePath);
+                if (!dotFile.delete())
+                    throw new RuntimeException("Cannot delete file: " + dotFile.getAbsolutePath());
 
         } catch (final IOException e) {
             throw new RuntimeException("Unable to generate graph: " + e.getMessage(), e);
@@ -180,12 +166,12 @@ public class ClassGraph {
 
     protected boolean isClassShown(final String className) {
         for (final String pattern : blacklistClassGlobs)
-            if (WildCardMatcher.match(className, pattern))
+            if (GlobMatcher.match(className, pattern))
                 return false;
 
         if (!whitelistClassGlobs.isEmpty()) {
             for (final String pattern : whitelistClassGlobs)
-                if (WildCardMatcher.match(className, pattern))
+                if (GlobMatcher.match(className, pattern))
                     return true;
             return false;
         }
@@ -199,12 +185,8 @@ public class ClassGraph {
         return this;
     }
 
-    public ClassGraph setTargetDirectoryPath(String directoryPath) {
-        if (!directoryPath.endsWith(separator))
-            directoryPath += separator;
-
-        targetDirectoryPath = directoryPath;
-
+    public ClassGraph setTargetDirectory(File targetDirectory) {
+        this.targetDirectory = targetDirectory;
         return this;
     }