1 // Copyright (C) 2004 Brian Alliet
3 package com.brian_web.gcclass;
6 import java.util.zip.*;
8 import org.apache.bcel.util.*;
9 import org.apache.bcel.classfile.*;
11 // This code is hideous... it is just a quick hack
13 public class SizeCheck {
14 public static void main(String[] args) throws Exception {
16 System.err.println("Usage SizeCheck classpath class1 ... [class n]");
19 String classpath = ClassPath.SYSTEM_CLASS_PATH + File.pathSeparator + args[0];
20 Repository repo = SyntheticRepository.getInstance(new ClassPath(classpath));
22 List all= new ArrayList();
24 for(int j=1;j<args.length;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;
34 baos = new ByteArrayOutputStream();
35 gzos = new GZIPOutputStream(baos);
36 dos = new DataOutputStream(gzos);
37 c.getConstantPool().dump(dos);
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);
48 stuff.add(new Stuff(baos.size(),methods[i].getName()));
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");
58 all.add(new Stuff(total,sb.toString()));
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);
66 System.out.println("Total: " + alltotal);
69 public static class Stuff implements Comparable {
72 public int compareTo(Object 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; }