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 found attributes into HTML file.
  59. *
  60. * @version $Id: AttributeHTML.java,v 1.1.1.1 2001/10/29 20:00:28 jvanzyl Exp $
  61. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  62. *
  63. */
  64. final class AttributeHTML implements com.sun.org.apache.bcel.internal.Constants {
  65. private String class_name; // name of current class
  66. private PrintWriter file; // file to write to
  67. private int attr_count = 0;
  68. private ConstantHTML constant_html;
  69. private ConstantPool constant_pool;
  70. AttributeHTML(String dir, String class_name, ConstantPool constant_pool,
  71. ConstantHTML constant_html) throws IOException
  72. {
  73. this.class_name = class_name;
  74. this.constant_pool = constant_pool;
  75. this.constant_html = constant_html;
  76. file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
  77. file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
  78. }
  79. private final String codeLink(int link, int method_number) {
  80. return "<A HREF=\"" + class_name + "_code.html#code" +
  81. method_number + "@" + link + "\" TARGET=Code>" +
  82. link + "</A>";
  83. }
  84. final void close() {
  85. file.println("</TABLE></BODY></HTML>");
  86. file.close();
  87. }
  88. final void writeAttribute(Attribute attribute, String anchor) throws IOException {
  89. writeAttribute(attribute, anchor, 0);
  90. }
  91. final void writeAttribute(Attribute attribute, String anchor, int method_number) throws IOException {
  92. byte tag = attribute.getTag();
  93. int index;
  94. if(tag == ATTR_UNKNOWN) // Don't know what to do about this one
  95. return;
  96. attr_count++; // Increment number of attributes found so far
  97. if(attr_count % 2 == 0)
  98. file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
  99. else
  100. file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
  101. file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>");
  102. /* Handle different attributes
  103. */
  104. switch(tag) {
  105. case ATTR_CODE:
  106. Code c = (Code)attribute;
  107. Attribute[] attributes = c.getAttributes();
  108. // Some directly printable values
  109. file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() +
  110. "</LI>\n<LI>Number of local variables = " +
  111. c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
  112. "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
  113. // Get handled exceptions and list them
  114. CodeException[] ce = c.getExceptionTable();
  115. int len = ce.length;
  116. if(len > 0) {
  117. file.print("<P><B>Exceptions handled</B><UL>");
  118. for(int i=0; i < len; i++) {
  119. int catch_type = ce[i].getCatchType(); // Index in constant pool
  120. file.print("<LI>");
  121. if(catch_type != 0)
  122. file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
  123. else
  124. file.print("Any Exception");
  125. file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
  126. " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " +
  127. codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
  128. }
  129. file.print("</UL>");
  130. }
  131. break;
  132. case ATTR_CONSTANT_VALUE:
  133. index = ((ConstantValue)attribute).getConstantValueIndex();
  134. // Reference _cp.html
  135. file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
  136. "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
  137. break;
  138. case ATTR_SOURCE_FILE:
  139. index = ((SourceFile)attribute).getSourceFileIndex();
  140. // Reference _cp.html
  141. file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
  142. "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
  143. break;
  144. case ATTR_EXCEPTIONS:
  145. // List thrown exceptions
  146. int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
  147. file.print("<UL>");
  148. for(int i=0; i < indices.length; i++)
  149. file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
  150. "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
  151. file.print("</UL>\n");
  152. break;
  153. case ATTR_LINE_NUMBER_TABLE:
  154. LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
  155. // List line number pairs
  156. file.print("<P>");
  157. for(int i=0; i < line_numbers.length; i++) {
  158. file.print("(" + line_numbers[i].getStartPC() + ", " + line_numbers[i].getLineNumber() + ")");
  159. if(i < line_numbers.length - 1)
  160. file.print(", "); // breakable
  161. }
  162. break;
  163. case ATTR_LOCAL_VARIABLE_TABLE:
  164. LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
  165. // List name, range and type
  166. file.print("<UL>");
  167. for(int i=0; i < vars.length; i++) {
  168. index = vars[i].getSignatureIndex();
  169. String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
  170. signature = Utility.signatureToString(signature, false);
  171. int start = vars[i].getStartPC();
  172. int end = (start + vars[i].getLength());
  173. file.println("<LI>" + Class2HTML.referenceType(signature) +
  174. " <B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
  175. "<BR>Valid from lines " +
  176. "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
  177. start + "</A> to " +
  178. "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
  179. end + "</A></LI>");
  180. }
  181. file.print("</UL>\n");
  182. break;
  183. case ATTR_INNER_CLASSES:
  184. InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
  185. // List inner classes
  186. file.print("<UL>");
  187. for(int i=0; i < classes.length; i++) {
  188. String name, access;
  189. index = classes[i].getInnerNameIndex();
  190. if(index > 0)
  191. name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
  192. else
  193. name = "<anonymous>";
  194. access = Utility.accessToString(classes[i].getInnerAccessFlags());
  195. file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
  196. constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
  197. " in class " +
  198. constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
  199. " named " + name + "</LI>\n");
  200. }
  201. file.print("</UL>\n");
  202. break;
  203. default: // Such as Unknown attribute or Deprecated
  204. file.print("<P>" + attribute.toString());
  205. }
  206. file.println("</TD></TR>");
  207. file.flush();
  208. }
  209. }