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