further documentation updates
[javainspect.git] / doc / index.org
1 #+TITLE: JavaInspect - Utility to visualize java software
2
3 -----
4 - [[http://www2.svjatoslav.eu/gitweb/?p=javainspect.git;a=snapshot;h=HEAD;sf=tgz][download latest snapshot]]
5
6 - This program is free software; you can redistribute it and/or modify
7   it under the terms of version 3 of the [[https://www.gnu.org/licenses/lgpl.html][GNU Lesser General Public
8   License]] or later as published by the Free Software Foundation.
9
10 - Program author:
11   - Svjatoslav Agejenko
12   - Homepage: http://svjatoslav.eu
13   - Email: mailto://svjatoslav@svjatoslav.eu
14
15 - [[http://svjatoslav.eu/programs.jsp][other applications hosted at svjatoslav.eu]]
16
17 * General
18 Goal: simplify/speed up understanding the computer program code by
19 automatically visualizing its structure.
20
21 JavaInspect is a Java library that primarily uses Java reflection to
22 discover and visualize any part of Java program provided that
23 classes to be visualised are available in the classpath.
24
25 JavaInspect currently has no GUI, configuration files, embedded
26 scripting support, direct Maven or Ant integration. The only way to
27 instuct Javainspect what to do is by using its Java API.
28
29 To get JavaInspect into same classpath with your projecs I so far came
30 up with 2 solutions:
31
32 1. Add JavaInspect library in your project as a dependency.
33 2. Create new Java project for the purpose visualizing your other
34    projects and include JavaInspect and your projecs binary artifacts
35    (Jar's) into new project classpath. Built binary Jar's (with no
36    source code) are sufficient because JavaInspect operates via
37    reflection.
38
39 After discovering application structure and optionally filtering out
40 unimportant parts, JavaInspect produces GraphViz dot file that
41 describes data to be visualized. Then launches GraphViz to generate
42 bitmap graph in PNG format. By default on your Desktop directory.
43
44 Note: GraphViz is developed and tested so far only on GNU Linux.
45
46 * Example graphs
47 + A very simple example:
48
49     [[file:example.png][file:example.resized.png]]
50
51     Graph legend:
52
53     file:legend.png
54
55 + Example visualization of [[http://www2.svjatoslav.eu/gitbrowse/sixth/doc/][Sixth]] project: [[http://www2.svjatoslav.eu/projects/sixth/codegraphs/][architecture graphs]].
56
57 * Usage
58 Currently the only way to control JavaInspect is by using Java
59 API. Simple Java based control/configuration code needs to be written
60 for each project. I usually put such code into directories devoted for
61 JUnit tests. Because it needs not to be compiled/embedded into final
62 product or project artifact I'm just willing to visualize.
63
64 Control code in general does the following:
65 1. Create graph object.
66 2. Java reflection/classloaders does not provide mechanism for
67    discovering all classes under given package. Therefore you need to
68    declare at least some classes to be added to the graph by:
69    + Manually adding individual classes to the graph.
70    + and/or: Let GraphViz recursively scan and parse specified
71      directories with Java source code files to discover class names.
72    + For every class added to the graph, GraphViz will recursively
73      inspect it and add all referecned classes to the graph as well.
74 3. Graphs easilly get very big and complex so optionally we filter
75    important code using classname wildcards patterns based blacklist
76    and/or whitelist.
77 4. Optionally we can tune some rendering parameters like:
78    + Possibility to remove orphaned classes (classes with no
79      references) from the graph.
80    + Specify target directory for generated visualization
81      files. (Default is user desktop directory)
82    + Keep intermediate GraphViz dot file for later inspection.
83 5. Render graph.
84
85
86 ** example 1: individually picked objects
87 This example demonstrates generating of class graph from hand picked
88 classes and visualizing GraphViz itself.
89
90 #+BEGIN_SRC java
91
92 // Create graph
93 final ClassGraph graph = new ClassGraph();
94
95 // Add some random object to the graph. GraphViz will detect Class from
96 // the object.
97 graph.add(graph);
98
99 // Also add some random class to the graph.
100 graph.add(Utils.class);
101
102 // Keep intermediary GraphViz DOT file for reference.
103 graph.setKeepDotFile(true);
104
105 // Produce bitmap image titled "JavaInspect.png" to the user Desktop
106 // directory
107 graph.generateGraph("JavaInspect");
108
109 #+END_SRC
110
111 Note: if desired, more compact version of the above:
112 #+BEGIN_SRC java
113 new ClassGraph().add(randomObject, RandomClass.class)
114                 .setKeepDotFile(true).generateGraph("JavaInspect");
115 #+END_SRC
116
117
118 Result:
119     - Generated DOT file: [[file:JavaInspect.dot][JavaInspect.dot]]
120     - Generated PNG image: [[file:JavaInspect.png][JavaInspect.png]]
121
122 ** example 2: scan java code, apply filters
123 #+BEGIN_SRC java
124 // Create graph
125 final ClassGraph graph = new ClassGraph();
126
127 // Recursively scan current directory for Java source code and attempt
128 // to detect class names from there to be added to the graph.
129 graph.addProject(".");
130
131 // Blacklist example classes from being shown on the graph
132 graph.blacklistClassPattern("eu.svjatoslav.inspector.java.structure.example.*");
133
134 // do not show single classes with no relationships on the graph
135 graph.hideOrphanedClasses();
136
137 // Produce bitmap image titled "JavaInspect full project.png" to the
138 // user Desktop directory.
139 graph.generateGraph("JavaInspect full project");
140 #+END_SRC
141 Result:
142     - Generated PNG image: [[file:JavaInspect%20full%20project.png][JavaInspect full project.png]]
143
144 * Embedding JavaInspect in your Maven project
145
146 Declare JavaInspect as dependency:
147 #+BEGIN_SRC xml
148     <dependencies>
149         ...
150         <dependency>
151             <groupId>eu.svjatoslav</groupId>
152             <artifactId>javainspect</artifactId>
153             <version>1.5-SNAPSHOT</version>
154         </dependency>
155         ...
156     </dependencies>
157 #+END_SRC
158
159
160 Add Maven repository to retrieve artifact from:
161 #+BEGIN_SRC xml
162     <repositories>
163         ...
164         <repository>
165             <id>svjatoslav.eu</id>
166             <name>Svjatoslav repository</name>
167             <url>http://www2.svjatoslav.eu/maven/</url>
168         </repository>
169         ...
170     </repositories>
171 #+END_SRC
172
173 * Requirements
174 [[http://www.graphviz.org/][GraphViz]] - shall be installed on the computer.
175
176 On Ubuntu/Debian use:
177 #+BEGIN_SRC sh
178 sudo apt-get install graphviz
179 #+END_SRC
180 * TODO
181 - BUG: Should not hide references if there are too many of them to
182   classes if referring classes are not visible anyway because of
183   blacklist/whitelist rules. Basically reference counting should
184   exclude not visible classes.
185 - FEATURE: add dark theme
186 - FEATURE: sort Class fields by alphabet
187 - FEATURE: visualize also concrete field values so it could be used as
188   ultra cool runtime logging framework
189 - FEATURE: possibility to visualize structure and data from JVM
190   snapshot
191 - FEATURE: possibility to attach to remote process to visualize
192   data/structure using JVM debug port and mechanism.
193 - FEATURE: possibility to attach to JVM using JVM agent
194 - FEATURE: possibility to script javainspect behavior
195 - FEATURE: possibility to select classes/fields/values to be
196   visualized in SQL like syntax
197 - FEATURE: configurable maven plugin to generate graphs as part of the
198   project build/release process