minor fix
[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: individually picked objects
87 This example demonstrates generating of class graph from hand picked
88 classes and visualizing GraphViz itself.
89
90 #+BEGIN_SRC java
91
92 // Create graph
93 final ClassGraph graph = new ClassGraph();
94
95 // Add some random object to the graph. GraphViz will detect Class from
96 // the object.
97 graph.add(graph);
98
99 // Also add some random class to the graph.
100 graph.add(Utils.class);
101
102 // Keep intermediary GraphViz DOT file for reference.
103 graph.setKeepDotFile(true);
104
105 // Produce bitmap image titled "JavaInspect.png" to the user Desktop
106 // directory
107 graph.generateGraph("JavaInspect");
108
109 #+END_SRC
110
111 Note: if desired, more compact version of the above:
112 #+BEGIN_SRC java
113 new ClassGraph().add(randomObject, RandomClass.class)
114                 .setKeepDotFile(true).generateGraph("JavaInspect");
115 #+END_SRC
116
117
118 Result:
119     - Generated DOT file: [[file:JavaInspect.dot][JavaInspect.dot]]
120     - Generated PNG image: [[file:JavaInspect.png][JavaInspect.png]]
121
122 ** example 2: scan java code, apply filters
123 #+BEGIN_SRC java
124 // Create graph
125 final ClassGraph graph = new ClassGraph();
126
127 // Recursively scan current directory for Java source code and attempt
128 // to detect class names from there to be added to the graph.
129 graph.addProject(".");
130
131 // Blacklist example classes from being shown on the graph
132 graph.blacklistClassPattern("eu.svjatoslav.inspector.java.structure.example.*");
133
134 // do not show single classes with no relationships on the graph
135 graph.hideOrphanedClasses();
136
137 // Produce bitmap image titled "JavaInspect full project.png" to the
138 // user Desktop directory.
139 graph.generateGraph("JavaInspect full project");
140 #+END_SRC
141 Result:
142     - Generated PNG image: [[file:JavaInspect%20full%20project.png][JavaInspect full project.png]]
143
144 ** example 3: GraphViz embedded in another project
145 1. Download project Sixth [[http://www2.svjatoslav.eu/gitweb/?p=sixth.git;a=snapshot;h=HEAD;sf=tgz][code snapshot]].
146 2. Inspect and run *DataGraph.java*.
147
148 * Embedding JavaInspect in your Maven project
149
150 Declare JavaInspect as dependency:
151 #+BEGIN_SRC xml
152 <dependencies>
153     ...
154     <dependency>
155         <groupId>eu.svjatoslav</groupId>
156         <artifactId>javainspect</artifactId>
157         <version>1.5-SNAPSHOT</version>
158     </dependency>
159     ...
160 </dependencies>
161 #+END_SRC
162
163
164 Add Maven repository to retrieve artifact from:
165 #+BEGIN_SRC xml
166 <repositories>
167     ...
168     <repository>
169         <id>svjatoslav.eu</id>
170         <name>Svjatoslav repository</name>
171         <url>http://www2.svjatoslav.eu/maven/</url>
172     </repository>
173     ...
174 </repositories>
175 #+END_SRC
176
177 * Requirements
178 [[http://www.graphviz.org/][GraphViz]] - shall be installed on the computer.
179
180 On Ubuntu/Debian use:
181 #+BEGIN_SRC sh
182 sudo apt-get install graphviz
183 #+END_SRC
184 * TO DO
185 - BUG: Should not hide references if there are too many of them to
186   classes if referring classes are not visible anyway because of
187   blacklist/whitelist rules. Basically reference counting should
188   exclude not visible classes.
189 - FEATURE: add dark theme
190 - FEATURE: sort Class fields by alphabet
191 - FEATURE: visualize also concrete field values so it could be used as
192   ultra cool runtime logging framework
193 - FEATURE: possibility to visualize structure and data from JVM
194   snapshot
195 - FEATURE: possibility to attach to remote process to visualize
196   data/structure using JVM debug port and mechanism.
197 - FEATURE: possibility to attach to JVM using JVM agent
198 - FEATURE: possibility to script javainspect behavior
199 - FEATURE: possibility to select classes/fields/values to be
200   visualized in SQL like syntax
201 - FEATURE: configurable maven plugin to generate graphs as part of the
202   project build/release process