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