KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > util > ConstantHTML


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.util;
18
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import org.apache.bcel.classfile.Constant;
23 import org.apache.bcel.classfile.ConstantClass;
24 import org.apache.bcel.classfile.ConstantFieldref;
25 import org.apache.bcel.classfile.ConstantInterfaceMethodref;
26 import org.apache.bcel.classfile.ConstantMethodref;
27 import org.apache.bcel.classfile.ConstantNameAndType;
28 import org.apache.bcel.classfile.ConstantPool;
29 import org.apache.bcel.classfile.ConstantString;
30 import org.apache.bcel.classfile.Method;
31 import org.apache.bcel.classfile.Utility;
32
33 /**
34  * Convert constant pool into HTML file.
35  *
36  * @version $Id: ConstantHTML.java 386056 2006-03-15 11:31:56Z tcurdt $
37  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
38  *
39  */

40 final class ConstantHTML implements org.apache.bcel.Constants {
41
42     private String JavaDoc class_name; // name of current class
43
private String JavaDoc class_package; // name of package
44
private ConstantPool constant_pool; // reference to constant pool
45
private PrintWriter JavaDoc file; // file to write to
46
private String JavaDoc[] constant_ref; // String to return for cp[i]
47
private Constant[] constants; // The constants in the cp
48
private Method[] methods;
49
50
51     ConstantHTML(String JavaDoc dir, String JavaDoc class_name, String JavaDoc class_package, Method[] methods,
52             ConstantPool constant_pool) throws IOException JavaDoc {
53         this.class_name = class_name;
54         this.class_package = class_package;
55         this.constant_pool = constant_pool;
56         this.methods = methods;
57         constants = constant_pool.getConstantPool();
58         file = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(dir + class_name + "_cp.html"));
59         constant_ref = new String JavaDoc[constants.length];
60         constant_ref[0] = "&lt;unknown&gt;";
61         file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
62         // Loop through constants, constants[0] is reserved
63
for (int i = 1; i < constants.length; i++) {
64             if (i % 2 == 0) {
65                 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
66             } else {
67                 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
68             }
69             if (constants[i] != null) {
70                 writeConstant(i);
71             }
72             file.print("</TD></TR>\n");
73         }
74         file.println("</TABLE></BODY></HTML>");
75         file.close();
76     }
77
78
79     String JavaDoc referenceConstant( int index ) {
80         return constant_ref[index];
81     }
82
83
84     private void writeConstant( int index ) {
85         byte tag = constants[index].getTag();
86         int class_index, name_index;
87         String JavaDoc ref;
88         // The header is always the same
89
file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag]
90                 + "</H4>");
91         /* For every constant type get the needed parameters and print them appropiately
92          */

93         switch (tag) {
94             case CONSTANT_InterfaceMethodref:
95             case CONSTANT_Methodref:
96                 // Get class_index and name_and_type_index, depending on type
97
if (tag == CONSTANT_Methodref) {
98                     ConstantMethodref c = (ConstantMethodref) constant_pool.getConstant(index,
99                             CONSTANT_Methodref);
100                     class_index = c.getClassIndex();
101                     name_index = c.getNameAndTypeIndex();
102                 } else {
103                     ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref) constant_pool
104                             .getConstant(index, CONSTANT_InterfaceMethodref);
105                     class_index = c1.getClassIndex();
106                     name_index = c1.getNameAndTypeIndex();
107                 }
108                 // Get method name and its class
109
String JavaDoc method_name = constant_pool.constantToString(name_index,
110                         CONSTANT_NameAndType);
111                 String JavaDoc html_method_name = Class2HTML.toHTML(method_name);
112                 // Partially compacted class name, i.e., / -> .
113
String JavaDoc method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
114                 String JavaDoc short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
115
short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
116
short_method_class = Utility.compactClassName(short_method_class, class_package
117                         + ".", true); // Remove class package prefix
118
// Get method signature
119
ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(
120                         name_index, CONSTANT_NameAndType);
121                 String JavaDoc signature = constant_pool.constantToString(c2.getSignatureIndex(),
122                         CONSTANT_Utf8);
123                 // Get array of strings containing the argument types
124
String JavaDoc[] args = Utility.methodSignatureArgumentTypes(signature, false);
125                 // Get return type string
126
String JavaDoc type = Utility.methodSignatureReturnType(signature, false);
127                 String JavaDoc ret_type = Class2HTML.referenceType(type);
128                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
129                 for (int i = 0; i < args.length; i++) {
130                     buf.append(Class2HTML.referenceType(args[i]));
131                     if (i < args.length - 1) {
132                         buf.append(",&nbsp;");
133                     }
134                 }
135                 buf.append(")");
136                 String JavaDoc arg_types = buf.toString();
137                 if (method_class.equals(class_name)) {
138                     ref = "<A HREF=\"" + class_name + "_code.html#method"
139                             + getMethodNumber(method_name + signature) + "\" TARGET=Code>"
140                             + html_method_name + "</A>";
141                 } else {
142                     ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>"
143                             + short_method_class + "</A>." + html_method_name;
144                 }
145                 constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp"
146                         + class_index + "\" TARGET=Constants>" + short_method_class
147                         + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + index
148                         + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
149                 file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types
150                         + "&nbsp;</TT>\n<UL>" + "<LI><A HREF=\"#cp" + class_index
151                         + "\">Class index(" + class_index + ")</A>\n" + "<LI><A HREF=\"#cp"
152                         + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
153                 break;
154             case CONSTANT_Fieldref:
155                 // Get class_index and name_and_type_index
156
ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index,
157                         CONSTANT_Fieldref);
158                 class_index = c3.getClassIndex();
159                 name_index = c3.getNameAndTypeIndex();
160                 // Get method name and its class (compacted)
161
String JavaDoc field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
162                 String JavaDoc short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
163
short_field_class = Utility.compactClassName(short_field_class,
164                         class_package + ".", true); // Remove class package prefix
165
String JavaDoc field_name = constant_pool
166                         .constantToString(name_index, CONSTANT_NameAndType);
167                 if (field_class.equals(class_name)) {
168                     ref = "<A HREF=\"" + field_class + "_methods.html#field" + field_name
169                             + "\" TARGET=Methods>" + field_name + "</A>";
170                 } else {
171                     ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + short_field_class
172                             + "</A>." + field_name + "\n";
173                 }
174                 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index
175                         + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\""
176                         + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>"
177                         + field_name + "</A>";
178                 file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp"
179                         + class_index + "\">Class(" + class_index + ")</A><BR>\n"
180                         + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index
181                         + ")</A></UL>");
182                 break;
183             case CONSTANT_Class:
184                 ConstantClass c4 = (ConstantClass) constant_pool.getConstant(index, CONSTANT_Class);
185                 name_index = c4.getNameIndex();
186                 String JavaDoc class_name2 = constant_pool.constantToString(index, tag); // / -> .
187
String JavaDoc short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
188
short_class_name = Utility.compactClassName(short_class_name, class_package + ".",
189                         true); // Remove class package prefix
190
ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name
191                         + "</A>";
192                 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index
193                         + "\" TARGET=ConstantPool>" + short_class_name + "</A>";
194                 file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
195                         + "\">Name index(" + name_index + ")</A></UL>\n");
196                 break;
197             case CONSTANT_String:
198                 ConstantString c5 = (ConstantString) constant_pool.getConstant(index,
199                         CONSTANT_String);
200                 name_index = c5.getStringIndex();
201                 String JavaDoc str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
202                 file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
203                         + "\">Name index(" + name_index + ")</A></UL>\n");
204                 break;
205             case CONSTANT_NameAndType:
206                 ConstantNameAndType c6 = (ConstantNameAndType) constant_pool.getConstant(index,
207                         CONSTANT_NameAndType);
208                 name_index = c6.getNameIndex();
209                 int signature_index = c6.getSignatureIndex();
210                 file.println("<P><TT>"
211                         + Class2HTML.toHTML(constant_pool.constantToString(index, tag))
212                         + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index("
213                         + name_index + ")</A>\n" + "<LI><A HREF=\"#cp" + signature_index
214                         + "\">Signature index(" + signature_index + ")</A></UL>\n");
215                 break;
216             default:
217                 file
218                         .println("<P><TT>"
219                                 + Class2HTML.toHTML(constant_pool.constantToString(index, tag))
220                                 + "</TT>\n");
221         } // switch
222
}
223
224
225     private final int getMethodNumber( String JavaDoc str ) {
226         for (int i = 0; i < methods.length; i++) {
227             String JavaDoc cmp = methods[i].getName() + methods[i].getSignature();
228             if (cmp.equals(str)) {
229                 return i;
230             }
231         }
232         return -1;
233     }
234 }
235
Popular Tags