From: Svjatoslav Agejenko Date: Sun, 31 Dec 2017 01:28:38 +0000 (+0200) Subject: Removed files that came from forked repo and not needed in this one. X-Git-Tag: javainspect-1.7~21 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=javainspect.git;a=commitdiff_plain;h=375d6ff0afd27c71927296cdb9c971a5d0c85341 Removed files that came from forked repo and not needed in this one. Moved commandline Main to commandline dedicated package. --- diff --git a/README.md b/README.md deleted file mode 100644 index 5ffec69..0000000 --- a/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# JavaInspect - -Visualize java class relationships as Graphviz. - -Forked from the original work from [Svjatoslav Agejenko](http://www.svjatoslav.eu/). -This repository only provides minor improvements. - -Original [git repository](http://www2.svjatoslav.eu/repositories/javainspect.git) - -Original [documentation](http://www2.svjatoslav.eu/gitbrowse/javainspect/doc/index.html) - -Minor enhancements: - -- Provides java main to run as CLI tool. -- Produces single executable with dependencies. -- Default to render SVG. -- Default to output to `user.dir` (i.e. working directory). - -### Building - -1. Install [Maven](http://maven.apache.org/). -2. Package JAR: - - `mvn clean package` - -### Running - -1. You will need to add the classes would like to visualize to your classpath. -2. Provide a package glob pattern to identify the classes you want to visualize. -3. Name your output. - -```bash -java \ - -cp .:./example/target/myproject-0.0.jar:./target/javainspect-1.6-SNAPSHOT.jar \ - eu.svjatoslav.inspector.java.methods.Main \ - ./example/src/main/java/ com.myproject.* myproject -``` - -Here we want to visualize _com.myproject.*_ classes found in -*./example/src/main/java/*. The resulting *dot* and *svg* -file will be prefixed with *myproject*. - -### Output example - -![MyProject output](https://github.com/abargnesi/javainspect/raw/master/myproject.png) - -The [dot](https://github.com/abargnesi/javainspect/raw/master/myproject.dot) file is also saved. diff --git a/javainspect.iml b/javainspect.iml index 854d268..4edf0f0 100644 --- a/javainspect.iml +++ b/javainspect.iml @@ -1,13 +1,12 @@ - - - - - - - + + + + + + diff --git a/myproject.dot b/myproject.dot deleted file mode 100644 index 068b68c..0000000 --- a/myproject.dot +++ /dev/null @@ -1,34 +0,0 @@ -digraph Java { -graph [rankdir=LR, overlap = false, concentrate=true]; - -// Class: com.myproject.NumberTranslator - class_com_myproject_NumberTranslator[label=< - - // class descriptor header - - - // methods: - // translate - -
com.myproject
NumberTranslator
Stringtranslate
>, shape="none"]; - - // method references to other classes - - // interfaces implemented by class: com.myproject.NumberTranslator - class_com_myproject_Behavior -> class_com_myproject_NumberTranslator[style="dotted", color="antiquewhite4", penwidth=10, dir="forward"]; - -// Class: com.myproject.Behavior - class_com_myproject_Behavior[label=< - - // class descriptor header - - - // methods: - // serialize - - // translate - -
com.myproject
Behavior
Stringserialize
Stringtranslate
>, shape="none"]; - - // method references to other classes -} diff --git a/myproject.png b/myproject.png deleted file mode 100644 index 268779f..0000000 Binary files a/myproject.png and /dev/null differ diff --git a/myproject.svg b/myproject.svg deleted file mode 100644 index afec2ba..0000000 --- a/myproject.svg +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - -Java - - -class_com_myproject_NumberTranslator - -com.myproject -NumberTranslator - -String - -translate - - - -class_com_myproject_Behavior - - -com.myproject -Behavior - -String - -serialize - -String - -translate - - - -class_com_myproject_Behavior->class_com_myproject_NumberTranslator - - - - - diff --git a/pom.xml b/pom.xml index 0a9bf25..755138e 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ - eu.svjatoslav.inspector.java.Main + eu.svjatoslav.inspector.java.commandline.Main diff --git a/src/main/java/eu/svjatoslav/inspector/java/Main.java b/src/main/java/eu/svjatoslav/inspector/java/Main.java deleted file mode 100644 index 9b706d3..0000000 --- a/src/main/java/eu/svjatoslav/inspector/java/Main.java +++ /dev/null @@ -1,126 +0,0 @@ -package eu.svjatoslav.inspector.java; - -import eu.svjatoslav.inspector.java.commandline.CommandlineConfiguration; -import eu.svjatoslav.inspector.java.structure.ClassGraph; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.List; -import java.util.jar.JarEntry; -import java.util.jar.JarInputStream; - -import static java.io.File.separator; -import static java.lang.System.getProperty; - -/** - * This class acts as a commandline interface for JavaInspect. - */ -public class Main { - public static void main(String[] args) throws IOException, ClassNotFoundException { - CommandlineConfiguration configuration = new CommandlineConfiguration(args); - if (!configuration.configurationOk) - System.exit(1); - - getClassGraph(configuration).generateGraph(configuration.graphName.getValue()); - - if (configuration.isDebug()) - System.out.println("Graph ready."); - } - - private static ClassGraph getClassGraph(CommandlineConfiguration configuration) throws IOException, ClassNotFoundException { - List jarFiles = configuration.jarFiles.getValue(); - - URLClassLoader classLoader = new URLClassLoader( - getFileUrls(jarFiles), - configuration.getClass().getClassLoader()); - - ClassGraph classGraph = new ClassGraph(); - - classGraph.setTargetDirectory(getTargetDirectory(configuration)); - - if (configuration.targetImageType.isSpecified()) - classGraph.setTargetImageType(configuration.targetImageType.getValue()); - - classGraph.setKeepDotFile(configuration.keepDotFile.getValue()); - - for (File jarFile : jarFiles) - addJarToGraph(jarFile, classLoader, classGraph, configuration); - - configuration.blacklistGlob.getValue().forEach(classGraph::blacklistClassGlob); - configuration.whitelistGlob.getValue().forEach(classGraph::whitelistClassGlob); - - if (configuration.hideOrphanedClasses.getValue()) - classGraph.hideOrphanedClasses(); - - return classGraph; - } - - private static File getTargetDirectory(CommandlineConfiguration configuration) { - if (configuration.targetDirectory.isSpecified()) - return configuration.targetDirectory.getValue(); - - // default to current directory - return new File(getProperty("user.dir") + separator); - } - - private static URL[] getFileUrls(List jarFiles) { - List urls = new ArrayList<>(); - jarFiles.forEach((File file) -> { - try { - urls.add(file.toURI().toURL()); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - }); - - return urls.toArray(new URL[urls.size()]); - } - - private static void addJarToGraph( - File jarFile, URLClassLoader classLoader, ClassGraph classGraph, CommandlineConfiguration configuration) - throws IOException, ClassNotFoundException { - - for (String className : getClassNamesFromJar(jarFile)) { - if (configuration.isDebug()) - System.out.println("Adding class to graph: " + className); - - classGraph.add(loadClassByName(classLoader, className)); - } - } - - private static Class loadClassByName(URLClassLoader classLoader, String className) throws ClassNotFoundException { - return Class.forName(className, true, classLoader); - } - - public static List getClassNamesFromJar(File jarFile) throws IOException { - List result = new ArrayList<>(); - try ( - JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile)) - ) { - while (true) { - JarEntry jarEntry = jarInputStream.getNextJarEntry(); - if (jarEntry == null) - break; - - if (isClassFile(jarEntry)) - result.add(getClassNameFromFileName(jarEntry)); - } - - return result; - } - } - - private static boolean isClassFile(JarEntry jarEntry) { - return jarEntry.getName().endsWith(".class"); - } - - private static String getClassNameFromFileName(JarEntry jarEntry) { - String result = jarEntry.getName().replaceAll("/", "\\."); - return result.substring(0, result.lastIndexOf('.')); - } -} diff --git a/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java b/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java new file mode 100644 index 0000000..8fcf5f6 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/commandline/Main.java @@ -0,0 +1,126 @@ +package eu.svjatoslav.inspector.java.commandline; + +import eu.svjatoslav.inspector.java.commandline.CommandlineConfiguration; +import eu.svjatoslav.inspector.java.structure.ClassGraph; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; + +import static java.io.File.separator; +import static java.lang.System.getProperty; + +/** + * This class acts as a commandline interface for JavaInspect. + */ +public class Main { + public static void main(String[] args) throws IOException, ClassNotFoundException { + CommandlineConfiguration configuration = new CommandlineConfiguration(args); + if (!configuration.configurationOk) + System.exit(1); + + getClassGraph(configuration).generateGraph(configuration.graphName.getValue()); + + if (configuration.isDebug()) + System.out.println("Graph ready."); + } + + private static ClassGraph getClassGraph(CommandlineConfiguration configuration) throws IOException, ClassNotFoundException { + List jarFiles = configuration.jarFiles.getValue(); + + URLClassLoader classLoader = new URLClassLoader( + getFileUrls(jarFiles), + configuration.getClass().getClassLoader()); + + ClassGraph classGraph = new ClassGraph(); + + classGraph.setTargetDirectory(getTargetDirectory(configuration)); + + if (configuration.targetImageType.isSpecified()) + classGraph.setTargetImageType(configuration.targetImageType.getValue()); + + classGraph.setKeepDotFile(configuration.keepDotFile.getValue()); + + for (File jarFile : jarFiles) + addJarToGraph(jarFile, classLoader, classGraph, configuration); + + configuration.blacklistGlob.getValue().forEach(classGraph::blacklistClassGlob); + configuration.whitelistGlob.getValue().forEach(classGraph::whitelistClassGlob); + + if (configuration.hideOrphanedClasses.getValue()) + classGraph.hideOrphanedClasses(); + + return classGraph; + } + + private static File getTargetDirectory(CommandlineConfiguration configuration) { + if (configuration.targetDirectory.isSpecified()) + return configuration.targetDirectory.getValue(); + + // default to current directory + return new File(getProperty("user.dir") + separator); + } + + private static URL[] getFileUrls(List jarFiles) { + List urls = new ArrayList<>(); + jarFiles.forEach((File file) -> { + try { + urls.add(file.toURI().toURL()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + }); + + return urls.toArray(new URL[urls.size()]); + } + + private static void addJarToGraph( + File jarFile, URLClassLoader classLoader, ClassGraph classGraph, CommandlineConfiguration configuration) + throws IOException, ClassNotFoundException { + + for (String className : getClassNamesFromJar(jarFile)) { + if (configuration.isDebug()) + System.out.println("Adding class to graph: " + className); + + classGraph.add(loadClassByName(classLoader, className)); + } + } + + private static Class loadClassByName(URLClassLoader classLoader, String className) throws ClassNotFoundException { + return Class.forName(className, true, classLoader); + } + + public static List getClassNamesFromJar(File jarFile) throws IOException { + List result = new ArrayList<>(); + try ( + JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile)) + ) { + while (true) { + JarEntry jarEntry = jarInputStream.getNextJarEntry(); + if (jarEntry == null) + break; + + if (isClassFile(jarEntry)) + result.add(getClassNameFromFileName(jarEntry)); + } + + return result; + } + } + + private static boolean isClassFile(JarEntry jarEntry) { + return jarEntry.getName().endsWith(".class"); + } + + private static String getClassNameFromFileName(JarEntry jarEntry) { + String result = jarEntry.getName().replaceAll("/", "\\."); + return result.substring(0, result.lastIndexOf('.')); + } +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/commandline/package-info.java b/src/main/java/eu/svjatoslav/inspector/java/commandline/package-info.java new file mode 100644 index 0000000..c122358 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/commandline/package-info.java @@ -0,0 +1,4 @@ +package eu.svjatoslav.inspector.java.commandline; +/** + * This package contains JavaInspect commandline interface. + */ \ No newline at end of file