Merge branch 'master' of ssh://svjatoslav.eu/home/git/repositories/javainspect
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 24 Dec 2017 11:51:49 +0000 (13:51 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 24 Dec 2017 11:51:49 +0000 (13:51 +0200)
.gitignore
README.md [new file with mode: 0644]
example/pom.xml [new file with mode: 0644]
example/src/main/java/com/myproject/Behavior.java [new file with mode: 0644]
example/src/main/java/com/myproject/NumberTranslator.java [new file with mode: 0644]
myproject.dot [new file with mode: 0644]
myproject.png [new file with mode: 0644]
myproject.svg [new file with mode: 0644]
pom.xml
src/main/java/eu/svjatoslav/inspector/java/methods/Main.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/inspector/java/structure/ClassGraph.java

index d8ed27d..a47ec35 100755 (executable)
@@ -3,3 +3,5 @@
 /.classpath
 /target/
 /.idea/
+dependency-reduced-pom.xml
+/example/target
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..5ffec69
--- /dev/null
+++ b/README.md
@@ -0,0 +1,47 @@
+# 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/example/pom.xml b/example/pom.xml
new file mode 100644 (file)
index 0000000..e3d0312
--- /dev/null
@@ -0,0 +1,24 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.myproject</groupId>
+    <artifactId>myproject</artifactId>
+    <version>0.0</version>
+    <packaging>jar</packaging>
+    <name>MyProject</name>
+    <description>MyProject</description>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+               </plugins>
+       </build>
+</project>
diff --git a/example/src/main/java/com/myproject/Behavior.java b/example/src/main/java/com/myproject/Behavior.java
new file mode 100644 (file)
index 0000000..d35ce27
--- /dev/null
@@ -0,0 +1,10 @@
+package com.myproject;
+
+public interface Behavior {
+
+    String translate(Object obj);
+
+    default String serialize(Object obj) {
+        return obj.toString();
+    }
+}
diff --git a/example/src/main/java/com/myproject/NumberTranslator.java b/example/src/main/java/com/myproject/NumberTranslator.java
new file mode 100644 (file)
index 0000000..b0cb6c4
--- /dev/null
@@ -0,0 +1,8 @@
+package com.myproject;
+
+public class NumberTranslator implements Behavior {
+
+    public String translate(Object obj) {
+        return serialize(obj);
+    }
+}
diff --git a/myproject.dot b/myproject.dot
new file mode 100644 (file)
index 0000000..068b68c
--- /dev/null
@@ -0,0 +1,34 @@
+digraph Java {
+graph [rankdir=LR, overlap = false, concentrate=true];
+
+// Class: com.myproject.NumberTranslator
+    class_com_myproject_NumberTranslator[label=<<TABLE  BORDER="1" CELLBORDER="1" CELLSPACING="0">
+
+    // class descriptor header
+    <TR><TD colspan="2" PORT="f0"><FONT POINT-SIZE="8.0" >com.myproject</FONT><br/><FONT POINT-SIZE="25.0"><B>NumberTranslator</B></FONT></TD></TR>
+
+    // methods:
+        // translate
+        <TR><td ALIGN="right"><FONT POINT-SIZE="8.0">String</FONT></td><TD PORT="translate" ALIGN="left"><FONT COLOR ="red" POINT-SIZE="11.0">translate</FONT></TD></TR>
+    </TABLE>>, 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=<<TABLE bgcolor="darkslategray1" BORDER="1" CELLBORDER="1" CELLSPACING="0">
+
+    // class descriptor header
+    <TR><TD colspan="2" PORT="f0"><FONT POINT-SIZE="8.0" >com.myproject</FONT><br/><FONT POINT-SIZE="25.0"><B>Behavior</B></FONT></TD></TR>
+
+    // methods:
+        // serialize
+        <TR><td ALIGN="right"><FONT POINT-SIZE="8.0">String</FONT></td><TD PORT="serialize" ALIGN="left"><FONT COLOR ="red" POINT-SIZE="11.0">serialize</FONT></TD></TR>
+        // translate
+        <TR><td ALIGN="right"><FONT POINT-SIZE="8.0">String</FONT></td><TD PORT="translate" ALIGN="left"><FONT COLOR ="red" POINT-SIZE="11.0">translate</FONT></TD></TR>
+    </TABLE>>, shape="none"];
+
+    // method references to other classes
+}
diff --git a/myproject.png b/myproject.png
new file mode 100644 (file)
index 0000000..268779f
Binary files /dev/null and b/myproject.png differ
diff --git a/myproject.svg b/myproject.svg
new file mode 100644 (file)
index 0000000..afec2ba
--- /dev/null
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.38.0 (20140413.2041)
+ -->
+<!-- Title: Java Pages: 1 -->
+<svg width="473pt" height="93pt"
+ viewBox="0.00 0.00 473.00 93.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 89)">
+<title>Java</title>
+<polygon fill="white" stroke="none" points="-4,4 -4,-89 469,-89 469,4 -4,4"/>
+<!-- class_com_myproject_NumberTranslator -->
+<g id="node1" class="node"><title>class_com_myproject_NumberTranslator</title>
+<polygon fill="none" stroke="black" points="193.5,-31.5 193.5,-70.5 456.5,-70.5 456.5,-31.5 193.5,-31.5"/>
+<text text-anchor="start" x="294" y="-62.1" font-family="Times,serif" font-size="8.00">com.myproject</text>
+<text text-anchor="start" x="196.5" y="-40.5" font-family="Times,serif" font-weight="bold" font-size="25.00">NumberTranslator</text>
+<polygon fill="none" stroke="black" points="193.5,-13.5 193.5,-31.5 312.5,-31.5 312.5,-13.5 193.5,-13.5"/>
+<text text-anchor="start" x="282.5" y="-20.6" font-family="Times,serif" font-size="8.00">String</text>
+<polygon fill="none" stroke="black" points="312.5,-13.5 312.5,-31.5 456.5,-31.5 456.5,-13.5 312.5,-13.5"/>
+<text text-anchor="start" x="315.5" y="-19.7" font-family="Times,serif" font-size="11.00" fill="red">translate</text>
+<polygon fill="none" stroke="black" points="192,-13 192,-72 457,-72 457,-13 192,-13"/>
+</g>
+<!-- class_com_myproject_Behavior -->
+<g id="node2" class="node"><title>class_com_myproject_Behavior</title>
+<polygon fill="#97ffff" stroke="none" points="8,-4 8,-81 140,-81 140,-4 8,-4"/>
+<polygon fill="none" stroke="black" points="9,-40.5 9,-79.5 139,-79.5 139,-40.5 9,-40.5"/>
+<text text-anchor="start" x="43" y="-71.1" font-family="Times,serif" font-size="8.00">com.myproject</text>
+<text text-anchor="start" x="12" y="-49.5" font-family="Times,serif" font-weight="bold" font-size="25.00">Behavior</text>
+<polygon fill="none" stroke="black" points="9,-22.5 9,-40.5 61,-40.5 61,-22.5 9,-22.5"/>
+<text text-anchor="start" x="31" y="-29.6" font-family="Times,serif" font-size="8.00">String</text>
+<polygon fill="none" stroke="black" points="61,-22.5 61,-40.5 139,-40.5 139,-22.5 61,-22.5"/>
+<text text-anchor="start" x="64" y="-28.7" font-family="Times,serif" font-size="11.00" fill="red">serialize</text>
+<polygon fill="none" stroke="black" points="9,-4.5 9,-22.5 61,-22.5 61,-4.5 9,-4.5"/>
+<text text-anchor="start" x="31" y="-11.6" font-family="Times,serif" font-size="8.00">String</text>
+<polygon fill="none" stroke="black" points="61,-4.5 61,-22.5 139,-22.5 139,-4.5 61,-4.5"/>
+<text text-anchor="start" x="64" y="-10.7" font-family="Times,serif" font-size="11.00" fill="red">translate</text>
+<polygon fill="none" stroke="black" points="8,-4 8,-81 140,-81 140,-4 8,-4"/>
+</g>
+<!-- class_com_myproject_Behavior&#45;&gt;class_com_myproject_NumberTranslator -->
+<g id="edge1" class="edge"><title>class_com_myproject_Behavior&#45;&gt;class_com_myproject_NumberTranslator</title>
+<path fill="none" stroke="#8b8378" stroke-width="10" stroke-dasharray="1,5" d="M148.214,-42.5C156.343,-42.5 164.825,-42.5 173.482,-42.5"/>
+<polygon fill="#8b8378" stroke="#8b8378" stroke-width="10" points="173.713,-51.2501 183.713,-42.5 173.713,-33.7501 173.713,-51.2501"/>
+</g>
+</g>
+</svg>
diff --git a/pom.xml b/pom.xml
index 0026b6e..fbdb534 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                 </dependencies>
             </plugin>
 
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>2.4.3</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <transformers>
+                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>eu.svjatoslav.inspector.java.methods.Main</mainClass>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
 
         <extensions>
         <developerConnection>scm:git:ssh://git@svjatoslav.eu/home/git/repositories/javainspect.git</developerConnection>
     </scm>
 
-</project>
\ No newline at end of file
+</project>
diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Main.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Main.java
new file mode 100644 (file)
index 0000000..7450c99
--- /dev/null
@@ -0,0 +1,30 @@
+package eu.svjatoslav.inspector.java.methods;
+
+import eu.svjatoslav.inspector.java.structure.ClassGraph;
+
+import java.io.File;
+
+import static java.lang.System.getProperty;
+
+public class Main {
+       public static void main(String[] args) {
+               if (args.length == 0) {
+                       System.err.println("usage: javainspect [PROJECT_DIR] [PACKAGE_GLOB] [GRAPH_NAME]");
+                       System.exit(1);
+               }
+
+               String projectDir  = args[0];
+               String packageGlob = args[1];
+               String graphName   = args[2];
+
+               ClassGraph cg = new ClassGraph();
+               cg.setTargetDirectory(getProperty("user.dir") + File.separator);
+
+               cg.addProject(projectDir);
+               cg.whitelistClassPattern(packageGlob);
+               cg.setKeepDotFile(true);
+               cg.generateGraph(graphName);
+
+               System.exit(0);
+       }
+}
index 1488b1c..c6b6cbf 100755 (executable)
@@ -93,7 +93,7 @@ public class ClassGraph {
     public void generateGraph(final String resultFileName) {
 
         final String dotFilePath = targetDirectory + resultFileName + ".dot";
-        final String imageFilePath = targetDirectory + resultFileName + ".png";
+        final String imageFilePath = targetDirectory + resultFileName + ".svg";
 
         System.out.println("Dot file path:" + dotFilePath);
 
@@ -106,7 +106,7 @@ public class ClassGraph {
             // execute GraphViz to visualize graph
             try {
                 Runtime.getRuntime()
-                        .exec(new String[]{"dot", "-Tpng", dotFilePath, "-o",
+                        .exec(new String[]{"dot", "-Tsvg", dotFilePath, "-o",
                                 imageFilePath}).waitFor();
             } catch (final InterruptedException ignored) {
             }