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