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