1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.util;
18
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.PrintWriter;
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 class_name;
48 private PrintWriter file;
49 private int attr_count = 0;
50 private ConstantHTML constant_html;
51 private ConstantPool constant_pool;
52
53
54 AttributeHTML(String dir, String class_name, ConstantPool constant_pool,
55 ConstantHTML constant_html) throws IOException {
56 this.class_name = class_name;
57 this.constant_pool = constant_pool;
58 this.constant_html = constant_html;
59 file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
60 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
61 }
62
63
64 private final String 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 anchor ) throws IOException {
77 writeAttribute(attribute, anchor, 0);
78 }
79
80
81 final void writeAttribute( Attribute attribute, String anchor, int method_number )
82 throws IOException {
83 byte tag = attribute.getTag();
84 int index;
85 if (tag == ATTR_UNKNOWN) {
86 return;
87 }
88 attr_count++;
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
97
98 switch (tag) {
99 case ATTR_CODE:
100 Code c = (Code) attribute;
101
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
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();
113 file.print("<LI>");
114 if (catch_type != 0) {
115 file.print(constant_html.referenceConstant(catch_type));
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
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
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
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
154 file.print("<P>");
155 for (int i = 0; i < line_numbers.length; i++) {
156 file.print("(" + line_numbers[i].getStartPC() + ", "
157 + line_numbers[i].getLineNumber() + ")");
158 if (i < line_numbers.length - 1) {
159 file.print(", ");
160 }
161 }
162 break;
163 case ATTR_LOCAL_VARIABLE_TABLE:
164 LocalVariable[] vars = ((LocalVariableTable) attribute).getLocalVariableTable();
165
166 file.print("<UL>");
167 for (int i = 0; i < vars.length; i++) {
168 index = vars[i].getSignatureIndex();
169 String 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) + " <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
186 file.print("<UL>");
187 for (int i = 0; i < classes.length; i++) {
188 String 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 = "<anonymous>";
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 class "
200 + constant_html.referenceConstant(classes[i].getOuterClassIndex())
201 + " named " + name + "</LI>\n");
202 }
203 file.print("</UL>\n");
204 break;
205 default:
206 file.print("<P>" + attribute.toString());
207 }
208 file.println("</TD></TR>");
209 file.flush();
210 }
211 }