< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/MethodHTML.java

Print this page




   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 
  22 package com.sun.org.apache.bcel.internal.util;
  23 
  24 
  25 import com.sun.org.apache.bcel.internal.classfile.*;
  26 import java.io.*;









  27 
  28 /**
  29  * Convert methods and fields into HTML file.
  30  *
  31  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  32  *
  33  */
  34 final class MethodHTML implements com.sun.org.apache.bcel.internal.Constants {
  35   private String        class_name;     // name of current class
  36   private PrintWriter   file;           // file to write to
  37   private ConstantHTML  constant_html;
  38   private AttributeHTML attribute_html;
  39 
  40   MethodHTML(String dir, String class_name,
  41              Method[] methods, Field[] fields,
  42              ConstantHTML constant_html, AttributeHTML attribute_html) throws IOException
  43   {
  44     this.class_name     = class_name;
  45     this.attribute_html = attribute_html;
  46     this.constant_html  = constant_html;
  47 
  48     file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));
  49 
  50     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
  51     file.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>" +
  52                  "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
  53     for(int i=0; i < fields.length; i++)
  54       writeField(fields[i]);

  55     file.println("</TABLE>");
  56 
  57     file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access&nbsp;flags</TH>" +
  58                  "<TH ALIGN=LEFT>Return&nbsp;type</TH><TH ALIGN=LEFT>Method&nbsp;name</TH>" +
  59                  "<TH ALIGN=LEFT>Arguments</TH></TR>");
  60     for(int i=0; i < methods.length; i++)
  61       writeMethod(methods[i], i);
  62 
  63     file.println("</TABLE></BODY></HTML>");
  64     file.close();
  65   }
  66 

  67   /**
  68    * Print field of class.
  69    *
  70    * @param field field to print
  71    * @exception java.io.IOException
  72    */
  73   private void writeField(Field field) throws IOException {
  74     String       type   = Utility.signatureToString(field.getSignature());
  75     String       name   = field.getName();
  76     String       access = Utility.accessToString(field.getAccessFlags());
  77     Attribute[]  attributes;
  78 
  79     access = Utility.replace(access, " ", "&nbsp;");
  80 
  81     file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>" +
  82                Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" +
  83                name + "</A></TD>");
  84 
  85     attributes = field.getAttributes();
  86 
  87     // Write them to the Attributes.html file with anchor "<name>[<i>]"
  88     for(int i=0; i < attributes.length; i++)
  89       attribute_html.writeAttribute(attributes[i], name + "@" + i);
  90 
  91     for(int i=0; i < attributes.length; i++) {
  92       if(attributes[i].getTag() == ATTR_CONSTANT_VALUE) { // Default value
  93         String str = ((ConstantValue)attributes[i]).toString();
  94 
  95         // Reference attribute in _attributes.html
  96         file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" +
  97                    name + "@" + i + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
  98         break;
  99       }
 100     }
 101 
 102     file.println("</TR>");
 103   }
 104 
 105   private final void writeMethod(Method method, int method_number) throws IOException {

 106     // Get raw signature
 107     String       signature      = method.getSignature();
 108     // Get array of strings containing the argument types
 109     String[]     args           = Utility.methodSignatureArgumentTypes(signature, false);
 110     // Get return type string
 111     String       type           = Utility.methodSignatureReturnType(signature, false);
 112     // Get method name
 113     String       name           = method.getName(), html_name;

 114     // Get method's access flags
 115     String       access         = Utility.accessToString(method.getAccessFlags());
 116     // Get the method's attributes, the Code Attribute in particular
 117     Attribute[]  attributes     = method.getAttributes();
 118 
 119     /* HTML doesn't like names like <clinit> and spaces are places to break
 120      * lines. Both we don't want...
 121      */
 122     access      = Utility.replace(access, " ", "&nbsp;");
 123     html_name   = Class2HTML.toHTML(name);
 124 
 125     file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number + ">" +
 126                access + "</A></FONT></TD>");
 127 
 128     file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" +
 129                "<A HREF=" + class_name + "_code.html#method" + method_number +
 130                " TARGET=Code>" + html_name + "</A></TD>\n<TD>(");
 131 
 132     for(int i=0; i < args.length; i++) {
 133       file.print(Class2HTML.referenceType(args[i]));
 134       if(i < args.length - 1)
 135         file.print(", ");
 136     }
 137 
 138     file.print(")</TD></TR>");
 139 
 140     // Check for thrown exceptions
 141     for(int i=0; i < attributes.length; i++) {
 142       attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
 143                                     method_number);
 144 
 145       byte tag = attributes[i].getTag();
 146       if(tag == ATTR_EXCEPTIONS) {
 147         file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
 148         int[] exceptions = ((ExceptionTable)attributes[i]).getExceptionIndexTable();
 149 
 150         for(int j=0; j < exceptions.length; j++) {
 151           file.print(constant_html.referenceConstant(exceptions[j]));
 152 
 153           if(j < exceptions.length - 1)
 154             file.print(", ");
 155         }

 156         file.println("</TD></TR>");
 157       } else if(tag == ATTR_CODE) {
 158         Attribute[] c_a = ((Code)attributes[i]).getAttributes();
 159 
 160         for(int j=0; j < c_a.length; j++)
 161           attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@" + j,
 162                                         method_number);
 163       }
 164     }
 165   }
 166 }


   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 
  22 package com.sun.org.apache.bcel.internal.util;
  23 
  24 import java.io.FileOutputStream;
  25 import java.io.IOException;
  26 import java.io.PrintWriter;
  27 
  28 import com.sun.org.apache.bcel.internal.Const;
  29 import com.sun.org.apache.bcel.internal.classfile.Attribute;
  30 import com.sun.org.apache.bcel.internal.classfile.Code;
  31 import com.sun.org.apache.bcel.internal.classfile.ConstantValue;
  32 import com.sun.org.apache.bcel.internal.classfile.ExceptionTable;
  33 import com.sun.org.apache.bcel.internal.classfile.Field;
  34 import com.sun.org.apache.bcel.internal.classfile.Method;
  35 import com.sun.org.apache.bcel.internal.classfile.Utility;
  36 
  37 /**
  38  * Convert methods and fields into HTML file.
  39  *
  40  * @version $Id: MethodHTML.java 1749603 2016-06-21 20:50:19Z ggregory $
  41  *
  42  */
  43 final class MethodHTML {
  44 
  45     private final String class_name; // name of current class
  46     private final PrintWriter file; // file to write to
  47     private final ConstantHTML constant_html;
  48     private final AttributeHTML attribute_html;
  49 
  50 
  51     MethodHTML(final String dir, final String class_name, final Method[] methods, final Field[] fields,
  52             final ConstantHTML constant_html, final AttributeHTML attribute_html) throws IOException {
  53         this.class_name = class_name;
  54         this.attribute_html = attribute_html;
  55         this.constant_html = constant_html;

  56         file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));

  57         file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
  58         file.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>"
  59                 + "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
  60         for (final Field field : fields) {
  61             writeField(field);
  62         }
  63         file.println("</TABLE>");
  64         file.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Access&nbsp;flags</TH>"
  65                 + "<TH ALIGN=LEFT>Return&nbsp;type</TH><TH ALIGN=LEFT>Method&nbsp;name</TH>"
  66                 + "<TH ALIGN=LEFT>Arguments</TH></TR>");
  67         for (int i = 0; i < methods.length; i++) {

  68             writeMethod(methods[i], i);
  69         }
  70         file.println("</TABLE></BODY></HTML>");
  71         file.close();
  72     }
  73 
  74 
  75     /**
  76      * Print field of class.
  77      *
  78      * @param field field to print
  79      * @throws java.io.IOException
  80      */
  81     private void writeField( final Field field ) throws IOException {
  82         final String type = Utility.signatureToString(field.getSignature());
  83         final String name = field.getName();
  84         String access = Utility.accessToString(field.getAccessFlags());
  85         Attribute[] attributes;

  86         access = Utility.replace(access, " ", "&nbsp;");
  87         file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>"
  88                 + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name
  89                 + "</A></TD>");


  90         attributes = field.getAttributes();

  91         // Write them to the Attributes.html file with anchor "<name>[<i>]"
  92         for (int i = 0; i < attributes.length; i++) {
  93             attribute_html.writeAttribute(attributes[i], name + "@" + i);
  94         }
  95         for (int i = 0; i < attributes.length; i++) {
  96             if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value
  97                 final String str = ((ConstantValue) attributes[i]).toString();

  98                 // Reference attribute in _attributes.html
  99                 file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" + name + "@" + i
 100                         + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
 101                 break;
 102             }
 103         }

 104         file.println("</TR>");
 105     }
 106 
 107 
 108     private void writeMethod( final Method method, final int method_number ) {
 109         // Get raw signature
 110         final String signature = method.getSignature();
 111         // Get array of strings containing the argument types
 112         final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
 113         // Get return type string
 114         final String type = Utility.methodSignatureReturnType(signature, false);
 115         // Get method name
 116         final String name = method.getName();
 117         String html_name;
 118         // Get method's access flags
 119         String access = Utility.accessToString(method.getAccessFlags());
 120         // Get the method's attributes, the Code Attribute in particular
 121         final Attribute[] attributes = method.getAttributes();

 122         /* HTML doesn't like names like <clinit> and spaces are places to break
 123          * lines. Both we don't want...
 124          */
 125         access = Utility.replace(access, " ", "&nbsp;");
 126         html_name = Class2HTML.toHTML(name);
 127         file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number
 128                 + ">" + access + "</A></FONT></TD>");
 129         file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + class_name
 130                 + "_code.html#method" + method_number + " TARGET=Code>" + html_name
 131                 + "</A></TD>\n<TD>(");
 132         for (int i = 0; i < args.length; i++) {



 133             file.print(Class2HTML.referenceType(args[i]));
 134             if (i < args.length - 1) {
 135                 file.print(", ");
 136             }
 137         }
 138         file.print(")</TD></TR>");

 139         // Check for thrown exceptions
 140         for (int i = 0; i < attributes.length; i++) {
 141             attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
 142                     method_number);
 143             final byte tag = attributes[i].getTag();
 144             if (tag == Const.ATTR_EXCEPTIONS) {

 145                 file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
 146                 final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
 147                 for (int j = 0; j < exceptions.length; j++) {

 148                     file.print(constant_html.referenceConstant(exceptions[j]));
 149                     if (j < exceptions.length - 1) {

 150                         file.print(", ");
 151                     }
 152                 }
 153                 file.println("</TD></TR>");
 154             } else if (tag == Const.ATTR_CODE) {
 155                 final Attribute[] c_a = ((Code) attributes[i]).getAttributes();
 156                 for (int j = 0; j < c_a.length; j++) {
 157                     attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
 158                             + j, method_number);
 159                 }
 160             }
 161         }
 162     }
 163 }
< prev index next >