e2a4f89a52c2a4c4f0c88f8c8791d89d8f819cd6
[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 GraphViz to generate
54 bitmap graph in PNG or SVG format.
55
56 By default on your Desktop directory when operated in library mode or
57 current working directory when operated as standalone commandline
58 application.
59
60 Notes:
61 + JavaInspect is developed and tested so far only on GNU/Linux.
62 + See: [[https://github.com/pahen/madge][Madge - similar tool for JavaScript]]
63
64 * Example graphs
65 + A very simple example:
66
67     file:example.png
68
69     Graph legend:
70
71     file:legend.png
72
73
74 + [[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]].
75
76 * Usage
77   :PROPERTIES:
78   :ID:       2ad2889e-6c95-4662-b3f4-2c341fc74522
79   :END:
80 JavaInspect can be controlled in 2 different ways:
81 + [[id:acf1896a-74b4-4914-acf6-a77075e07f25][as standalone commandline utility]]
82 + [[id:bbeeffc8-3767-440d-8d93-ec9124dd60ee][as embedded Java library via Java API]]
83
84 ** usage as commandline utility
85    :PROPERTIES:
86    :ID:       acf1896a-74b4-4914-acf6-a77075e07f25
87    :END:
88 To enable commandline support, (study and) execute script:
89 : commandline launcher/install
90
91 Warning: It was tested only on Debian Stretch linux.
92
93 Available commandline arguments:
94 #+BEGIN_VERSE
95 -j (existing files)...
96     JAR file(s) to render.
97
98 -n (mandatory, string)
99     Graph name.
100
101 --debug
102     Show debug info.
103
104 -k
105     Keep dot file.
106
107 -h
108     Hide orphaned classes.
109
110 -w (one to many strings)...
111     Whitelist glob(s).
112
113 -b (one to many strings)...
114     Blacklist glob(s).
115
116 -d (existingdirectory)
117     Target directory. Default is current directory.
118
119 -t (options: png, svg)
120     Target image type. Default is: svg.
121 #+END_VERSE
122 ** usage via Java API
123    :PROPERTIES:
124    :ID:       bbeeffc8-3767-440d-8d93-ec9124dd60ee
125    :END:
126 Requires that classes to be visualised are available in the classpath.
127
128 To get JavaInspect into same classpath with your projecs I so far came
129 up with 2 solutions:
130
131 1. Add JavaInspect library in your project as a dependency.
132
133 2. Create new Java project for the purpose visualizing your other
134    projects and include JavaInspect and your projecs binary artifacts
135    (Jar's) into new project classpath. Built binary Jar's (with no
136    source code) are sufficient because JavaInspect operates via
137    reflection.
138
139 Simple Java based control/configuration code needs to be written for
140 each project. I usually put such code into directories devoted for
141 JUnit tests. Because it needs not to be compiled/embedded into final
142 product or project artifact I'm just willing to visualize.
143
144 Control code in general does the following:
145 1. Create graph object.
146 2. Java reflection/classloaders does not provide mechanism for
147    discovering all classes under given package. Therefore you need to
148    declare at least some classes to be added to the graph by:
149    + Manually adding individual classes to the graph.
150    + and/or: Let GraphViz recursively scan and parse specified
151      directories with Java source code files to discover class names.
152    + For every class added to the graph, GraphViz will recursively
153      inspect it and add all referecned classes to the graph as well.
154 3. Graphs easilly get very big and complex so optionally we filter
155    important code using classname wildcards patterns based blacklist
156    and/or whitelist.
157 4. Optionally we can tune some rendering parameters like:
158    + Possibility to remove orphaned classes (classes with no
159      references) from the graph.
160    + Specify target directory for generated visualization
161      files. (Default is user desktop directory)
162    + Keep intermediate GraphViz dot file for later inspection.
163 5. Render graph.
164
165
166 *** example 1: individually picked objects
167 This example demonstrates generating of class graph from hand picked
168 classes and visualizing GraphViz itself.
169
170 #+BEGIN_SRC java
171
172 // Create graph
173 final ClassGraph graph = new ClassGraph();
174
175 // Add some random object to the graph. GraphViz will detect Class from
176 // the object.
177 graph.add(graph);
178
179 // Also add some random class to the graph.
180 graph.add(Utils.class);
181
182 // Keep intermediary GraphViz DOT file for reference.
183 graph.setKeepDotFile(true);
184
185 // Produce bitmap image titled "JavaInspect.png" to the user Desktop
186 // directory
187 graph.generateGraph("JavaInspect");
188
189 #+END_SRC
190
191 Note: if desired, more compact version of the above:
192 #+BEGIN_SRC java
193 new ClassGraph().add(randomObject, RandomClass.class)
194                 .setKeepDotFile(true).generateGraph("JavaInspect");
195 #+END_SRC
196
197
198 Result:
199     - Generated DOT file: [[file:JavaInspect.dot][JavaInspect.dot]]
200     - Generated PNG image: [[file:JavaInspect.png][JavaInspect.png]]
201
202 *** example 2: scan java code, apply filters
203 #+BEGIN_SRC java
204 // Create graph
205 final ClassGraph graph = new ClassGraph();
206
207 // Recursively scan current directory for Java source code and attempt
208 // to detect class names from there to be added to the graph.
209 graph.addProject(".");
210
211 // Blacklist example classes from being shown on the graph
212 graph.blacklistClassPattern("eu.svjatoslav.inspector.java.structure.example.*");
213
214 // do not show single classes with no relationships on the graph
215 graph.hideOrphanedClasses();
216
217 // Produce bitmap image titled "JavaInspect full project.png" to the
218 // user Desktop directory.
219 graph.generateGraph("JavaInspect full project");
220 #+END_SRC
221 Result:
222     - Generated PNG image: [[file:JavaInspect%20full%20project.png][JavaInspect full project.png]]
223
224 *** example 3: GraphViz embedded in another project
225 1. Download project Sixth [[https://www2.svjatoslav.eu/gitweb/?p=sixth.git;a=snapshot;h=HEAD;sf=tgz][code snapshot]].
226 2. Inspect and run *DataGraph.java*.
227
228 *** Embedding JavaInspect in your Maven project
229
230 Declare JavaInspect as dependency:
231 #+BEGIN_SRC xml
232 <dependencies>
233     ...
234     <dependency>
235         <groupId>eu.svjatoslav</groupId>
236         <artifactId>javainspect</artifactId>
237         <version>1.6</version>
238     </dependency>
239     ...
240 </dependencies>
241 #+END_SRC
242
243
244 Add Maven repository to retrieve artifact from:
245 #+BEGIN_SRC xml
246 <repositories>
247     ...
248     <repository>
249         <id>svjatoslav.eu</id>
250         <name>Svjatoslav repository</name>
251         <url>http://www2.svjatoslav.eu/maven/</url>
252     </repository>
253     ...
254 </repositories>
255 #+END_SRC
256
257 * Requirements
258 [[http://www.graphviz.org/][GraphViz]] - shall be installed on the computer.
259
260 On Ubuntu/Debian use:
261 #+BEGIN_SRC sh
262 sudo apt-get install graphviz
263 #+END_SRC
264 * TO DO
265 Note: Because this is side project (and I have many of them) I can
266 only contribute few hours per year at average. Any help is welcome.  A
267 LOT of cool ideas could be implemented. For intstance:
268
269 - BUG: Should not hide references if there are too many of them to
270   classes if referring classes are not visible anyway because of
271   blacklist/whitelist rules. Basically reference counting should
272   exclude not visible classes.
273
274 - BUG: Orphaned class removal does not work always. There are many
275   bugs and corner cases to find and fix still.
276
277 - BUG: Code is not very readable. Document and refactor for better
278   maintainability.
279
280 - FEATURE: Create installable DEB package.
281   - Submit it to some Debian developer for integration or become
282     Debian package maintainer.
283
284 - FEATURE: Make it modular. That is: central part, an application
285   model could be standalone and serializable.
286
287   - There could be multiple ways to acquire model:
288     - By introspecting application via Java reflections (current mode
289       of operation).
290     - By parsing java source. (unfinished)
291
292   - There could be ways to manipulate model:
293     - Store/load/compare.
294     - Trim uninteresting parts.
295     - Highlight important parts.
296
297   - There could be multiple ways to render model:
298     - PNG/SVG (currently implemented)
299     - PlantUML (TODO)
300     - Interactive 3D visualization (TODO)
301
302 - FEATURE: Replace internal java parser in package
303   eu.svjatoslav.inspector.java.methods with: https://javaparser.org/
304
305 - FEATURE: Integarte with [[http://plantuml.com/class-diagram][PlantUML]].
306
307 - FEATURE: Add dark theme for generated graphs.
308
309 - FEATURE: Sort Class fields by alphabet.
310
311 - FEATURE: Visualize also concrete field values so it could be used as
312   ultra cool runtime logging/debugging framework.
313
314 - FEATURE: Possibility to visualize structure and data from JVM
315   snapshot.
316
317 - FEATURE: Possibility to attach to remote process to visualize
318   data/structure using JVM debug port and mechanism.
319
320 - FEATURE: Possibility to attach to JVM using JVM agent.
321
322 - FEATURE: Possibility to inspect graphs in 3D using [[https://www3.svjatoslav.eu/projects/sixth-3d/][Sixth 3D engine]].
323
324 - FEATURE: Possibility to select classes/fields/values to be
325   visualized in some graph query language. For greater flexibility in
326   comparison to currently supported glob syntax.
327
328 - FEATURE: Add option to control JavaInspect via JSON or XML config
329   file. For example different graphs for given project could be
330   defined once in plain text config, possibly with the aid of some
331   interactive utility. Then defined graphs could be updated as part of
332   project build or release process.
333
334 - FEATURE: Configurable maven plugin to generate graphs as part of the
335   project build/release process.
336 * See also
337 Similar or alternative solutions:
338 + http://www.class-visualizer.net/