From: Svjatoslav Agejenko
-JavaInspect is a Java library that you can embed into your Java -project with a few lines of Maven configuration and then visualize any -part of your Java program structure with few simple JavaInspect API -calls at application runtime. +JavaInspect is a Java library that primarily uses Java reflection to +discover and visualize any part of Java program provided that +classes to be visualised are available in the classpath.
-JavaInspect uses Java reflection to discover class relations and -structure and produces GraphViz dot file that describes your -application. Then launches GraphViz to generate bitmap graph in PNG -format on your Desktop directory. +JavaInspect currently has no GUI, configuration files, embedded +scripting support, direct Maven or Ant integration. The only way to +instuct Javainspect what to do is by using its Java API.
- - --This is simple utility, quickly written. Tested on GNU Linux (can be -relatively simply ported to other operating systems too). So far I -used it for my own needs. There might be bugs and missing -features. Feedback and code contributions are welcome. +To get JavaInspect into same classpath with your projecs I so far came +up with 2 solutions:
--Example visualization of Sixth project: architecture graphs. +After discovering application structure and optionally filtering out +unimportant parts, JavaInspect produces GraphViz dot file that +describes data to be visualized. Then launches GraphViz to generate +bitmap graph in PNG format. By default on your Desktop directory.
-A very simple example: +Note: GraphViz is developed and tested so far only on GNU Linux.
+Graph legend:
@@ -242,12 +252,70 @@ Graph legend:
+Currently the only way to control JavaInspect is by using Java +API. Simple Java based control/configuration code needs to be written +for each project. I usually put such code into directories devoted for +JUnit tests. Because it needs not to be compiled/embedded into final +product or project artifact I'm just willing to visualize. +
+ ++Control code in general does the following: +
+This example demonstrates generating of class graph from hand picked classes. @@ -288,9 +356,9 @@ Result:
Recursively scan current directory for Java source code and attempt to detect class names from there to be added to the graph. @@ -321,10 +389,11 @@ Result:
Declare JavaInspect as dependency:
@@ -362,9 +431,9 @@ Add Maven repository to retrieve artifact from:GraphViz - shall be installed on the computer.
@@ -377,9 +446,9 @@ sudo apt-get install graphviztrue
then intermediary GraphViz DOT
- * file will be kept.
- */
-
- public void generateGraph(final String resultFileName,
- final boolean keepDotFile) {
-
- final String desktopPath = CommonPathResolver.getDesktopDirectory()
- .getAbsolutePath() + "/";
-
- generateGraph(desktopPath, resultFileName, keepDotFile);
- }
-
/**
* @param targetDirectory
* target directory name
@@ -121,11 +97,7 @@ public class ClassGraph {
* file will be kept.
*/
- public void generateGraph(String targetDirectory,
- final String resultFileName, final boolean keepDotFile) {
-
- if (!targetDirectory.endsWith("/"))
- targetDirectory += "/";
+ public void generateGraph(final String resultFileName) {
final String dotFilePath = targetDirectory + resultFileName + ".dot";
final String imageFilePath = targetDirectory + resultFileName + ".png";
@@ -149,8 +121,7 @@ public class ClassGraph {
if (!keepDotFile) {
// delete dot file
- final File dotFile = new File(dotFilePath);
- dotFile.delete();
+ new File(dotFilePath).delete();
}
} catch (final IOException e) {
System.err.println(e);
@@ -210,7 +181,7 @@ public class ClassGraph {
}
- public boolean isClassShown(final String className) {
+ protected boolean isClassShown(final String className) {
for (final String pattern : blacklistClassPatterns)
if (WildCardMatcher.match(className, pattern))
return false;
@@ -225,8 +196,25 @@ public class ClassGraph {
return true;
}
- public void whitelistClassPattern(final String pattern) {
+ public ClassGraph setKeepDotFile(final boolean keepDotFile) {
+ this.keepDotFile = keepDotFile;
+
+ return this;
+ }
+
+ public ClassGraph setTargetDirectory(String directoryPath) {
+ if (!directoryPath.endsWith("/"))
+ directoryPath += "/";
+
+ targetDirectory = directoryPath;
+
+ return this;
+ }
+
+ public ClassGraph whitelistClassPattern(final String pattern) {
whitelistClassPatterns.add(pattern);
+
+ return this;
}
}
diff --git a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java
index 50f9c7e..a3f83ec 100755
--- a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java
+++ b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderDemoClasses.java
@@ -18,7 +18,7 @@ public class RenderDemoClasses {
public static void main(final String[] args) {
new ClassGraph().add(SampleClass.class, SampleClass2.class)
- .generateGraph("example", false);
+ .generateGraph("example");
}
}
diff --git a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java
index 2bfd560..181ac09 100755
--- a/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java
+++ b/src/test/java/eu/svjatoslav/inspector/java/structure/example/RenderJavaInspect.java
@@ -37,25 +37,22 @@ public class RenderJavaInspect {
private static void handpickClassesExample() {
/*
* This example demonstrates generating of class graph from hand picked
- * classes.
+ * classes and visualizing GraphViz itself.
*/
// Create graph
final ClassGraph graph = new ClassGraph();
- // While classes and objects can be immediately passed to ClassGraph
- // constructor as arguments, it is also possible to add then one by one
- // as in the following example.
-
- // Add some object to the graph.
+ // Add some random object to the graph. GraphViz will detect Class from
+ // the object.
graph.add(graph);
- // Add some class to the graph.
+ // Add some random class to the graph.
graph.add(Utils.class);
// Produce bitmap image titled "JavaInspect.png" to the user Desktop
// directory and keep intermediary GraphViz DOT file for reference.
- graph.generateGraph("JavaInspect", true);
+ graph.setKeepDotFile(true).generateGraph("JavaInspect");
}
public static void main(final String[] args) throws FileNotFoundException {