1 2 package com.sun.org.apache.bcel.internal.util; 3 4 57 58 import com.sun.org.apache.bcel.internal.classfile.*; 59 import java.io.*; 60 61 68 final class ConstantHTML implements com.sun.org.apache.bcel.internal.Constants { 69 private String class_name; private String class_package; private ConstantPool constant_pool; private PrintWriter file; private String [] constant_ref; private Constant[] constants; private Method[] methods; 76 77 ConstantHTML(String dir, String class_name, String class_package, Method[] methods, 78 ConstantPool constant_pool) throws IOException 79 { 80 this.class_name = class_name; 81 this.class_package = class_package; 82 this.constant_pool = constant_pool; 83 this.methods = methods; 84 constants = constant_pool.getConstantPool(); 85 file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html")); 86 constant_ref = new String [constants.length]; 87 constant_ref[0] = "<unknown>"; 88 89 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); 90 91 for(int i=1; i < constants.length; i++) { 93 if(i % 2 == 0) 94 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); 95 else 96 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); 97 98 if(constants[i] != null) 99 writeConstant(i); 100 101 file.print("</TD></TR>\n"); 102 } 103 104 file.println("</TABLE></BODY></HTML>"); 105 file.close(); 106 } 107 108 String referenceConstant(int index) { 109 return constant_ref[index]; 110 } 111 112 private void writeConstant(int index) { 113 byte tag = constants[index].getTag(); 114 int class_index, name_index; 115 String ref; 116 117 file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>"); 119 120 122 switch(tag) { 123 case CONSTANT_InterfaceMethodref: 124 case CONSTANT_Methodref: 125 if(tag == CONSTANT_Methodref) { 127 ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref); 128 class_index = c.getClassIndex(); 129 name_index = c.getNameAndTypeIndex(); 130 } 131 else { 132 ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref); 133 class_index = c1.getClassIndex(); 134 name_index = c1.getNameAndTypeIndex(); 135 } 136 137 String method_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType); 139 String html_method_name = Class2HTML.toHTML(method_name); 140 141 String method_class = constant_pool.constantToString(class_index, CONSTANT_Class); 143 String short_method_class = Utility.compactClassName(method_class); short_method_class = Utility.compactClassName(method_class); short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); 147 ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType); 149 String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8); 150 String [] args = Utility.methodSignatureArgumentTypes(signature, false); 152 153 String type = Utility.methodSignatureReturnType(signature, false); 155 String ret_type = Class2HTML.referenceType(type); 156 157 StringBuffer buf = new StringBuffer ("("); 158 for(int i=0; i < args.length; i++) { 159 buf.append(Class2HTML.referenceType(args[i])); 160 if(i < args.length - 1) 161 buf.append(", "); 162 } 163 buf.append(")"); 164 165 String arg_types = buf.toString(); 166 167 if(method_class.equals(class_name)) ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) + 169 "\" TARGET=Code>" + html_method_name + "</A>"; 170 else 171 ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class + 172 "</A>." + html_method_name; 173 174 constant_ref[index] = ret_type + " <A HREF=\"" + class_name + "_cp.html#cp" + class_index + 175 "\" TARGET=Constants>" + 176 short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + 177 index + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types; 178 179 file.println("<P><TT>" + ret_type + " " + ref + arg_types + " </TT>\n<UL>" + 180 "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index + ")</A>\n" + 181 "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>"); 182 break; 183 184 case CONSTANT_Fieldref: 185 ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref); 187 class_index = c3.getClassIndex(); 188 name_index = c3.getNameAndTypeIndex(); 189 190 String field_class = constant_pool.constantToString(class_index, CONSTANT_Class); 192 String short_field_class = Utility.compactClassName(field_class); short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); 195 String field_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType); 196 197 if(field_class.equals(class_name)) ref = "<A HREF=\"" + field_class + "_methods.html#field" + 199 field_name + "\" TARGET=Methods>" + field_name + "</A>"; 200 else 201 ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + 202 short_field_class + "</A>." + field_name + "\n"; 203 204 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index + "\" TARGET=Constants>" + 205 short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + 206 index + "\" TARGET=ConstantPool>" + field_name + "</A>"; 207 208 file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + 209 "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index + ")</A><BR>\n" + 210 "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>"); 211 break; 212 213 case CONSTANT_Class: 214 ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class); 215 name_index = c4.getNameIndex(); 216 String class_name2 = constant_pool.constantToString(index, tag); String short_class_name = Utility.compactClassName(class_name2); short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); 220 ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>"; 221 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index + 222 "\" TARGET=ConstantPool>" + short_class_name + "</A>"; 223 224 file.println("<P><TT>" + ref + "</TT><UL>" + 225 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); 226 break; 227 228 case CONSTANT_String: 229 ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String); 230 name_index = c5.getStringIndex(); 231 232 String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag)); 233 234 file.println("<P><TT>" + str + "</TT><UL>" + 235 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n"); 236 break; 237 238 case CONSTANT_NameAndType: 239 ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType); 240 name_index = c6.getNameIndex(); 241 int signature_index = c6.getSignatureIndex(); 242 243 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" + 244 "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" + 245 "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" + 246 signature_index + ")</A></UL>\n"); 247 break; 248 249 default: 250 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n"); 251 } } 253 254 private final int getMethodNumber(String str) { 255 for(int i=0; i < methods.length; i++) { 256 String cmp = methods[i].getName() + methods[i].getSignature(); 257 if(cmp.equals(str)) 258 return i; 259 } 260 return -1; 261 } 262 } 263 | Popular Tags |