1 30 package org.objectweb.asm.depend; 31 32 import java.awt.Color ; 33 import java.awt.Font ; 34 import java.awt.Graphics2D ; 35 import java.awt.RenderingHints ; 36 import java.awt.geom.AffineTransform ; 37 import java.awt.image.BufferedImage ; 38 import java.io.FileOutputStream ; 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 import java.util.Arrays ; 42 import java.util.Collections ; 43 import java.util.Enumeration ; 44 import java.util.List ; 45 import java.util.Map ; 46 import java.util.Set ; 47 import java.util.zip.ZipEntry ; 48 import java.util.zip.ZipFile ; 49 50 import javax.imageio.ImageIO ; 51 52 import org.objectweb.asm.ClassReader; 53 54 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 LABEL_FONT = "Tahoma-9"; 72 73 public static void main(String [] args) throws IOException { 74 DependencyVisitor v = new DependencyVisitor(); 75 76 ZipFile f = new ZipFile (args[0]); 77 78 long l1 = System.currentTimeMillis(); 79 Enumeration < ? extends ZipEntry > en = f.entries(); 80 while (en.hasMoreElements()) { 81 ZipEntry e = en.nextElement(); 82 String 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 <String , Map <String , Integer >> globals = v.getGlobals(); 90 Set <String > jarPackages = globals.keySet(); 91 Set <String > classPackages = v.getPackages(); 92 int size = classPackages.size(); 93 System.err.println("time: " + (l2 - l1) / 1000f + " " + size); 94 95 String [] jarNames = jarPackages.toArray(new String [jarPackages.size()]); 96 String [] classNames = classPackages.toArray(new String [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 [] jarNames, 105 String [] classNames, 106 Map <String , Map <String , Integer >> globals) throws IOException 107 { 108 int max = 0; 110 for (int i = 0; i < classNames.length; i++) { 111 Map <String , Integer > map = globals.get(classNames[i]); 112 if (map == null) 113 continue; 114 Integer maxCount = Collections.max(map.values()); 115 if (maxCount > max) { 116 max = maxCount; 117 } 118 } 119 120 List <Color > colors = new ArrayList <Color >(); 121 for (int i = LABEL_WIDTH; i >= 0; i--) { 122 colors.add(new Color (i, i, 255)); 123 } 124 for (int i = 255; i >= 128; i--) { 125 colors.add(new Color (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 img = new BufferedImage (width + LABEL_WIDTH, heigh 133 + LABEL_WIDTH, BufferedImage.TYPE_INT_RGB); 134 135 Graphics2D 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 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 for (int y = 0; y < classNames.length; y++) { 155 157 for (int x = 0; x < jarNames.length; x++) { 158 Map <String , Integer > map = globals.get(jarNames[x]); 159 160 Integer 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 Font f = Font.decode(LABEL_FONT); 177 g.setFont(f); 178 g.setColor(Color.GRAY); 180 181 for (int y = 0; y < classNames.length; y++) { 182 AffineTransform 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 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 fos = new FileOutputStream ("test.png"); 200 ImageIO.write(img, "png", fos); 201 fos.flush(); 202 fos.close(); 203 } 204 } 205 | Popular Tags |