KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > ListCodeSize


1 package gnu.bytecode;
2 import java.io.*;
3
4 /** Lists the number of bytes in named methods.
5  * Useful for regression testing of code generation and inlining.
6  */

7
8 public class ListCodeSize
9 {
10   public static void usage()
11   {
12     System.err.println("Usage: class methodname ...");
13     System.exit(-1);
14   }
15
16   static void print (Method method)
17   {
18     System.out.print(method);
19     CodeAttr code = method.getCode();
20     if (code == null)
21       System.out.print(": no code");
22     else
23       {
24         System.out.print(": ");
25         System.out.print(code.getPC());
26         System.out.print(" bytes");
27       }
28     System.out.println();
29   }
30
31   public static final void main (String JavaDoc[] args)
32   {
33     if (args.length == 0)
34       usage();
35     String JavaDoc filename = args[0];
36     try
37       {
38     java.io.InputStream JavaDoc inp = new FileInputStream(filename);
39
40         ClassType ctype = new ClassType();
41         new ClassFileInput(ctype, inp);
42
43         if (args.length == 1)
44           {
45             for (Method method = ctype.getMethods(); method != null;
46                  method = method.getNext())
47               {
48                 print(method);
49               }
50           }
51         else
52           {
53             for (int i = 1; i < args.length; i++)
54               {
55                 for (Method method = ctype.getMethods(); method != null;
56                      method = method.getNext())
57                   {
58                     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
59                     sbuf.append(method.getName());
60                     method.listParameters(sbuf);
61                     sbuf.append(method.getReturnType().getName());
62                     if (sbuf.toString().startsWith(args[i]))
63                       print(method);
64                   }
65               }
66           }
67       }
68     catch (java.io.FileNotFoundException JavaDoc e)
69       {
70     System.err.println("File "+filename+" not found");
71     System.exit(-1);
72       }
73     catch (java.io.IOException JavaDoc e)
74       {
75     System.err.println(e);
76         e.printStackTrace();
77     System.exit(-1);
78       }
79   }
80 }
81
Popular Tags