• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 package org.apache.bcel.util;
19 
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.PrintWriter;
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.ConstantValue;
28 import org.apache.bcel.classfile.ExceptionTable;
29 import org.apache.bcel.classfile.Field;
30 import org.apache.bcel.classfile.Method;
31 import org.apache.bcel.classfile.Utility;
32 
33 /**
34  * Convert methods and fields into HTML file.
35  *
36  * @version $Id$
37  *
38  */
39 final class MethodHTML {
40 
41     private final String class_name; // name of current class
42     private final PrintWriter file; // file to write to
43     private final ConstantHTML constant_html;
44     private final AttributeHTML attribute_html;
45 
46 
MethodHTML(final String dir, final String class_name, final Method[] methods, final Field[] fields, final ConstantHTML constant_html, final AttributeHTML attribute_html)47     MethodHTML(final String dir, final String class_name, final Method[] methods, final Field[] fields,
48             final ConstantHTML constant_html, final AttributeHTML attribute_html) throws IOException {
49         this.class_name = class_name;
50         this.attribute_html = attribute_html;
51         this.constant_html = constant_html;
52         file = new PrintWriter(new FileOutputStream(dir + class_name + "_methods.html"));
53         file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
54         file.println("<TR><TH ALIGN=LEFT>Access&nbsp;flags</TH><TH ALIGN=LEFT>Type</TH>"
55                 + "<TH ALIGN=LEFT>Field&nbsp;name</TH></TR>");
56         for (final Field field : fields) {
57             writeField(field);
58         }
59         file.println("</TABLE>");
60         file.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>"
62                 + "<TH ALIGN=LEFT>Arguments</TH></TR>");
63         for (int i = 0; i < methods.length; i++) {
64             writeMethod(methods[i], i);
65         }
66         file.println("</TABLE></BODY></HTML>");
67         file.close();
68     }
69 
70 
71     /**
72      * Print field of class.
73      *
74      * @param field field to print
75      * @throws java.io.IOException
76      */
writeField( final Field field )77     private void writeField( final Field field ) throws IOException {
78         final String type = Utility.signatureToString(field.getSignature());
79         final String name = field.getName();
80         String access = Utility.accessToString(field.getAccessFlags());
81         Attribute[] attributes;
82         access = Utility.replace(access, " ", "&nbsp;");
83         file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>"
84                 + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name
85                 + "</A></TD>");
86         attributes = field.getAttributes();
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() == Const.ATTR_CONSTANT_VALUE) { // Default value
93                 final String str = ((ConstantValue) attributes[i]).toString();
94                 // Reference attribute in _attributes.html
95                 file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" + name + "@" + i
96                         + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
97                 break;
98             }
99         }
100         file.println("</TR>");
101     }
102 
103 
writeMethod( final Method method, final int method_number )104     private void writeMethod( final Method method, final int method_number ) {
105         // Get raw signature
106         final String signature = method.getSignature();
107         // Get array of strings containing the argument types
108         final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
109         // Get return type string
110         final String type = Utility.methodSignatureReturnType(signature, false);
111         // Get method name
112         final String name = method.getName();
113         String 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         final Attribute[] attributes = method.getAttributes();
118         /* HTML doesn't like names like <clinit> and spaces are places to break
119          * lines. Both we don't want...
120          */
121         access = Utility.replace(access, " ", "&nbsp;");
122         html_name = Class2HTML.toHTML(name);
123         file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number
124                 + ">" + access + "</A></FONT></TD>");
125         file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + class_name
126                 + "_code.html#method" + method_number + " TARGET=Code>" + html_name
127                 + "</A></TD>\n<TD>(");
128         for (int i = 0; i < args.length; i++) {
129             file.print(Class2HTML.referenceType(args[i]));
130             if (i < args.length - 1) {
131                 file.print(", ");
132             }
133         }
134         file.print(")</TD></TR>");
135         // Check for thrown exceptions
136         for (int i = 0; i < attributes.length; i++) {
137             attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
138                     method_number);
139             final byte tag = attributes[i].getTag();
140             if (tag == Const.ATTR_EXCEPTIONS) {
141                 file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
142                 final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
143                 for (int j = 0; j < exceptions.length; j++) {
144                     file.print(constant_html.referenceConstant(exceptions[j]));
145                     if (j < exceptions.length - 1) {
146                         file.print(", ");
147                     }
148                 }
149                 file.println("</TD></TR>");
150             } else if (tag == Const.ATTR_CODE) {
151                 final Attribute[] c_a = ((Code) attributes[i]).getAttributes();
152                 for (int j = 0; j < c_a.length; j++) {
153                     attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
154                             + j, method_number);
155                 }
156             }
157         }
158     }
159 }
160