KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CountOpcodes


1 import jode.bytecode.*;
2 import java.util.*;
3 import com.sun.java.util.collections.Iterator;
4
5 public class CountOpcodes {
6     static int[] opcodeCount = new int[256];
7     static int[] predsCount = new int[1024];
8     static int[] succsCount = new int[1024];
9     static Vector instructions = new Vector(73400);
10
11     public static void handleBytecode(BytecodeInfo bc) {
12     for (Iterator i = bc.getInstructions().iterator(); i.hasNext();) {
13         Instruction instr = (Instruction) i.next();
14         instructions.addElement(instr);
15         opcodeCount[instr.getOpcode()]++;
16         Instruction[] p = instr.getPreds();
17         if (p == null)
18         predsCount[0]++;
19         else
20         predsCount[p.length]++;
21
22         Instruction[] s = instr.getSuccs();
23         if (s == null)
24         succsCount[0]++;
25         else
26         succsCount[s.length]++;
27     }
28     }
29
30     public static void handlePackage(String JavaDoc pack) {
31     Enumeration subs = ClassInfo.getClassesAndPackages(pack);
32     while (subs.hasMoreElements()) {
33         String JavaDoc comp = (String JavaDoc) subs.nextElement();
34         String JavaDoc full = pack + "." + comp;
35         if (ClassInfo.isPackage(full))
36         handlePackage(full);
37         else {
38         ClassInfo clazz = ClassInfo.forName(full);
39         clazz.loadInfo(ClassInfo.FULLINFO);
40         MethodInfo[] ms = clazz.getMethods();
41         for (int i=0; i < ms.length; i++) {
42             BytecodeInfo bc = ms[i].getBytecode();
43             if (bc != null)
44             handleBytecode(bc);
45         }
46         }
47     }
48     }
49
50     public static void main(String JavaDoc[] params) {
51     ClassInfo.setClassPath(params[0]);
52     Runtime JavaDoc runtime = Runtime.getRuntime();
53     long free = runtime.freeMemory();
54     long last;
55     do {
56         last = free;
57         runtime.gc();
58         runtime.runFinalization();
59         free = runtime.freeMemory();
60     } while (free < last);
61     System.err.println("used before: "+(runtime.totalMemory()- free));
62     long time = System.currentTimeMillis();
63     handlePackage("com");
64     System.err.println("Time used: "+(System.currentTimeMillis() - time));
65     free = runtime.freeMemory();
66     do {
67         last = free;
68         runtime.gc();
69         runtime.runFinalization();
70         free = runtime.freeMemory();
71     } while (free < last);
72     System.err.println("used after: "+(runtime.totalMemory()- free));
73     System.err.println("instruction count: "+instructions.size());
74     for (int i=0; i< 256; i++) {
75         if (opcodeCount[i] > 0)
76         System.err.println("Opcode "+i+": \t ("+Opcodes.opcodeString[i]+")\t"+opcodeCount[i]);
77     }
78     int moreThanTwo = 0;
79     for (int i=0; i< predsCount.length; i++) {
80         if (predsCount[i] > 0) {
81         System.err.println("preds "+i+": \t"+predsCount[i]);
82         if (i>1)
83             moreThanTwo +=predsCount[i];
84         }
85     }
86     System.err.println("preds >2: \t"+moreThanTwo);
87     
88     moreThanTwo = 0;
89     for (int i=0; i< succsCount.length; i++) {
90         if (succsCount[i] > 0) {
91         System.err.println("succs "+i+": \t"+succsCount[i]);
92         if (i>1)
93             moreThanTwo +=succsCount[i];
94         }
95     }
96     System.err.println("succs >2: \t"+moreThanTwo);
97     }
98 }
99
100
Popular Tags