/Users/lyon/j4p/src/javassist/bytecode/ClassFileWriter.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved. 
4     * 
5     * The contents of this file are subject to the Mozilla Public License Version 
6     * 1.1 (the "License"); you may not use this file except in compliance with 
7     * the License.  Alternatively, the contents of this file may be used under 
8     * the terms of the GNU Lesser General Public License Version 2.1 or later. 
9     * 
10    * Software distributed under the License is distributed on an "AS IS" basis, 
11    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
12    * for the specific language governing rights and limitations under the 
13    * License. 
14    */ 
15    
16   package javassist.bytecode; 
17    
18   import java.io.PrintWriter; 
19    
20   import javassist.Modifier; 
21    
22   import java.util.List; 
23    
24   /** 
25    * A utility class for priting the contents of a class file. 
26    * It prints a constant pool table, fields, and methods in a 
27    * human readable representation. 
28    */ 
29   public class ClassFileWriter { 
30       /** 
31        * Prints the contents of a class file to the standard output stream. 
32        */ 
33       public static void print(ClassFile cf) { 
34           print(cf, new PrintWriter(System.out, true)); 
35       } 
36    
37       /** 
38        * Prints the contents of a class file. 
39        */ 
40       public static void print(ClassFile cf, PrintWriter out) { 
41           List list; 
42           int n; 
43    
44           /* 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers 
45            * are of a class. 
46            */ 
47           int mod 
48                   = AccessFlag.toModifier(cf.getAccessFlags() 
49                   & ~AccessFlag.SYNCHRONIZED); 
50           out.println(Modifier.toString(mod) + " class " 
51                   + cf.getName() + " extends " + cf.getSuperclass()); 
52           out.println(); 
53    
54           ConstPool cp = cf.getConstPool(); 
55           list = cf.getFields(); 
56           n = list.size(); 
57           for (int i = 0; i < n; ++i) { 
58               FieldInfo finfo = (FieldInfo) list.get(i); 
59               int acc = finfo.getAccessFlags(); 
60               out.println(Modifier.toString(AccessFlag.toModifier(acc)) 
61                       + " " + finfo.getName() + "\t" 
62                       + finfo.getDescriptor()); 
63               printAttributes(finfo.getAttributes(), out); 
64           } 
65    
66           out.println(); 
67           list = cf.getMethods(); 
68           n = list.size(); 
69           for (int i = 0; i < n; ++i) { 
70               MethodInfo minfo = (MethodInfo) list.get(i); 
71               int acc = minfo.getAccessFlags(); 
72               out.println(Modifier.toString(AccessFlag.toModifier(acc)) 
73                       + " " + minfo.getName() + "\t" 
74                       + minfo.getDescriptor()); 
75               printAttributes(minfo.getAttributes(), out); 
76               out.println(); 
77           } 
78    
79           out.println(); 
80           printAttributes(cf.getAttributes(), out); 
81       } 
82    
83       static void printAttributes(List list, PrintWriter out) { 
84           if (list == null) 
85               return; 
86    
87           int n = list.size(); 
88           for (int i = 0; i < n; ++i) { 
89               AttributeInfo ai = (AttributeInfo) list.get(i); 
90               if (ai instanceof CodeAttribute) { 
91                   CodeAttribute ca = (CodeAttribute) ai; 
92                   out.println("attribute: " + ai.getName() + ": " 
93                           + ai.getClass().getName()); 
94                   out.println("max stack " + ca.getMaxStack() 
95                           + ", max locals " + ca.getMaxLocals() 
96                           + ", " + ca.getExceptionTable().size() 
97                           + " catch blocks"); 
98                   out.println("<code attribute begin>"); 
99                   printAttributes(ca.getAttributes(), out); 
100                  out.println("<code attribute end>"); 
101              } else 
102                  out.println("attribute: " + ai.getName() 
103                          + " (" + ai.get().length + " byte): " 
104                          + ai.getClass().getName()); 
105          } 
106      } 
107  } 
108