|
1 /* |
|
2 * reserved comment block |
|
3 * DO NOT REMOVE OR ALTER! |
|
4 */ |
|
5 |
|
6 package com.sun.org.apache.bcel.internal.util; |
|
7 |
|
8 /* ==================================================================== |
|
9 * The Apache Software License, Version 1.1 |
|
10 * |
|
11 * Copyright (c) 2001 The Apache Software Foundation. All rights |
|
12 * reserved. |
|
13 * |
|
14 * Redistribution and use in source and binary forms, with or without |
|
15 * modification, are permitted provided that the following conditions |
|
16 * are met: |
|
17 * |
|
18 * 1. Redistributions of source code must retain the above copyright |
|
19 * notice, this list of conditions and the following disclaimer. |
|
20 * |
|
21 * 2. Redistributions in binary form must reproduce the above copyright |
|
22 * notice, this list of conditions and the following disclaimer in |
|
23 * the documentation and/or other materials provided with the |
|
24 * distribution. |
|
25 * |
|
26 * 3. The end-user documentation included with the redistribution, |
|
27 * if any, must include the following acknowledgment: |
|
28 * "This product includes software developed by the |
|
29 * Apache Software Foundation (http://www.apache.org/)." |
|
30 * Alternately, this acknowledgment may appear in the software itself, |
|
31 * if and wherever such third-party acknowledgments normally appear. |
|
32 * |
|
33 * 4. The names "Apache" and "Apache Software Foundation" and |
|
34 * "Apache BCEL" must not be used to endorse or promote products |
|
35 * derived from this software without prior written permission. For |
|
36 * written permission, please contact apache@apache.org. |
|
37 * |
|
38 * 5. Products derived from this software may not be called "Apache", |
|
39 * "Apache BCEL", nor may "Apache" appear in their name, without |
|
40 * prior written permission of the Apache Software Foundation. |
|
41 * |
|
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
|
43 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
45 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
|
46 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
49 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
50 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
51 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
|
52 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
53 * SUCH DAMAGE. |
|
54 * ==================================================================== |
|
55 * |
|
56 * This software consists of voluntary contributions made by many |
|
57 * individuals on behalf of the Apache Software Foundation. For more |
|
58 * information on the Apache Software Foundation, please see |
|
59 * <http://www.apache.org/>. |
|
60 */ |
|
61 |
|
62 import com.sun.org.apache.bcel.internal.classfile.*; |
|
63 import java.io.*; |
|
64 |
|
65 /** |
|
66 * Convert constant pool into HTML file. |
|
67 * |
|
68 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
|
69 * |
|
70 */ |
|
71 final class ConstantHTML implements com.sun.org.apache.bcel.internal.Constants { |
|
72 private String class_name; // name of current class |
|
73 private String class_package; // name of package |
|
74 private ConstantPool constant_pool; // reference to constant pool |
|
75 private PrintWriter file; // file to write to |
|
76 private String[] constant_ref; // String to return for cp[i] |
|
77 private Constant[] constants; // The constants in the cp |
|
78 private Method[] methods; |
|
79 |
|
80 ConstantHTML(String dir, String class_name, String class_package, Method[] methods, |
|
81 ConstantPool constant_pool) throws IOException |
|
82 { |
|
83 this.class_name = class_name; |
|
84 this.class_package = class_package; |
|
85 this.constant_pool = constant_pool; |
|
86 this.methods = methods; |
|
87 constants = constant_pool.getConstantPool(); |
|
88 file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html")); |
|
89 constant_ref = new String[constants.length]; |
|
90 constant_ref[0] = "<unknown>"; |
|
91 |
|
92 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); |
|
93 |
|
94 // Loop through constants, constants[0] is reserved |
|
95 for(int i=1; i < constants.length; i++) { |
|
96 if(i % 2 == 0) |
|
97 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); |
|
98 else |
|
99 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); |
|
100 |
|
101 if(constants[i] != null) |
|
102 writeConstant(i); |
|
103 |
|
104 file.print("</TD></TR>\n"); |
|
105 } |
|
106 |
|
107 file.println("</TABLE></BODY></HTML>"); |
|
108 file.close(); |
|
109 } |
|
110 |
|
111 String referenceConstant(int index) { |
|
112 return constant_ref[index]; |
|
113 } |
|
114 |
|
115 private void writeConstant(int index) { |
|
116 byte tag = constants[index].getTag(); |
|
117 int class_index, name_index; |
|
118 String ref; |
|
119 |
|
120 // The header is always the same |
|
121 file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>"); |
|
122 |
|
123 /* For every constant type get the needed parameters and print them appropiately |
|
124 */ |
|
125 switch(tag) { |
|
126 case CONSTANT_InterfaceMethodref: |
|
127 case CONSTANT_Methodref: |
|
128 // Get class_index and name_and_type_index, depending on type |
|
129 if(tag == CONSTANT_Methodref) { |
|
130 ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref); |
|
131 class_index = c.getClassIndex(); |
|
132 name_index = c.getNameAndTypeIndex(); |
|
133 } |
|
134 else { |
|
135 ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref); |
|
136 class_index = c1.getClassIndex(); |
|
137 name_index = c1.getNameAndTypeIndex(); |
|
138 } |
|
139 |
|
140 // Get method name and its class |
|
141 String method_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType); |
|
142 String html_method_name = Class2HTML.toHTML(method_name); |
|
143 |
|
144 // Partially compacted class name, i.e., / -> . |
|
145 String method_class = constant_pool.constantToString(class_index, CONSTANT_Class); |
|
146 String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang. |
|
147 short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang. |
|
148 short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); // Remove class package prefix |
|
149 |
|
150 // Get method signature |
|
151 ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType); |
|
152 String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8); |
|
153 // Get array of strings containing the argument types |
|
154 String[] args = Utility.methodSignatureArgumentTypes(signature, false); |
|
155 |
|
156 // Get return type string |
|
157 String type = Utility.methodSignatureReturnType(signature, false); |
|
158 String ret_type = Class2HTML.referenceType(type); |
|
159 |
|
160 StringBuffer buf = new StringBuffer("("); |
|
161 for(int i=0; i < args.length; i++) { |
|
162 buf.append(Class2HTML.referenceType(args[i])); |
|
163 if(i < args.length - 1) |
|
164 buf.append(", "); |
|
165 } |
|
166 buf.append(")"); |
|
167 |
|
168 String arg_types = buf.toString(); |
|
169 |
|
170 if(method_class.equals(class_name)) // Method is local to class |
|
171 ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) + |
|
172 "\" TARGET=Code>" + html_method_name + "</A>"; |
|
173 else |
|
174 ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class + |
|
175 "</A>." + html_method_name; |
|
176 |
|
177 constant_ref[index] = ret_type + " <A HREF=\"" + class_name + "_cp.html#cp" + class_index + |
|
178 "\" TARGET=Constants>" + |
|
179 short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + |
|
180 index + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types; |
|
181 |
|
182 file.println("<P><TT>" + ret_type + " " + ref + arg_types + " </TT>\n<UL>" + |
|
183 "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index + ")</A>\n" + |
|
184 "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>"); |
|
185 break; |
|
186 |
|
187 case CONSTANT_Fieldref: |
|
188 // Get class_index and name_and_type_index |
|
189 ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref); |
|
190 class_index = c3.getClassIndex(); |
|
191 name_index = c3.getNameAndTypeIndex(); |
|
192 |
|
193 // Get method name and its class (compacted) |
|
194 String field_class = constant_pool.constantToString(class_index, CONSTANT_Class); |
|
195 String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang. |
|
196 short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); // Remove class package prefix |
|
197 |
|
198 String field_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType); |
|
199 |
|
200 if(field_class.equals(class_name)) // Field is local to class |
|
201 ref = "<A HREF=\"" + field_class + "_methods.html#field" + |
|
202 field_name + "\" TARGET=Methods>" + field_name + "</A>"; |
|
203 else |
|
204 ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + |
|
205 short_field_class + "</A>." + field_name + "\n"; |
|
206 |
|
207 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index + "\" TARGET=Constants>" + |
|
208 short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + |
|
209 index + "\" TARGET=ConstantPool>" + field_name + "</A>"; |
|
210 |
|
211 file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + |
|
212 "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index + ")</A><BR>\n" + |
|
213 "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>"); |
|
214 break; |
|
215 |
|
216 case CONSTANT_Class: |
|
217 ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class); |
|
218 name_index = c4.getNameIndex(); |
|
219 String class_name2 = constant_pool.constantToString(index, tag); // / -> . |
|
220 String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang. |
|
221 short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); // Remove class package prefix |
|
222 |
|
223 ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>"; |
|
224 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index + |
|
225 "\" TARGET=ConstantPool>" + short_class_name + "</A>"; |
|
226 |
|
227 file.println("<P><TT>" + ref + "</TT><UL>" + |
|
228 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); |
|
229 break; |
|
230 |
|
231 case CONSTANT_String: |
|
232 ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String); |
|
233 name_index = c5.getStringIndex(); |
|
234 |
|
235 String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag)); |
|
236 |
|
237 file.println("<P><TT>" + str + "</TT><UL>" + |
|
238 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); |
|
239 break; |
|
240 |
|
241 case CONSTANT_NameAndType: |
|
242 ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType); |
|
243 name_index = c6.getNameIndex(); |
|
244 int signature_index = c6.getSignatureIndex(); |
|
245 |
|
246 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" + |
|
247 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" + |
|
248 "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" + |
|
249 signature_index + ")</A></UL>\n"); |
|
250 break; |
|
251 |
|
252 default: |
|
253 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n"); |
|
254 } // switch |
|
255 } |
|
256 |
|
257 private final int getMethodNumber(String str) { |
|
258 for(int i=0; i < methods.length; i++) { |
|
259 String cmp = methods[i].getName() + methods[i].getSignature(); |
|
260 if(cmp.equals(str)) |
|
261 return i; |
|
262 } |
|
263 return -1; |
|
264 } |
|
265 } |