KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > depend > DependencyTracker


1 /***
2  * ASM examples: examples showing how ASM can be used
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.depend;
31
32 import java.awt.Color JavaDoc;
33 import java.awt.Font JavaDoc;
34 import java.awt.Graphics2D JavaDoc;
35 import java.awt.RenderingHints JavaDoc;
36 import java.awt.geom.AffineTransform JavaDoc;
37 import java.awt.image.BufferedImage JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Set JavaDoc;
47 import java.util.zip.ZipEntry JavaDoc;
48 import java.util.zip.ZipFile JavaDoc;
49
50 import javax.imageio.ImageIO JavaDoc;
51
52 import org.objectweb.asm.ClassReader;
53
54 /**
55  * DependencyTracker
56  *
57  * @author Eugene Kuleshov
58  *
59  * @see http://www.onjava.com/pub/a/onjava/2005/08/17/asm3.html
60  */

61 public class DependencyTracker {
62
63     private static final int CELL_PAD = 1;
64
65     private static final int GRID_SIZE = 10;
66
67     private static final int CELLS_SIZE = 8;
68
69     private static final int LABEL_WIDTH = 200;
70
71     private static final String JavaDoc LABEL_FONT = "Tahoma-9";
72
73     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
74         DependencyVisitor v = new DependencyVisitor();
75
76         ZipFile JavaDoc f = new ZipFile JavaDoc(args[0]);
77
78         long l1 = System.currentTimeMillis();
79         Enumeration JavaDoc< ? extends ZipEntry JavaDoc> en = f.entries();
80         while (en.hasMoreElements()) {
81             ZipEntry JavaDoc e = en.nextElement();
82             String JavaDoc name = e.getName();
83             if (name.endsWith(".class")) {
84                 new ClassReader(f.getInputStream(e)).accept(v, false);
85             }
86         }
87         long l2 = System.currentTimeMillis();
88
89         Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Integer JavaDoc>> globals = v.getGlobals();
90         Set JavaDoc<String JavaDoc> jarPackages = globals.keySet();
91         Set JavaDoc<String JavaDoc> classPackages = v.getPackages();
92         int size = classPackages.size();
93         System.err.println("time: " + (l2 - l1) / 1000f + " " + size);
94
95         String JavaDoc[] jarNames = jarPackages.toArray(new String JavaDoc[jarPackages.size()]);
96         String JavaDoc[] classNames = classPackages.toArray(new String JavaDoc[classPackages.size()]);
97         Arrays.sort(jarNames);
98         Arrays.sort(classNames);
99
100         buildDiagram(jarNames, classNames, globals);
101     }
102
103     public static void buildDiagram(
104         String JavaDoc[] jarNames,
105         String JavaDoc[] classNames,
106         Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Integer JavaDoc>> globals) throws IOException JavaDoc
107     {
108         // normalize
109
int max = 0;
110         for (int i = 0; i < classNames.length; i++) {
111             Map JavaDoc<String JavaDoc, Integer JavaDoc> map = globals.get(classNames[i]);
112             if (map == null)
113                 continue;
114             Integer JavaDoc maxCount = Collections.max(map.values());
115             if (maxCount > max) {
116                 max = maxCount;
117             }
118         }
119
120         List JavaDoc<Color JavaDoc> colors = new ArrayList JavaDoc<Color JavaDoc>();
121         for (int i = LABEL_WIDTH; i >= 0; i--) {
122             colors.add(new Color JavaDoc(i, i, 255));
123         }
124         for (int i = 255; i >= 128; i--) {
125             colors.add(new Color JavaDoc(0, 0, i));
126         }
127         int maxcolor = colors.size() - 1;
128
129         int heigh = CELL_PAD + (CELLS_SIZE + CELL_PAD) * classNames.length;
130         int width = CELL_PAD + (CELLS_SIZE + CELL_PAD) * jarNames.length;
131
132         BufferedImage JavaDoc img = new BufferedImage JavaDoc(width + LABEL_WIDTH, heigh
133                 + LABEL_WIDTH, BufferedImage.TYPE_INT_RGB);
134
135         Graphics2D JavaDoc g = img.createGraphics();
136         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
137                 RenderingHints.VALUE_ANTIALIAS_ON);
138
139         g.setColor(Color.WHITE);
140         g.fillRect(0, 0, width + LABEL_WIDTH, heigh + LABEL_WIDTH);
141
142         // draw lines
143
g.setColor(Color.LIGHT_GRAY);
144         for (int y = GRID_SIZE; y < classNames.length; y += GRID_SIZE) {
145             g.drawLine(0, y * (CELLS_SIZE + CELL_PAD), width, y
146                     * (CELLS_SIZE + CELL_PAD));
147         }
148         for (int x = GRID_SIZE; x < jarNames.length; x += GRID_SIZE) {
149             g.drawLine(x * (CELLS_SIZE + CELL_PAD), 0, x
150                     * (CELLS_SIZE + CELL_PAD), heigh);
151         }
152
153         // draw diagram
154
for (int y = 0; y < classNames.length; y++) {
155             // System.err.println( y+" : "+classNames[ y]);
156

157             for (int x = 0; x < jarNames.length; x++) {
158                 Map JavaDoc<String JavaDoc, Integer JavaDoc> map = globals.get(jarNames[x]);
159
160                 Integer JavaDoc count = map == null ? null : map.get(classNames[y]);
161                 if (count != null) {
162                     int b = (int) ((float) count * maxcolor / max);
163
164                     g.setColor(colors.get(b));
165                     g.fillRect(CELL_PAD + (x * (CELLS_SIZE + CELL_PAD)),
166                             CELL_PAD + (y * (CELLS_SIZE + CELL_PAD)),
167                             CELLS_SIZE,
168                             CELLS_SIZE);
169                 }
170
171             }
172
173         }
174
175         // draw labels
176
Font JavaDoc f = Font.decode(LABEL_FONT);
177         g.setFont(f);
178         // g.setColor( new Color( 70, 70, 255));
179
g.setColor(Color.GRAY);
180
181         for (int y = 0; y < classNames.length; y++) {
182             AffineTransform JavaDoc trans = g.getTransform();
183             g.transform(AffineTransform.getTranslateInstance(CELL_PAD * 2
184                     + width, CELLS_SIZE + y * (CELLS_SIZE + CELL_PAD)));
185             g.transform(AffineTransform.getRotateInstance(Math.PI / 12));
186             g.drawString(classNames[y], 0, 0);
187             g.setTransform(trans);
188         }
189
190         for (int x = 0; x < jarNames.length; x++) {
191             AffineTransform JavaDoc trans = g.getTransform();
192             g.transform(AffineTransform.getTranslateInstance(CELL_PAD * 2 + x
193                     * (CELLS_SIZE + CELL_PAD), heigh + CELL_PAD * 2));
194             g.transform(AffineTransform.getRotateInstance(Math.PI / 2.5));
195             g.drawString(jarNames[x], 0, 0);
196             g.setTransform(trans);
197         }
198
199         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("test.png");
200         ImageIO.write(img, "png", fos);
201         fos.flush();
202         fos.close();
203     }
204 }
205
Popular Tags