updated example code
[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 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 needed, this code can be very compactly written too:
112 #+BEGIN_SRC java
113
114 #+END_SRC
115
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
123 Recursively scan current directory for Java source code and attempt to
124 detect class names from there to be added to the graph.
125
126 #+BEGIN_SRC java
127   graph.addProject(".");
128
129   // Blacklist example classes from being shown on the graph
130   graph.getFilter().blacklistClassPattern(
131       "eu.svjatoslav.inspector.java.structure.example.*");
132
133   // do not show single classes with no relationships on the graph
134   graph.hideOrphanedClasses();
135
136   // Produce bitmap image titled "JavaInspect full project.png" to the
137   // user Desktop directory.
138   graph.generateGraph("JavaInspect full project");
139 #+END_SRC
140 Result:
141     - Generated PNG image: [[file:JavaInspect%20full%20project.png][JavaInspect full project.png]]
142
143 * Embedding JavaInspect in your Maven project
144
145 Declare JavaInspect as dependency:
146 #+BEGIN_SRC xml
147     <dependencies>
148         ...
149         <dependency>
150             <groupId>eu.svjatoslav</groupId>
151             <artifactId>javainspect</artifactId>
152             <version>1.3</version>
153         </dependency>
154         ...
155     </dependencies>
156 #+END_SRC
157
158
159 Add Maven repository to retrieve artifact from:
160 #+BEGIN_SRC xml
161     <repositories>
162         ...
163         <repository>
164             <id>svjatoslav.eu</id>
165             <name>Svjatoslav repository</name>
166             <url>http://www2.svjatoslav.eu/maven/</url>
167         </repository>
168         ...
169     </repositories>
170 #+END_SRC
171
172 * Requirements
173
174 [[http://www.graphviz.org/][GraphViz]] - shall be installed on the computer.
175
176 On Ubuntu/Debian use:
177 : sudo apt-get install graphviz
178 * TODO
179 - BUG: Should not hide references if there are too many of them to
180   classes if referring classes are not visible anyway because of
181   blacklist/whitelist rules. Basically reference counting should
182   exclude not visible classes.
183 - BUG: Current code is quite messy (because of lack of time) things
184   were implemented ad-hoc. Needs cleanup/refactoring for better
185   readability.
186 - FEATURE: add dark theme
187 - FEATURE: sort Class fields by alphabet
188 - FEATURE: visualize also concrete field values so it could be used as
189   ultra cool runtime logging framework
190 - FEATURE: possibility to visualize structure and data from JVM
191   snapshot
192 - FEATURE: possibility to attach to remote process to visualize
193   data/structure using JVM debug port and mechanism.
194 - FEATURE: possibility to attach to JVM using JVM agent
195 - FEATURE: possibility to script javainspect behavior
196 - FEATURE: possibility to select classes/fields/values to be
197   visualized in SQL like syntax
198 - FEATURE: configurable maven plugin to generate graphs as part of the
199   project build/release process