KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > ClassFileWriter


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 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 JavaDoc;
19 import javassist.Modifier;
20 import java.util.List JavaDoc;
21
22 /**
23  * A utility class for priting the contents of a class file.
24  * It prints a constant pool table, fields, and methods in a
25  * human readable representation.
26  */

27 public class ClassFileWriter {
28     /**
29      * Prints the contents of a class file to the standard output stream.
30      */

31     public static void print(ClassFile cf) {
32         print(cf, new PrintWriter JavaDoc(System.out, true));
33     }
34
35     /**
36      * Prints the contents of a class file.
37      */

38     public static void print(ClassFile cf, PrintWriter JavaDoc out) {
39         List JavaDoc list;
40         int n;
41
42         /* 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers
43          * are of a class.
44          */

45         int mod
46             = AccessFlag.toModifier(cf.getAccessFlags()
47                                     & ~AccessFlag.SYNCHRONIZED);
48         out.println("major: " + cf.major + ", minor: " + cf.minor);
49         out.println(Modifier.toString(mod) + " class "
50                     + cf.getName() + " extends " + cf.getSuperclass());
51
52         String JavaDoc[] infs = cf.getInterfaces();
53         if (infs != null && infs.length > 0) {
54             out.print(" implements ");
55             out.print(infs[0]);
56             for (int i = 1; i < infs.length; ++i)
57                 out.print(", " + infs[i]);
58
59             out.println();
60         }
61
62         out.println();
63         ConstPool cp = cf.getConstPool();
64         list = cf.getFields();
65         n = list.size();
66         for (int i = 0; i < n; ++i) {
67             FieldInfo finfo = (FieldInfo)list.get(i);
68             int acc = finfo.getAccessFlags();
69             out.println(Modifier.toString(AccessFlag.toModifier(acc))
70                         + " " + finfo.getName() + "\t"
71                         + finfo.getDescriptor());
72             printAttributes(finfo.getAttributes(), out);
73         }
74
75         out.println();
76         list = cf.getMethods();
77         n = list.size();
78         for (int i = 0; i < n; ++i) {
79             MethodInfo minfo = (MethodInfo)list.get(i);
80             int acc = minfo.getAccessFlags();
81             out.println(Modifier.toString(AccessFlag.toModifier(acc))
82                         + " " + minfo.getName() + "\t"
83                         + minfo.getDescriptor());
84             printAttributes(minfo.getAttributes(), out);
85             out.println();
86         }
87
88         out.println();
89         printAttributes(cf.getAttributes(), out);
90     }
91
92     static void printAttributes(List JavaDoc list, PrintWriter JavaDoc out) {
93         if (list == null)
94             return;
95
96         int n = list.size();
97         for (int i = 0; i < n; ++i) {
98             AttributeInfo ai = (AttributeInfo)list.get(i);
99             if (ai instanceof CodeAttribute) {
100                 CodeAttribute ca = (CodeAttribute)ai;
101                 out.println("attribute: " + ai.getName() + ": "
102                             + ai.getClass().getName());
103                 out.println("max stack " + ca.getMaxStack()
104                             + ", max locals " + ca.getMaxLocals()
105                             + ", " + ca.getExceptionTable().size()
106                             + " catch blocks");
107                 out.println("<code attribute begin>");
108                 printAttributes(ca.getAttributes(), out);
109                 out.println("<code attribute end>");
110             }
111             else
112                 out.println("attribute: " + ai.getName()
113                             + " (" + ai.get().length + " byte): "
114                             + ai.getClass().getName());
115         }
116     }
117 }
118
Popular Tags