KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > util > AttributeHTML


1 package com.sun.org.apache.bcel.internal.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.classfile.*;
58 import java.io.*;
59
60 /**
61  * Convert found attributes into HTML file.
62  *
63  * @version $Id: AttributeHTML.java,v 1.1.1.1 2001/10/29 20:00:28 jvanzyl Exp $
64  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
65  *
66  */

67 final class AttributeHTML implements com.sun.org.apache.bcel.internal.Constants {
68   private String JavaDoc class_name; // name of current class
69
private PrintWriter file; // file to write to
70
private int attr_count = 0;
71   private ConstantHTML constant_html;
72   private ConstantPool constant_pool;
73
74   AttributeHTML(String JavaDoc dir, String JavaDoc class_name, ConstantPool constant_pool,
75         ConstantHTML constant_html) throws IOException
76   {
77     this.class_name = class_name;
78     this.constant_pool = constant_pool;
79     this.constant_html = constant_html;
80
81     file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
82     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
83   }
84
85   private final String JavaDoc codeLink(int link, int method_number) {
86     return "<A HREF=\"" + class_name + "_code.html#code" +
87       method_number + "@" + link + "\" TARGET=Code>" +
88       link + "</A>";
89   }
90     
91   final void close() {
92     file.println("</TABLE></BODY></HTML>");
93     file.close();
94   }
95
96   final void writeAttribute(Attribute attribute, String JavaDoc anchor) throws IOException {
97     writeAttribute(attribute, anchor, 0);
98   }
99
100   final void writeAttribute(Attribute attribute, String JavaDoc anchor, int method_number) throws IOException {
101     byte tag = attribute.getTag();
102     int index;
103  
104     if(tag == ATTR_UNKNOWN) // Don't know what to do about this one
105
return;
106
107     attr_count++; // Increment number of attributes found so far
108

109     if(attr_count % 2 == 0)
110       file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
111     else
112       file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
113
114     file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>");
115
116     /* Handle different attributes
117      */

118     switch(tag) {
119     case ATTR_CODE:
120       Code c = (Code)attribute;
121       Attribute[] attributes = c.getAttributes();
122
123       // Some directly printable values
124
file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() +
125          "</LI>\n<LI>Number of local variables = " +
126          c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
127          "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
128
129       // Get handled exceptions and list them
130
CodeException[] ce = c.getExceptionTable();
131       int len = ce.length;
132
133       if(len > 0) {
134     file.print("<P><B>Exceptions handled</B><UL>");
135
136     for(int i=0; i < len; i++) {
137       int catch_type = ce[i].getCatchType(); // Index in constant pool
138

139       file.print("<LI>");
140
141       if(catch_type != 0)
142         file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
143
else
144         file.print("Any Exception");
145
146       file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
147              " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " +
148              codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
149     }
150     file.print("</UL>");
151       }
152       break;
153
154     case ATTR_CONSTANT_VALUE:
155       index = ((ConstantValue)attribute).getConstantValueIndex();
156
157       // Reference _cp.html
158
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
159          "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
160       break;
161
162     case ATTR_SOURCE_FILE:
163       index = ((SourceFile)attribute).getSourceFileIndex();
164
165       // Reference _cp.html
166
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
167          "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
168       break;
169       
170     case ATTR_EXCEPTIONS:
171       // List thrown exceptions
172
int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
173
174       file.print("<UL>");
175
176       for(int i=0; i < indices.length; i++)
177     file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
178            "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
179
180       file.print("</UL>\n");
181       break;
182
183     case ATTR_LINE_NUMBER_TABLE:
184       LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
185
186       // List line number pairs
187
file.print("<P>");
188
189       for(int i=0; i < line_numbers.length; i++) {
190     file.print("(" + line_numbers[i].getStartPC() + ",&nbsp;" + line_numbers[i].getLineNumber() + ")");
191
192     if(i < line_numbers.length - 1)
193       file.print(", "); // breakable
194
}
195       break;
196
197     case ATTR_LOCAL_VARIABLE_TABLE:
198       LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
199
200       // List name, range and type
201
file.print("<UL>");
202
203       for(int i=0; i < vars.length; i++) {
204     index = vars[i].getSignatureIndex();
205     String JavaDoc signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
206     signature = Utility.signatureToString(signature, false);
207     int start = vars[i].getStartPC();
208     int end = (start + vars[i].getLength());
209
210     file.println("<LI>" + Class2HTML.referenceType(signature) +
211              "&nbsp;<B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
212              "<BR>Valid from lines " +
213              "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
214              start + "</A> to " +
215              "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
216              end + "</A></LI>");
217       }
218       file.print("</UL>\n");
219
220       break;
221
222     case ATTR_INNER_CLASSES:
223       InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
224
225       // List inner classes
226
file.print("<UL>");
227
228       for(int i=0; i < classes.length; i++) {
229     String JavaDoc name, access;
230     
231     index = classes[i].getInnerNameIndex();
232     if(index > 0)
233       name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
234     else
235       name = "&lt;anonymous&gt;";
236
237     access = Utility.accessToString(classes[i].getInnerAccessFlags());
238
239     file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
240            constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
241            " in&nbsp;class " +
242            constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
243            " named " + name + "</LI>\n");
244       }
245
246       file.print("</UL>\n");
247       break;
248
249     default: // Such as Unknown attribute or Deprecated
250
file.print("<P>" + attribute.toString());
251     }
252
253     file.println("</TD></TR>");
254     file.flush();
255   }
256 }
257
Popular Tags