87aad61060e56b3596f30d40917f0d00eb51fe9f
[org.ibex.gcclass.git] / src / org / ibex / gcclass / SizeCheck.java
1 // Copyright (C) 2004 Brian Alliet
2
3 package org.ibex.gcclass;
4
5 import java.util.*;
6 import java.util.zip.*;
7 import java.io.*;
8 import org.apache.bcel.util.*;
9 import org.apache.bcel.classfile.*;
10
11 // This code is hideous... it is just a quick hack
12
13 public class SizeCheck {
14     public static void main(String[] args) throws Exception {
15         if(args.length < 3) {
16             System.err.println("Usage SizeCheck classpath class1 ... [class n]");
17             System.exit(1);
18         }
19         String classpath = ClassPath.SYSTEM_CLASS_PATH + File.pathSeparator + args[0];
20         Repository repo = SyntheticRepository.getInstance(new ClassPath(classpath));
21         
22         List all= new ArrayList();
23         int alltotal=0;
24         for(int j=1;j<args.length;j++) {
25             String s = args[j];
26             if(s.endsWith(".class")) s = s.substring(0,s.length()-6).replace('/','.');
27             while(s.startsWith(".")) s = s.substring(1);
28             JavaClass c = repo.loadClass(s);
29             List stuff = new ArrayList();
30             ByteArrayOutputStream baos;
31             GZIPOutputStream gzos;
32             DataOutputStream dos;
33             int total = 0;
34             baos = new ByteArrayOutputStream();
35             gzos = new GZIPOutputStream(baos);
36             dos = new DataOutputStream(gzos);
37             c.getConstantPool().dump(dos);
38             gzos.finish();
39             total += baos.size();
40             stuff.add(new Stuff(baos.size(),"Constant Pool"));
41             Method[] methods = c.getMethods();
42             for(int i=0;i<methods.length;i++) {
43                 baos = new ByteArrayOutputStream();
44                 gzos = new GZIPOutputStream(baos);
45                 dos = new DataOutputStream(gzos);
46                 methods[i].dump(dos);
47                 gzos.finish();
48                 stuff.add(new Stuff(baos.size(),methods[i].getName()));
49                 total += baos.size();
50             }
51             stuff.add(new Stuff(total,"Total"));
52             StringBuffer sb = new StringBuffer(s + ":\n");
53             Collections.sort(stuff);
54             for(int i=0;i<stuff.size();i++) {
55                 Stuff st = (Stuff) stuff.get(i);
56                 sb.append("\t" + st.size + (st.size < 1000 ? "\t\t" : "\t") + st.desc + "\n");
57             }
58             all.add(new Stuff(total,sb.toString()));
59             alltotal += total;
60         }
61         Collections.sort(all);
62         for(int i=0;i<all.size();i++) {
63             Stuff st = (Stuff) all.get(i);
64             System.out.println(st.size + ": " + st.desc);
65         }
66         System.out.println("Total: " + alltotal);
67     }
68     
69     public static class Stuff implements Comparable {
70         int size;
71         String desc;
72         public int compareTo(Object o_) {
73             Stuff o = (Stuff)o_;
74             return desc.equals("Total") ? 1 : o.desc.equals("Total") ? -1 : -(size - o.size); }
75         public Stuff(int size, String desc) { this.size = size; this.desc = desc; }
76     }
77 }