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