1/* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5/* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22package com.sun.org.apache.bcel.internal.util; 23 24import java.io.FileOutputStream; 25import java.io.IOException; 26import java.io.PrintWriter; 27 28import com.sun.org.apache.bcel.internal.Const; 29import com.sun.org.apache.bcel.internal.classfile.Constant; 30import com.sun.org.apache.bcel.internal.classfile.ConstantClass; 31import com.sun.org.apache.bcel.internal.classfile.ConstantFieldref; 32import com.sun.org.apache.bcel.internal.classfile.ConstantInterfaceMethodref; 33import com.sun.org.apache.bcel.internal.classfile.ConstantMethodref; 34import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType; 35import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 36import com.sun.org.apache.bcel.internal.classfile.ConstantString; 37import com.sun.org.apache.bcel.internal.classfile.Method; 38import com.sun.org.apache.bcel.internal.classfile.Utility; 39 40/** 41 * Convert constant pool into HTML file. 42 * 43 * @version $Id: ConstantHTML.java 1749603 2016-06-21 20:50:19Z ggregory $ 44 * 45 */ 46final class ConstantHTML { 47 48 private final String class_name; // name of current class 49 private final String class_package; // name of package 50 private final ConstantPool constant_pool; // reference to constant pool 51 private final PrintWriter file; // file to write to 52 private final String[] constant_ref; // String to return for cp[i] 53 private final Constant[] constants; // The constants in the cp 54 private final Method[] methods; 55 56 57 ConstantHTML(final String dir, final String class_name, final String class_package, 58 final Method[] methods, final ConstantPool constant_pool) throws IOException { 59 this.class_name = class_name; 60 this.class_package = class_package; 61 this.constant_pool = constant_pool; 62 this.methods = methods; 63 constants = constant_pool.getConstantPool(); 64 file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html")); 65 constant_ref = new String[constants.length]; 66 constant_ref[0] = "<unknown>"; 67 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); 68 // Loop through constants, constants[0] is reserved 69 for (int i = 1; i < constants.length; i++) { 70 if (i % 2 == 0) { 71 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); 72 } else { 73 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); 74 } 75 if (constants[i] != null) { 76 writeConstant(i); 77 } 78 file.print("</TD></TR>\n"); 79 } 80 file.println("</TABLE></BODY></HTML>"); 81 file.close(); 82 } 83 84 85 String referenceConstant( final int index ) { 86 return constant_ref[index]; 87 } 88 89 90 private void writeConstant( final int index ) { 91 final byte tag = constants[index].getTag(); 92 int class_index; 93 int name_index; 94 String ref; 95 // The header is always the same 96 file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag) 97 + "</H4>"); 98 /* For every constant type get the needed parameters and print them appropiately 99 */ 100 switch (tag) { 101 case Const.CONSTANT_InterfaceMethodref: 102 case Const.CONSTANT_Methodref: 103 // Get class_index and name_and_type_index, depending on type 104 if (tag == Const.CONSTANT_Methodref) { 105 final ConstantMethodref c = (ConstantMethodref) constant_pool.getConstant(index, 106 Const.CONSTANT_Methodref); 107 class_index = c.getClassIndex(); 108 name_index = c.getNameAndTypeIndex(); 109 } else { 110 final ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref) constant_pool 111 .getConstant(index, Const.CONSTANT_InterfaceMethodref); 112 class_index = c1.getClassIndex(); 113 name_index = c1.getNameAndTypeIndex(); 114 } 115 // Get method name and its class 116 final String method_name = constant_pool.constantToString(name_index, 117 Const.CONSTANT_NameAndType); 118 final String html_method_name = Class2HTML.toHTML(method_name); 119 // Partially compacted class name, i.e., / -> . 120 final String method_class = constant_pool.constantToString(class_index, Const.CONSTANT_Class); 121 String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang. 122 short_method_class = Utility.compactClassName(short_method_class, class_package 123 + ".", true); // Remove class package prefix 124 // Get method signature 125 final ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant( 126 name_index, Const.CONSTANT_NameAndType); 127 final String signature = constant_pool.constantToString(c2.getSignatureIndex(), 128 Const.CONSTANT_Utf8); 129 // Get array of strings containing the argument types 130 final String[] args = Utility.methodSignatureArgumentTypes(signature, false); 131 // Get return type string 132 final String type = Utility.methodSignatureReturnType(signature, false); 133 final String ret_type = Class2HTML.referenceType(type); 134 final StringBuilder buf = new StringBuilder("("); 135 for (int i = 0; i < args.length; i++) { 136 buf.append(Class2HTML.referenceType(args[i])); 137 if (i < args.length - 1) { 138 buf.append(", "); 139 } 140 } 141 buf.append(")"); 142 final String arg_types = buf.toString(); 143 if (method_class.equals(class_name)) { 144 ref = "<A HREF=\"" + class_name + "_code.html#method" 145 + getMethodNumber(method_name + signature) + "\" TARGET=Code>" 146 + html_method_name + "</A>"; 147 } else { 148 ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" 149 + short_method_class + "</A>." + html_method_name; 150 } 151 constant_ref[index] = ret_type + " <A HREF=\"" + class_name + "_cp.html#cp" 152 + class_index + "\" TARGET=Constants>" + short_method_class 153 + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + index 154 + "\" TARGET=ConstantPool>" + html_method_name + "</A> " + arg_types; 155 file.println("<P><TT>" + ret_type + " " + ref + arg_types 156 + " </TT>\n<UL>" + "<LI><A HREF=\"#cp" + class_index 157 + "\">Class index(" + class_index + ")</A>\n" + "<LI><A HREF=\"#cp" 158 + name_index + "\">NameAndType index(" + name_index + ")</A></UL>"); 159 break; 160 case Const.CONSTANT_Fieldref: 161 // Get class_index and name_and_type_index 162 final ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index, 163 Const.CONSTANT_Fieldref); 164 class_index = c3.getClassIndex(); 165 name_index = c3.getNameAndTypeIndex(); 166 // Get method name and its class (compacted) 167 final String field_class = constant_pool.constantToString(class_index, Const.CONSTANT_Class); 168 String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang. 169 short_field_class = Utility.compactClassName(short_field_class, 170 class_package + ".", true); // Remove class package prefix 171 final String field_name = constant_pool 172 .constantToString(name_index, Const.CONSTANT_NameAndType); 173 if (field_class.equals(class_name)) { 174 ref = "<A HREF=\"" + field_class + "_methods.html#field" + field_name 175 + "\" TARGET=Methods>" + field_name + "</A>"; 176 } else { 177 ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + short_field_class 178 + "</A>." + field_name + "\n"; 179 } 180 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index 181 + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\"" 182 + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" 183 + field_name + "</A>"; 184 file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp" 185 + class_index + "\">Class(" + class_index + ")</A><BR>\n" 186 + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index 187 + ")</A></UL>"); 188 break; 189 case Const.CONSTANT_Class: 190 final ConstantClass c4 = (ConstantClass) constant_pool.getConstant(index, Const.CONSTANT_Class); 191 name_index = c4.getNameIndex(); 192 final String class_name2 = constant_pool.constantToString(index, tag); // / -> . 193 String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang. 194 short_class_name = Utility.compactClassName(short_class_name, class_package + ".", 195 true); // Remove class package prefix 196 ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name 197 + "</A>"; 198 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index 199 + "\" TARGET=ConstantPool>" + short_class_name + "</A>"; 200 file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index 201 + "\">Name index(" + name_index + ")</A></UL>\n"); 202 break; 203 case Const.CONSTANT_String: 204 final ConstantString c5 = (ConstantString) constant_pool.getConstant(index, 205 Const.CONSTANT_String); 206 name_index = c5.getStringIndex(); 207 final String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag)); 208 file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index 209 + "\">Name index(" + name_index + ")</A></UL>\n"); 210 break; 211 case Const.CONSTANT_NameAndType: 212 final ConstantNameAndType c6 = (ConstantNameAndType) constant_pool.getConstant(index, 213 Const.CONSTANT_NameAndType); 214 name_index = c6.getNameIndex(); 215 final int signature_index = c6.getSignatureIndex(); 216 file.println("<P><TT>" 217 + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) 218 + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index(" 219 + name_index + ")</A>\n" + "<LI><A HREF=\"#cp" + signature_index 220 + "\">Signature index(" + signature_index + ")</A></UL>\n"); 221 break; 222 default: 223 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n"); 224 } // switch 225 } 226 227 228 private int getMethodNumber( final String str ) { 229 for (int i = 0; i < methods.length; i++) { 230 final String cmp = methods[i].getName() + methods[i].getSignature(); 231 if (cmp.equals(str)) { 232 return i; 233 } 234 } 235 return -1; 236 } 237} 238