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