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 methods and fields into HTML file.
  59. *
  60. * @version $Id: MethodHTML.java,v 1.1.1.1 2001/10/29 20:00:31 jvanzyl Exp $
  61. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  62. *
  63. */
  64. final class MethodHTML 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 ConstantHTML constant_html;
  68. private AttributeHTML attribute_html;
  69. MethodHTML(String dir, String class_name,
  70. Method[] methods, Field[] fields,
  71. ConstantHTML constant_html, AttributeHTML attribute_html) throws IOException
  72. {
  73. this.class_name = class_name;
  74. this.attribute_html = attribute_html;
  75. this.constant_html = constant_html;
  76. file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));
  77. file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
  78. file.println("<TR><TH ALIGN=LEFT>Access flags</TH><TH ALIGN=LEFT>Type</TH>" +
  79. "<TH ALIGN=LEFT>Field name</TH></TR>");
  80. for(int i=0; i < fields.length; i++)
  81. writeField(fields[i]);
  82. file.println("</TABLE>");
  83. file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access flags</TH>" +
  84. "<TH ALIGN=LEFT>Return type</TH><TH ALIGN=LEFT>Method name</TH>" +
  85. "<TH ALIGN=LEFT>Arguments</TH></TR>");
  86. for(int i=0; i < methods.length; i++)
  87. writeMethod(methods[i], i);
  88. file.println("</TABLE></BODY></HTML>");
  89. file.close();
  90. }
  91. /**
  92. * Print field of class.
  93. *
  94. * @param field field to print
  95. * @exception java.io.IOException
  96. */
  97. private void writeField(Field field) throws IOException {
  98. String type = Utility.signatureToString(field.getSignature());
  99. String name = field.getName();
  100. String access = Utility.accessToString(field.getAccessFlags());
  101. Attribute[] attributes;
  102. access = Utility.replace(access, " ", " ");
  103. file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" +
  104. Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" +
  105. name + "</A></TD>");
  106. attributes = field.getAttributes();
  107. // Write them to the Attributes.html file with anchor "<name>[<i>]"
  108. for(int i=0; i < attributes.length; i++)
  109. attribute_html.writeAttribute(attributes[i], name + "@" + i);
  110. for(int i=0; i < attributes.length; i++) {
  111. if(attributes[i].getTag() == ATTR_CONSTANT_VALUE) { // Default value
  112. String str = ((ConstantValue)attributes[i]).toString();
  113. // Reference attribute in _attributes.html
  114. file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" +
  115. name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
  116. break;
  117. }
  118. }
  119. file.println("</TR>");
  120. }
  121. private final void writeMethod(Method method, int method_number) throws IOException {
  122. // Get raw signature
  123. String signature = method.getSignature();
  124. // Get array of strings containing the argument types
  125. String[] args = Utility.methodSignatureArgumentTypes(signature, false);
  126. // Get return type string
  127. String type = Utility.methodSignatureReturnType(signature, false);
  128. // Get method name
  129. String name = method.getName(), html_name;
  130. // Get method's access flags
  131. String access = Utility.accessToString(method.getAccessFlags());
  132. // Get the method's attributes, the Code Attribute in particular
  133. Attribute[] attributes = method.getAttributes();
  134. /* HTML doesn't like names like <clinit> and spaces are places to break
  135. * lines. Both we don't want...
  136. */
  137. access = Utility.replace(access, " ", " ");
  138. html_name = Class2HTML.toHTML(name);
  139. file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number + ">" +
  140. access + "</A></FONT></TD>");
  141. file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" +
  142. "<A HREF=" + class_name + "_code.html#method" + method_number +
  143. " TARGET=Code>" + html_name + "</A></TD>\n<TD>(");
  144. for(int i=0; i < args.length; i++) {
  145. file.print(Class2HTML.referenceType(args[i]));
  146. if(i < args.length - 1)
  147. file.print(", ");
  148. }
  149. file.print(")</TD></TR>");
  150. // Check for thrown exceptions
  151. for(int i=0; i < attributes.length; i++) {
  152. attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
  153. method_number);
  154. byte tag = attributes[i].getTag();
  155. if(tag == ATTR_EXCEPTIONS) {
  156. file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
  157. int[] exceptions = ((ExceptionTable)attributes[i]).getExceptionIndexTable();
  158. for(int j=0; j < exceptions.length; j++) {
  159. file.print(constant_html.referenceConstant(exceptions[j]));
  160. if(j < exceptions.length - 1)
  161. file.print(", ");
  162. }
  163. file.println("</TD></TR>");
  164. } else if(tag == ATTR_CODE) {
  165. Attribute[] c_a = ((Code)attributes[i]).getAttributes();
  166. for(int j=0; j < c_a.length; j++)
  167. attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@" + j,
  168. method_number);
  169. }
  170. }
  171. }
  172. }