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