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