updated project documentation
[javainspect.git] / doc / index.org
1 #+TITLE: JavaInspect - Utility to visualize java software
2
3 * (document settings) :noexport:
4 ** use dark style for TWBS-HTML exporter
5 #+HTML_HEAD: <link href="https://bootswatch.com/3/darkly/bootstrap.min.css" rel="stylesheet">
6 #+HTML_HEAD: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
7 #+HTML_HEAD: <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
8 #+HTML_HEAD: <style type="text/css">
9 #+HTML_HEAD:   footer {background-color: #111 !important;}
10 #+HTML_HEAD:   pre {background-color: #111; color: #ccc;}
11 #+HTML_HEAD: </style>
12
13 * General
14 - This program is free software: you can redistribute it and/or modify
15   it under the terms of the [[https://www.gnu.org/licenses/lgpl.html][GNU Lesser General Public License]] as
16   published by the Free Software Foundation, either version 3 of the
17   License, or (at your option) any later version.
18
19 - Program authors:
20   - Svjatoslav Agejenko
21     - Homepage: https://svjatoslav.eu
22     - Email: mailto://svjatoslav@svjatoslav.eu
23     - [[https://www.svjatoslav.eu/projects/][Other software projects hosted at svjatoslav.eu]]
24
25   - Tony Bargnesi
26     - GitHub fork for the project:
27       https://github.com/abargnesi/javainspect
28
29 ** Source code
30 - [[https://www2.svjatoslav.eu/gitweb/?p=javainspect.git;a=snapshot;h=HEAD;sf=tgz][Download latest snapshot in TAR GZ format]]
31
32 - [[https://www2.svjatoslav.eu/gitweb/?p=javainspect.git;a=summary][Browse Git repository online]]
33
34 - Clone Git repository using command:
35   : git clone https://www2.svjatoslav.eu/git/javainspect.git
36
37 * Goal and operating principle
38 Goal: simplify/speed up understanding the computer program code by
39 automatically visualizing its structure.
40
41 [[https://www3.svjatoslav.eu/projects/sixth-3d/graphs/][See example produced graphs]] for [[https://www3.svjatoslav.eu/projects/sixth-3d/][Sixth 3D - 3D engine project]].
42
43 JavaInspect can be used as a [[id:acf1896a-74b4-4914-acf6-a77075e07f25][standalone commandline utility]] as well as
44 [[id:bbeeffc8-3767-440d-8d93-ec9124dd60ee][java library]]. JavaInspect uses primarily Java built-in reflection to
45 discover and visualize any part of Java program.
46
47 JavaInspect currently has no GUI, configuration files, embedded
48 scripting support, direct Maven or Ant integration. See [[id:2ad2889e-6c95-4662-b3f4-2c341fc74522][usage]] to learn
49 how to instuct Javainspect what to do.
50
51 After discovering application structure and optionally filtering out
52 unimportant parts, JavaInspect produces GraphViz dot file that
53 describes data to be visualized. Then launches [[https://graphviz.org/][GraphViz]] to generate
54 bitmap graph in PNG or SVG format.
55
56 Notes:
57 + JavaInspect is developed and tested so far only on GNU/Linux.
58 + See: [[https://github.com/pahen/madge][Madge - similar tool for JavaScript]]
59
60 * Example graphs
61 + A very simple example:
62
63     file:example.png
64
65     Graph legend:
66
67     file:legend.png
68
69
70 + [[https://www3.svjatoslav.eu/projects/sixth-3d/graphs/][See example produced graphs]] for [[https://www3.svjatoslav.eu/projects/sixth-3d/][Sixth 3D - 3D engine project]].
71
72 * Usage
73   :PROPERTIES:
74   :ID:       2ad2889e-6c95-4662-b3f4-2c341fc74522
75   :END:
76 JavaInspect can be controlled in 2 different ways:
77 + [[id:acf1896a-74b4-4914-acf6-a77075e07f25][as standalone commandline utility]]
78 + [[id:bbeeffc8-3767-440d-8d93-ec9124dd60ee][as embedded Java library via Java API]]
79
80 ** usage as commandline utility
81    :PROPERTIES:
82    :ID:       acf1896a-74b4-4914-acf6-a77075e07f25
83    :END:
84 To enable commandline support, (study and) execute script:
85 : commandline launcher/install
86
87 Warning: It was tested only on Debian Stretch linux.
88
89 Available commandline arguments:
90 #+BEGIN_VERSE
91 -j (existing files)...
92     JAR file(s) to render.
93
94 -n (mandatory, string)
95     Graph name.
96
97 --debug
98     Show debug info.
99
100 -k
101     Keep dot file.
102
103 -h
104     Hide orphaned classes.
105
106 -w (one to many strings)...
107     Whitelist glob(s).
108
109 -b (one to many strings)...
110     Blacklist glob(s).
111
112 -d (existingdirectory)
113     Target directory. Default is current directory.
114
115 -t (options: png, svg)
116     Target image type. Default is: svg.
117 #+END_VERSE
118 ** usage via Java API
119    :PROPERTIES:
120    :ID:       bbeeffc8-3767-440d-8d93-ec9124dd60ee
121    :END:
122 Requires that classes to be visualised are available in the classpath.
123
124 To get JavaInspect into same classpath with your projecs I so far came
125 up with 2 solutions:
126
127 1. Add JavaInspect library in your project as a dependency.
128
129 2. Create new Java project for the purpose visualizing your other
130    projects and include JavaInspect and your projecs binary artifacts
131    (Jar's) into new project classpath. Built binary Jar's (with no
132    source code) are sufficient because JavaInspect operates via
133    reflection.
134
135 Simple Java based control/configuration code needs to be written for
136 each project. I usually put such code into directories devoted for
137 JUnit tests. Because it needs not to be compiled/embedded into final
138 product or project artifact I'm just willing to visualize.
139
140 Control code in general does the following:
141 1. Create graph object.
142 2. Java reflection/classloaders does not provide mechanism for
143    discovering all classes under given package. Therefore you need to
144    declare at least some classes to be added to the graph by manually
145    adding individual classes to the graph. For every class added to
146    the graph, GraphViz will recursively inspect it and add all
147    referecned classes to the graph as well.
148 3. Graphs easilly get very big and complex so optionally we filter
149    important code using classname [[https://en.wikipedia.org/wiki/Glob_(programming)][glob]] patterns based blacklist and/or
150    whitelist.
151 4. Optionally we can tune some rendering parameters like:
152    + Possibility to remove orphaned classes (classes with no
153      references) from the graph.
154    + Specify target directory for generated visualization
155      files. (Default is current directory)
156    + Keep intermediate GraphViz dot file for later inspection.
157 5. Render graph.
158
159
160 *** example 1: individually picked objects
161 This example demonstrates generating of class graph from hand picked
162 classes and visualizing GraphViz itself.
163
164 #+BEGIN_SRC java
165
166 // Create graph
167 final ClassGraph graph = new ClassGraph();
168
169 // Add some random object to the graph. GraphViz will detect Class from
170 // the object.
171 graph.add(graph);
172
173 // Also add some random class to the graph.
174 graph.add(Utils.class);
175
176 // Keep intermediary GraphViz DOT file for reference.
177 graph.setKeepDotFile(true);
178
179 // Produce bitmap image titled "JavaInspect.png" to the user Desktop
180 // directory
181 graph.generateGraph("JavaInspect");
182
183 #+END_SRC
184
185 Note: if desired, more compact version of the above:
186 #+BEGIN_SRC java
187 new ClassGraph().add(randomObject, RandomClass.class)
188                 .setKeepDotFile(true).generateGraph("JavaInspect");
189 #+END_SRC
190
191
192 Result:
193     - Generated DOT file: [[file:JavaInspect.dot][JavaInspect.dot]]
194     - Generated PNG image: [[file:JavaInspect.png][JavaInspect.png]]
195
196 *** example 2: GraphViz embedded in another project
197 1. Download project Sixth [[https://www2.svjatoslav.eu/gitweb/?p=sixth.git;a=snapshot;h=HEAD;sf=tgz][code snapshot]].
198 2. Inspect and run *DataGraph.java*.
199
200 *** Embedding JavaInspect in your Maven project
201
202 Declare JavaInspect as dependency:
203 #+BEGIN_SRC xml
204 <dependencies>
205     ...
206     <dependency>
207         <groupId>eu.svjatoslav</groupId>
208         <artifactId>javainspect</artifactId>
209         <version>1.7</version>
210     </dependency>
211     ...
212 </dependencies>
213 #+END_SRC
214
215
216 Add Maven repository to retrieve artifact from:
217 #+BEGIN_SRC xml
218 <repositories>
219     ...
220     <repository>
221         <id>svjatoslav.eu</id>
222         <name>Svjatoslav repository</name>
223         <url>http://www2.svjatoslav.eu/maven/</url>
224     </repository>
225     ...
226 </repositories>
227 #+END_SRC
228
229 * Requirements
230 [[http://www.graphviz.org/][GraphViz]] - shall be installed on the computer.
231
232 On Ubuntu/Debian use:
233 #+BEGIN_SRC sh
234 sudo apt-get install graphviz
235 #+END_SRC
236 * TO DO
237 Note: Because this is side project (and I have many of them) I can
238 only contribute few hours per year at average. Any help is welcome.  A
239 LOT of cool ideas could be implemented. For intstance:
240
241 - BUG: Should not hide references if there are too many of them to
242   classes if referring classes are not visible anyway because of
243   blacklist/whitelist rules. Basically reference counting should
244   exclude not visible classes.
245
246 - BUG: Orphaned class removal does not work always. There are many
247   bugs and corner cases to find and fix still.
248
249 - BUG: Code is not very readable. Document and refactor for better
250   maintainability.
251
252 - FEATURE: Create installable DEB package.
253   - Submit it to some Debian developer for integration or become
254     Debian package maintainer.
255
256 - FEATURE: Make it modular. That is: central part, an application
257   model could be standalone and serializable.
258
259   - There could be multiple ways to acquire model:
260     - By introspecting application via Java reflections (current mode
261       of operation).
262     - By parsing java source. (unfinished)
263
264   - There could be ways to manipulate model:
265     - Store/load/compare.
266     - Trim uninteresting parts.
267     - Highlight important parts.
268
269   - There could be multiple ways to render model:
270     - PNG/SVG (currently implemented)
271     - PlantUML (TODO)
272     - Interactive 3D visualization (TODO)
273
274 - FEATURE: Implement (or integrate existing java parser
275   https://javaparser.org/) to be able to produce code visualizations
276   based on source code (in addition to current reflection based
277   approach).
278
279 - FEATURE: Integarte with [[http://plantuml.com/class-diagram][PlantUML]].
280
281 - FEATURE: Add dark theme for generated graphs.
282
283 - FEATURE: Sort Class fields by alphabet.
284
285 - FEATURE: Visualize also concrete field values so it could be used as
286   ultra cool runtime logging/debugging framework.
287
288 - FEATURE: Possibility to visualize structure and data from JVM
289   snapshot.
290
291 - FEATURE: Possibility to attach to remote process to visualize
292   data/structure using JVM debug port and mechanism.
293
294 - FEATURE: Possibility to attach to JVM using JVM agent.
295
296 - FEATURE: Possibility to inspect graphs in 3D using [[https://www3.svjatoslav.eu/projects/sixth-3d/][Sixth 3D engine]].
297
298 - FEATURE: Possibility to select classes/fields/values to be
299   visualized in some graph query language. For greater flexibility in
300   comparison to currently supported glob syntax.
301
302 - FEATURE: Add option to control JavaInspect via JSON or XML config
303   file. For example different graphs for given project could be
304   defined once in plain text config, possibly with the aid of some
305   interactive utility. Then defined graphs could be updated as part of
306   project build or release process.
307
308 - FEATURE: Configurable maven plugin to generate graphs as part of the
309   project build/release process.
310 * See also
311 Similar or alternative solutions:
312 + http://www.class-visualizer.net/