|
1 /* |
|
2 * reserved comment block |
|
3 * DO NOT REMOVE OR ALTER! |
|
4 */ |
|
5 package com.sun.org.apache.bcel.internal.util; |
|
6 |
|
7 /* ==================================================================== |
|
8 * The Apache Software License, Version 1.1 |
|
9 * |
|
10 * Copyright (c) 2001 The Apache Software Foundation. All rights |
|
11 * reserved. |
|
12 * |
|
13 * Redistribution and use in source and binary forms, with or without |
|
14 * modification, are permitted provided that the following conditions |
|
15 * are met: |
|
16 * |
|
17 * 1. Redistributions of source code must retain the above copyright |
|
18 * notice, this list of conditions and the following disclaimer. |
|
19 * |
|
20 * 2. Redistributions in binary form must reproduce the above copyright |
|
21 * notice, this list of conditions and the following disclaimer in |
|
22 * the documentation and/or other materials provided with the |
|
23 * distribution. |
|
24 * |
|
25 * 3. The end-user documentation included with the redistribution, |
|
26 * if any, must include the following acknowledgment: |
|
27 * "This product includes software developed by the |
|
28 * Apache Software Foundation (http://www.apache.org/)." |
|
29 * Alternately, this acknowledgment may appear in the software itself, |
|
30 * if and wherever such third-party acknowledgments normally appear. |
|
31 * |
|
32 * 4. The names "Apache" and "Apache Software Foundation" and |
|
33 * "Apache BCEL" must not be used to endorse or promote products |
|
34 * derived from this software without prior written permission. For |
|
35 * written permission, please contact apache@apache.org. |
|
36 * |
|
37 * 5. Products derived from this software may not be called "Apache", |
|
38 * "Apache BCEL", nor may "Apache" appear in their name, without |
|
39 * prior written permission of the Apache Software Foundation. |
|
40 * |
|
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
|
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
|
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
|
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
52 * SUCH DAMAGE. |
|
53 * ==================================================================== |
|
54 * |
|
55 * This software consists of voluntary contributions made by many |
|
56 * individuals on behalf of the Apache Software Foundation. For more |
|
57 * information on the Apache Software Foundation, please see |
|
58 * <http://www.apache.org/>. |
|
59 */ |
|
60 |
|
61 import com.sun.org.apache.bcel.internal.classfile.*; |
|
62 import java.io.*; |
|
63 |
|
64 /** |
|
65 * Convert found attributes into HTML file. |
|
66 * |
|
67 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
|
68 * |
|
69 */ |
|
70 final class AttributeHTML implements com.sun.org.apache.bcel.internal.Constants { |
|
71 private String class_name; // name of current class |
|
72 private PrintWriter file; // file to write to |
|
73 private int attr_count = 0; |
|
74 private ConstantHTML constant_html; |
|
75 private ConstantPool constant_pool; |
|
76 |
|
77 AttributeHTML(String dir, String class_name, ConstantPool constant_pool, |
|
78 ConstantHTML constant_html) throws IOException |
|
79 { |
|
80 this.class_name = class_name; |
|
81 this.constant_pool = constant_pool; |
|
82 this.constant_html = constant_html; |
|
83 |
|
84 file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html")); |
|
85 file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>"); |
|
86 } |
|
87 |
|
88 private final String codeLink(int link, int method_number) { |
|
89 return "<A HREF=\"" + class_name + "_code.html#code" + |
|
90 method_number + "@" + link + "\" TARGET=Code>" + |
|
91 link + "</A>"; |
|
92 } |
|
93 |
|
94 final void close() { |
|
95 file.println("</TABLE></BODY></HTML>"); |
|
96 file.close(); |
|
97 } |
|
98 |
|
99 final void writeAttribute(Attribute attribute, String anchor) throws IOException { |
|
100 writeAttribute(attribute, anchor, 0); |
|
101 } |
|
102 |
|
103 final void writeAttribute(Attribute attribute, String anchor, int method_number) throws IOException { |
|
104 byte tag = attribute.getTag(); |
|
105 int index; |
|
106 |
|
107 if(tag == ATTR_UNKNOWN) // Don't know what to do about this one |
|
108 return; |
|
109 |
|
110 attr_count++; // Increment number of attributes found so far |
|
111 |
|
112 if(attr_count % 2 == 0) |
|
113 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>"); |
|
114 else |
|
115 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>"); |
|
116 |
|
117 file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>"); |
|
118 |
|
119 /* Handle different attributes |
|
120 */ |
|
121 switch(tag) { |
|
122 case ATTR_CODE: |
|
123 Code c = (Code)attribute; |
|
124 |
|
125 // Some directly printable values |
|
126 file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + |
|
127 "</LI>\n<LI>Number of local variables = " + |
|
128 c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name + |
|
129 "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n"); |
|
130 |
|
131 // Get handled exceptions and list them |
|
132 CodeException[] ce = c.getExceptionTable(); |
|
133 int len = ce.length; |
|
134 |
|
135 if(len > 0) { |
|
136 file.print("<P><B>Exceptions handled</B><UL>"); |
|
137 |
|
138 for(int i=0; i < len; i++) { |
|
139 int catch_type = ce[i].getCatchType(); // Index in constant pool |
|
140 |
|
141 file.print("<LI>"); |
|
142 |
|
143 if(catch_type != 0) |
|
144 file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html |
|
145 else |
|
146 file.print("Any Exception"); |
|
147 |
|
148 file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) + |
|
149 " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " + |
|
150 codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>"); |
|
151 } |
|
152 file.print("</UL>"); |
|
153 } |
|
154 break; |
|
155 |
|
156 case ATTR_CONSTANT_VALUE: |
|
157 index = ((ConstantValue)attribute).getConstantValueIndex(); |
|
158 |
|
159 // Reference _cp.html |
|
160 file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + |
|
161 "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n"); |
|
162 break; |
|
163 |
|
164 case ATTR_SOURCE_FILE: |
|
165 index = ((SourceFile)attribute).getSourceFileIndex(); |
|
166 |
|
167 // Reference _cp.html |
|
168 file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + |
|
169 "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n"); |
|
170 break; |
|
171 |
|
172 case ATTR_EXCEPTIONS: |
|
173 // List thrown exceptions |
|
174 int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable(); |
|
175 |
|
176 file.print("<UL>"); |
|
177 |
|
178 for(int i=0; i < indices.length; i++) |
|
179 file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] + |
|
180 "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n"); |
|
181 |
|
182 file.print("</UL>\n"); |
|
183 break; |
|
184 |
|
185 case ATTR_LINE_NUMBER_TABLE: |
|
186 LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable(); |
|
187 |
|
188 // List line number pairs |
|
189 file.print("<P>"); |
|
190 |
|
191 for(int i=0; i < line_numbers.length; i++) { |
|
192 file.print("(" + line_numbers[i].getStartPC() + ", " + line_numbers[i].getLineNumber() + ")"); |
|
193 |
|
194 if(i < line_numbers.length - 1) |
|
195 file.print(", "); // breakable |
|
196 } |
|
197 break; |
|
198 |
|
199 case ATTR_LOCAL_VARIABLE_TABLE: |
|
200 LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable(); |
|
201 |
|
202 // List name, range and type |
|
203 file.print("<UL>"); |
|
204 |
|
205 for(int i=0; i < vars.length; i++) { |
|
206 index = vars[i].getSignatureIndex(); |
|
207 String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes(); |
|
208 signature = Utility.signatureToString(signature, false); |
|
209 int start = vars[i].getStartPC(); |
|
210 int end = (start + vars[i].getLength()); |
|
211 |
|
212 file.println("<LI>" + Class2HTML.referenceType(signature) + |
|
213 " <B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() + |
|
214 "<BR>Valid from lines " + |
|
215 "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" + |
|
216 start + "</A> to " + |
|
217 "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" + |
|
218 end + "</A></LI>"); |
|
219 } |
|
220 file.print("</UL>\n"); |
|
221 |
|
222 break; |
|
223 |
|
224 case ATTR_INNER_CLASSES: |
|
225 InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses(); |
|
226 |
|
227 // List inner classes |
|
228 file.print("<UL>"); |
|
229 |
|
230 for(int i=0; i < classes.length; i++) { |
|
231 String name, access; |
|
232 |
|
233 index = classes[i].getInnerNameIndex(); |
|
234 if(index > 0) |
|
235 name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes(); |
|
236 else |
|
237 name = "<anonymous>"; |
|
238 |
|
239 access = Utility.accessToString(classes[i].getInnerAccessFlags()); |
|
240 |
|
241 file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+ |
|
242 constant_html.referenceConstant(classes[i].getInnerClassIndex()) + |
|
243 " in class " + |
|
244 constant_html.referenceConstant(classes[i].getOuterClassIndex()) + |
|
245 " named " + name + "</LI>\n"); |
|
246 } |
|
247 |
|
248 file.print("</UL>\n"); |
|
249 break; |
|
250 |
|
251 default: // Such as Unknown attribute or Deprecated |
|
252 file.print("<P>" + attribute.toString()); |
|
253 } |
|
254 |
|
255 file.println("</TD></TR>"); |
|
256 file.flush(); |
|
257 } |
|
258 } |