View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.util;
    18
    19  import java.io.FileOutputStream;
    20  import java.io.IOException;
    21  import java.io.PrintWriter;
    22  import org.apache.bcel.classfile.Attribute;
    23  import org.apache.bcel.classfile.Code;
    24  import org.apache.bcel.classfile.ConstantValue;
    25  import org.apache.bcel.classfile.ExceptionTable;
    26  import org.apache.bcel.classfile.Field;
    27  import org.apache.bcel.classfile.Method;
    28  import org.apache.bcel.classfile.Utility;
    29
    30  /***
    31   * Convert methods and fields into HTML file.
    32   *
    33   * @version $Id: MethodHTML.java 386056 2006-03-15 11:31:56Z tcurdt $
    34   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    35   *
    36   */
    37  final class MethodHTML implements org.apache.bcel.Constants {
    38
    39      private String class_name; // name of current class
    40      private PrintWriter file; // file to write to
    41      private ConstantHTML constant_html;
    42      private AttributeHTML attribute_html;
    43
    44
    45      MethodHTML(String dir, String class_name, Method[] methods, Field[] fields,
    46              ConstantHTML constant_html, AttributeHTML attribute_html) throws IOException {
    47          this.class_name = class_name;
    48          this.attribute_html = attribute_html;
    49          this.constant_html = constant_html;
    50          file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));
    51          file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
    52          file.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>"
    53                  + "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
    54          for (int i = 0; i < fields.length; i++) {
    55              writeField(fields[i]);
    56          }
    57          file.println("</TABLE>");
    58          file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access&nbsp;flags</TH>"
    59                  + "<TH ALIGN=LEFT>Return&nbsp;type</TH><TH ALIGN=LEFT>Method&nbsp;name</TH>"
    60                  + "<TH ALIGN=LEFT>Arguments</TH></TR>");
    61          for (int i = 0; i < methods.length; i++) {
    62              writeMethod(methods[i], i);
    63          }
    64          file.println("</TABLE></BODY></HTML>");
    65          file.close();
    66      }
    67
    68
    69      /***
    70       * Print field of class.
    71       *
    72       * @param field field to print
    73       * @exception java.io.IOException
    74       */
    75      private void writeField( Field field ) throws IOException {
    76          String type = Utility.signatureToString(field.getSignature());
    77          String name = field.getName();
    78          String access = Utility.accessToString(field.getAccessFlags());
    79          Attribute[] attributes;
    80          access = Utility.replace(access, " ", "&nbsp;");
    81          file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>"
    82                  + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name
    83                  + "</A></TD>");
    84          attributes = field.getAttributes();
    85          // Write them to the Attributes.html file with anchor "<name>[<i>]"
    86          for (int i = 0; i < attributes.length; i++) {
    87              attribute_html.writeAttribute(attributes[i], name + "@" + i);
    88          }
    89          for (int i = 0; i < attributes.length; i++) {
    90              if (attributes[i].getTag() == ATTR_CONSTANT_VALUE) { // Default value
    91                  String str = ((ConstantValue) attributes[i]).toString();
    92                  // Reference attribute in _attributes.html
    93                  file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" + name + "@" + i
    94                          + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
    95                  break;
    96              }
    97          }
    98          file.println("</TR>");
    99      }
    100
    101
    102     private final void writeMethod( Method method, int method_number ) throws IOException {
    103         // Get raw signature
    104         String signature = method.getSignature();
    105         // Get array of strings containing the argument types
    106         String[] args = Utility.methodSignatureArgumentTypes(signature, false);
    107         // Get return type string
    108         String type = Utility.methodSignatureReturnType(signature, false);
    109         // Get method name
    110         String name = method.getName(), html_name;
    111         // Get method's access flags
    112         String access = Utility.accessToString(method.getAccessFlags());
    113         // Get the method's attributes, the Code Attribute in particular
    114         Attribute[] attributes = method.getAttributes();
    115         /* HTML doesn't like names like <clinit> and spaces are places to break
    116          * lines. Both we don't want...
    117          */
    118         access = Utility.replace(access, " ", "&nbsp;");
    119         html_name = Class2HTML.toHTML(name);
    120         file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number
    121                 + ">" + access + "</A></FONT></TD>");
    122         file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + class_name
    123                 + "_code.html#method" + method_number + " TARGET=Code>" + html_name
    124                 + "</A></TD>\n<TD>(");
    125         for (int i = 0; i < args.length; i++) {
    126             file.print(Class2HTML.referenceType(args[i]));
    127             if (i < args.length - 1) {
    128                 file.print(", ");
    129             }
    130         }
    131         file.print(")</TD></TR>");
    132         // Check for thrown exceptions
    133         for (int i = 0; i < attributes.length; i++) {
    134             attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
    135                     method_number);
    136             byte tag = attributes[i].getTag();
    137             if (tag == ATTR_EXCEPTIONS) {
    138                 file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
    139                 int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
    140                 for (int j = 0; j < exceptions.length; j++) {
    141                     file.print(constant_html.referenceConstant(exceptions[j]));
    142                     if (j < exceptions.length - 1) {
    143                         file.print(", ");
    144                     }
    145                 }
    146                 file.println("</TD></TR>");
    147             } else if (tag == ATTR_CODE) {
    148                 Attribute[] c_a = ((Code) attributes[i]).getAttributes();
    149                 for (int j = 0; j < c_a.length; j++) {
    150                     attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
    151                             + j, method_number);
    152                 }
    153             }
    154         }
    155     }
    156 }