1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.util;
18
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.PrintWriter;
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 class_name;
43 private String class_package/package-summary.html">ong> String class_package;
44 private ConstantPool constant_pool;
45 private PrintWriter file;
46 private String[] constant_ref;
47 private Constant[] constants;
48 private Method[] methods;
49
50
51 ConstantHTML(String dir, String class_name, String class_package, Method[] methods,/package-summary.html">ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
52 ConstantPool constant_pool) throws IOException {
53 this.class_name = class_name;
54 this.class_package</strong> = class_package;
55 this.constant_pool = constant_pool;
56 this.methods = methods;
57 constants = constant_pool.getConstantPool();
58 file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
59 constant_ref = new String[constants.length];
60 constant_ref[0] = "<unknown>";
61 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
62
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 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 ref;
88
89 file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag]
90 + "</H4>");
91
92
93 switch (tag) {
94 case CONSTANT_InterfaceMethodref:
95 case CONSTANT_Methodref:
96
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
109 String method_name = constant_pool.constantToString(name_index,
110 CONSTANT_NameAndType);
111 String html_method_name = Class2HTML.toHTML(method_name);
112
113 String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
114 String short_method_class = Utility.compactClassName(method_class);
115 short_method_class = Utility.compactClassName(method_class);
116 short_method_g>class = Utility.compactClassName(short_method_class, class_package
117 + ".", true);
118
119 ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(
120 name_index, CONSTANT_NameAndType);
121 String signature = constant_pool.constantToString(c2.getSignatureIndex(),
122 CONSTANT_Utf8);
123
124 String[] args = Utility.methodSignatureArgumentTypes(signature, false);
125
126 String type = Utility.methodSignatureReturnType(signature, false);
127 String ret_type = Class2HTML.referenceType(type);
128 StringBuffer buf = new StringBuffer("(");
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(", ");
133 }
134 }
135 buf.append(")");
136 String 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 + " <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> " + arg_types;
149 file.println("<P><TT>" + ret_type + " " + ref + arg_types
150 + " </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
156 ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index,
157 CONSTANT_Fieldref);
158 class_index = c3.getClassIndex();
159 name_index = c3.getNameAndTypeIndex();
160
161 String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
162 String short_field_class = Utility.compactClassName(field_class);
163 short_field_class = Utility.compactClassName(short_field_class,
164 class_package + ".", true);
165 String 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 class_name2 = constant_pool.constantToString(index, tag);
187 String short_class_name = Utility.compactClassName(class_name2);
188 short_class_name = Utility.compactClassName(short_class_name, class_package + ".",
189 true);
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 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 }
222 }
223
224
225 private final int getMethodNumber( String str ) {
226 for (int i = 0; i < methods.length; i++) {
227 String cmp = methods[i].getName() + methods[i].getSignature();
228 if (cmp.equals(str)) {
229 return i;
230 }
231 }
232 return -1;
233 }
234 }