1
package org.aspectj.apache.bcel.util;
3
/* ====================================================================
4
* The Apache Software License, Version 1.1
6
* Copyright (c) 2001 The Apache Software Foundation. All rights
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
49
* ====================================================================
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/>.
57
import java.io.FileOutputStream;
58
import java.io.IOException;
59
import java.io.PrintWriter;
61
import org.aspectj.apache.bcel.classfile.Attribute;
62
import org.aspectj.apache.bcel.classfile.Code;
63
import org.aspectj.apache.bcel.classfile.CodeException;
64
import org.aspectj.apache.bcel.classfile.ConstantPool;
65
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
66
import org.aspectj.apache.bcel.classfile.ConstantValue;
67
import org.aspectj.apache.bcel.classfile.ExceptionTable;
68
import org.aspectj.apache.bcel.classfile.InnerClass;
69
import org.aspectj.apache.bcel.classfile.InnerClasses;
70
import org.aspectj.apache.bcel.classfile.LineNumber;
71
import org.aspectj.apache.bcel.classfile.LineNumberTable;
72
import org.aspectj.apache.bcel.classfile.LocalVariable;
73
import org.aspectj.apache.bcel.classfile.LocalVariableTable;
74
import org.aspectj.apache.bcel.classfile.SourceFile;
75
import org.aspectj.apache.bcel.classfile.Utility;
78
* Convert found attributes into HTML file.
80
* @version $Id: AttributeHTML.java,v 1.5 2007/02/15 14:43:16 aclement Exp $
81
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
84
final class AttributeHTML implements org.aspectj.apache.bcel.Constants {
85
private String class_name; // name of current class
86
private PrintWriter file; // file to write to
87
private int attr_count = 0;
88
private ConstantHTML constant_html;
89
private ConstantPool constant_pool;
91
AttributeHTML(String dir, String class_name, ConstantPool constant_pool,
92
ConstantHTML constant_html) throws IOException
94
this.class_name = class_name;
95
this.constant_pool = constant_pool;
96
this.constant_html = constant_html;
98
file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
99
file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
102
private final String codeLink(int link, int method_number) {
103
return "<A HREF=\"" + class_name + "_code.html#code" +
104
method_number + "@" + link + "\" TARGET=Code>" +
109
file.println("</TABLE></BODY></HTML>");
113
final void writeAttribute(Attribute attribute, String anchor) throws IOException {
114
writeAttribute(attribute, anchor, 0);
117
final void writeAttribute(Attribute attribute, String anchor, int method_number) throws IOException {
118
byte tag = attribute.getTag();
121
if(tag == ATTR_UNKNOWN) // Don't know what to do about this one
124
attr_count++; // Increment number of attributes found so far
126
if(attr_count % 2 == 0)
127
file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
129
file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
131
file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>");
133
/* Handle different attributes
137
Code c = (Code)attribute;
139
// Some directly printable values
140
file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() +
141
"</LI>\n<LI>Number of local variables = " +
142
c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
143
"_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
145
// Get handled exceptions and list them
146
CodeException[] ce = c.getExceptionTable();
150
file.print("<P><B>Exceptions handled</B><UL>");
152
for(int i=0; i < len; i++) {
153
int catch_type = ce[i].getCatchType(); // Index in constant pool
158
file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
160
file.print("Any Exception");
162
file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
163
" to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " +
164
codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
170
case ATTR_CONSTANT_VALUE:
171
index = ((ConstantValue)attribute).getConstantValueIndex();
173
// Reference _cp.html
174
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
175
"\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
178
case ATTR_SOURCE_FILE:
179
index = ((SourceFile)attribute).getSourceFileIndex();
181
// Reference _cp.html
182
file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index +
183
"\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
186
case ATTR_EXCEPTIONS:
187
// List thrown exceptions
188
int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
192
for(int i=0; i < indices.length; i++)
193
file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
194
"\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
196
file.print("</UL>\n");
199
case ATTR_LINE_NUMBER_TABLE:
200
LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
202
// List line number pairs
205
for(int i=0; i < line_numbers.length; i++) {
206
file.print("(" + line_numbers[i].getStartPC() + ", " + line_numbers[i].getLineNumber() + ")");
208
if(i < line_numbers.length - 1)
209
file.print(", "); // breakable
213
case ATTR_LOCAL_VARIABLE_TABLE:
214
LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
216
// List name, range and type
219
for(int i=0; i < vars.length; i++) {
220
index = vars[i].getSignatureIndex();
221
String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
222
signature = Utility.signatureToString(signature, false);
223
int start = vars[i].getStartPC();
224
int end = (start + vars[i].getLength());
226
file.println("<LI>" + Class2HTML.referenceType(signature) +
227
" <B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
228
"<BR>Valid from lines " +
229
"<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
231
"<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
234
file.print("</UL>\n");
238
case ATTR_INNER_CLASSES:
239
InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
241
// List inner classes
244
for(int i=0; i < classes.length; i++) {
247
index = classes[i].getInnerNameIndex();
249
name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
251
name = "<anonymous>";
253
access = Utility.accessToString(classes[i].getInnerAccessFlags());
255
file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
256
constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
258
constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
259
" named " + name + "</LI>\n");
262
file.print("</UL>\n");
265
default: // Such as Unknown attribute or Deprecated
266
file.print("<P>" + attribute.toString());
269
file.println("</TD></TR>");