KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > util > AttributeHTML


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 JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import org.apache.bcel.classfile.Attribute;
23 import org.apache.bcel.classfile.Code;
24 import org.apache.bcel.classfile.CodeException;
25 import org.apache.bcel.classfile.ConstantPool;
26 import org.apache.bcel.classfile.ConstantUtf8;
27 import org.apache.bcel.classfile.ConstantValue;
28 import org.apache.bcel.classfile.ExceptionTable;
29 import org.apache.bcel.classfile.InnerClass;
30 import org.apache.bcel.classfile.InnerClasses;
31 import org.apache.bcel.classfile.LineNumber;
32 import org.apache.bcel.classfile.LineNumberTable;
33 import org.apache.bcel.classfile.LocalVariable;
34 import org.apache.bcel.classfile.LocalVariableTable;
35 import org.apache.bcel.classfile.SourceFile;
36 import org.apache.bcel.classfile.Utility;
37
38 /**
39  * Convert found attributes into HTML file.
40  *
41  * @version $Id: AttributeHTML.java 386056 2006-03-15 11:31:56Z tcurdt $
42  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
43  *
44  */

45 final class AttributeHTML implements org.apache.bcel.Constants {
46
47     private String JavaDoc class_name; // name of current class
48
private PrintWriter JavaDoc file; // file to write to
49
private int attr_count = 0;
50     private ConstantHTML constant_html;
51     private ConstantPool constant_pool;
52
53
54     AttributeHTML(String JavaDoc dir, String JavaDoc class_name, ConstantPool constant_pool,
55             ConstantHTML constant_html) throws IOException JavaDoc {
56         this.class_name = class_name;
57         this.constant_pool = constant_pool;
58         this.constant_html = constant_html;
59         file = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(dir + class_name + "_attributes.html"));
60         file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
61     }
62
63
64     private final String JavaDoc codeLink( int link, int method_number ) {
65         return "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + link
66                 + "\" TARGET=Code>" + link + "</A>";
67     }
68
69
70     final void close() {
71         file.println("</TABLE></BODY></HTML>");
72         file.close();
73     }
74
75
76     final void writeAttribute( Attribute attribute, String JavaDoc anchor ) throws IOException JavaDoc {
77         writeAttribute(attribute, anchor, 0);
78     }
79
80
81     final void writeAttribute( Attribute attribute, String JavaDoc anchor, int method_number )
82             throws IOException JavaDoc {
83         byte tag = attribute.getTag();
84         int index;
85         if (tag == ATTR_UNKNOWN) {
86             return;
87         }
88         attr_count++; // Increment number of attributes found so far
89
if (attr_count % 2 == 0) {
90             file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
91         } else {
92             file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
93         }
94         file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag]
95                 + "</A></H4>");
96         /* Handle different attributes
97          */

98         switch (tag) {
99             case ATTR_CODE:
100                 Code c = (Code) attribute;
101                 // Some directly printable values
102
file.print("<UL><LI>Maximum stack size = " + c.getMaxStack()
103                         + "</LI>\n<LI>Number of local variables = " + c.getMaxLocals()
104                         + "</LI>\n<LI><A HREF=\"" + class_name + "_code.html#method"
105                         + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
106                 // Get handled exceptions and list them
107
CodeException[] ce = c.getExceptionTable();
108                 int len = ce.length;
109                 if (len > 0) {
110                     file.print("<P><B>Exceptions handled</B><UL>");
111                     for (int i = 0; i < len; i++) {
112                         int catch_type = ce[i].getCatchType(); // Index in constant pool
113
file.print("<LI>");
114                         if (catch_type != 0) {
115                             file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
116
} else {
117                             file.print("Any Exception");
118                         }
119                         file.print("<BR>(Ranging from lines "
120                                 + codeLink(ce[i].getStartPC(), method_number) + " to "
121                                 + codeLink(ce[i].getEndPC(), method_number) + ", handled at line "
122                                 + codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
123                     }
124                     file.print("</UL>");
125                 }
126                 break;
127             case ATTR_CONSTANT_VALUE:
128                 index = ((ConstantValue) attribute).getConstantValueIndex();
129                 // Reference _cp.html
130
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index
131                         + "\" TARGET=\"ConstantPool\">Constant value index(" + index
132                         + ")</A></UL>\n");
133                 break;
134             case ATTR_SOURCE_FILE:
135                 index = ((SourceFile) attribute).getSourceFileIndex();
136                 // Reference _cp.html
137
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index
138                         + "\" TARGET=\"ConstantPool\">Source file index(" + index + ")</A></UL>\n");
139                 break;
140             case ATTR_EXCEPTIONS:
141                 // List thrown exceptions
142
int[] indices = ((ExceptionTable) attribute).getExceptionIndexTable();
143                 file.print("<UL>");
144                 for (int i = 0; i < indices.length; i++) {
145                     file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i]
146                             + "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i]
147                             + ")</A>\n");
148                 }
149                 file.print("</UL>\n");
150                 break;
151             case ATTR_LINE_NUMBER_TABLE:
152                 LineNumber[] line_numbers = ((LineNumberTable) attribute).getLineNumberTable();
153                 // List line number pairs
154
file.print("<P>");
155                 for (int i = 0; i < line_numbers.length; i++) {
156                     file.print("(" + line_numbers[i].getStartPC() + ",&nbsp;"
157                             + line_numbers[i].getLineNumber() + ")");
158                     if (i < line_numbers.length - 1) {
159                         file.print(", "); // breakable
160
}
161                 }
162                 break;
163             case ATTR_LOCAL_VARIABLE_TABLE:
164                 LocalVariable[] vars = ((LocalVariableTable) attribute).getLocalVariableTable();
165                 // List name, range and type
166
file.print("<UL>");
167                 for (int i = 0; i < vars.length; i++) {
168                     index = vars[i].getSignatureIndex();
169                     String JavaDoc signature = ((ConstantUtf8) constant_pool.getConstant(index,
170                             CONSTANT_Utf8)).getBytes();
171                     signature = Utility.signatureToString(signature, false);
172                     int start = vars[i].getStartPC();
173                     int end = (start + vars[i].getLength());
174                     file.println("<LI>" + Class2HTML.referenceType(signature) + "&nbsp;<B>"
175                             + vars[i].getName() + "</B> in slot %" + vars[i].getIndex()
176                             + "<BR>Valid from lines " + "<A HREF=\"" + class_name
177                             + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>"
178                             + start + "</A> to " + "<A HREF=\"" + class_name + "_code.html#code"
179                             + method_number + "@" + end + "\" TARGET=Code>" + end + "</A></LI>");
180                 }
181                 file.print("</UL>\n");
182                 break;
183             case ATTR_INNER_CLASSES:
184                 InnerClass[] classes = ((InnerClasses) attribute).getInnerClasses();
185                 // List inner classes
186
file.print("<UL>");
187                 for (int i = 0; i < classes.length; i++) {
188                     String JavaDoc name, access;
189                     index = classes[i].getInnerNameIndex();
190                     if (index > 0) {
191                         name = ((ConstantUtf8) constant_pool.getConstant(index, CONSTANT_Utf8))
192                                 .getBytes();
193                     } else {
194                         name = "&lt;anonymous&gt;";
195                     }
196                     access = Utility.accessToString(classes[i].getInnerAccessFlags());
197                     file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "
198                             + constant_html.referenceConstant(classes[i].getInnerClassIndex())
199                             + " in&nbsp;class "
200                             + constant_html.referenceConstant(classes[i].getOuterClassIndex())
201                             + " named " + name + "</LI>\n");
202                 }
203                 file.print("</UL>\n");
204                 break;
205             default: // Such as Unknown attribute or Deprecated
206
file.print("<P>" + attribute.toString());
207         }
208         file.println("</TD></TR>");
209         file.flush();
210     }
211 }
212
Popular Tags