1   package org.apache.bcel.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 org.apache.bcel.classfile.*;
58  import java.io.*;
59  
60  /***
61   * Convert found attributes into HTML file.
62   *
63   * @version $Id: AttributeHTML.java,v 1.2 2002/06/04 11:16:21 mdahm Exp $
64   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
65   * 
66   */
67  final class AttributeHTML implements org.apache.bcel.Constants {
68    private String       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 dir, String 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 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 anchor) throws IOException {	
97      writeAttribute(attribute, anchor, 0);
98    }
99  
100   final void writeAttribute(Attribute attribute, String 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 
122       // Some directly printable values
123       file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + 
124 		 "</LI>\n<LI>Number of local variables = " + 
125 		 c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
126 		 "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
127 
128       // Get handled exceptions and list them
129       CodeException[] ce  = c.getExceptionTable();
130       int             len = ce.length;
131 
132       if(len > 0) {
133 	file.print("<P><B>Exceptions handled</B><UL>");
134 
135 	for(int i=0; i < len; i++) {
136 	  int catch_type = ce[i].getCatchType(); // Index in constant pool
137 	
138 	  file.print("<LI>");
139 
140 	  if(catch_type != 0)
141 	    file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
142 	  else
143 	    file.print("Any Exception");
144 
145 	  file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
146 		     " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " + 
147 		     codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
148 	}
149 	file.print("</UL>");
150       }
151       break;
152 
153     case ATTR_CONSTANT_VALUE:
154       index = ((ConstantValue)attribute).getConstantValueIndex();
155 
156       // Reference _cp.html
157       file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
158 		 "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
159       break;
160 
161     case ATTR_SOURCE_FILE:
162       index = ((SourceFile)attribute).getSourceFileIndex();
163 
164       // Reference _cp.html
165       file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
166 		 "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
167       break;
168 	  
169     case ATTR_EXCEPTIONS:
170       // List thrown exceptions
171       int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
172 
173       file.print("<UL>");
174 
175       for(int i=0; i < indices.length; i++)
176 	file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
177 		   "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
178 
179       file.print("</UL>\n");
180       break;
181 
182     case ATTR_LINE_NUMBER_TABLE:
183       LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
184 
185       // List line number pairs
186       file.print("<P>");
187 
188       for(int i=0; i < line_numbers.length; i++) {
189 	file.print("(" + line_numbers[i].getStartPC() + ", " + line_numbers[i].getLineNumber() + ")");
190 
191 	if(i < line_numbers.length - 1)
192 	  file.print(", "); // breakable
193       }
194       break;
195 
196     case ATTR_LOCAL_VARIABLE_TABLE:
197       LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
198 
199       // List name, range and type
200       file.print("<UL>");
201 
202       for(int i=0; i < vars.length; i++) {
203 	index = vars[i].getSignatureIndex();
204 	String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
205 	signature = Utility.signatureToString(signature, false);
206 	int  start = vars[i].getStartPC();
207 	int  end   = (start + vars[i].getLength());
208 
209 	file.println("<LI>" + Class2HTML.referenceType(signature) +
210 		     " <B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
211 		     "<BR>Valid from lines " +
212 		     "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
213 		     start + "</A> to " +
214 		     "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
215 		     end + "</A></LI>");
216       }
217       file.print("</UL>\n");
218 
219       break;
220 
221     case ATTR_INNER_CLASSES:
222       InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
223 
224       // List inner classes
225       file.print("<UL>");
226 
227       for(int i=0; i < classes.length; i++) {
228 	String name, access;
229 	
230 	index = classes[i].getInnerNameIndex();
231 	if(index > 0)
232 	  name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
233 	else
234 	  name = "<anonymous>";
235 
236 	access = Utility.accessToString(classes[i].getInnerAccessFlags());
237 
238 	file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
239 		   constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
240 		   " in class " + 
241 		   constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
242 		   " named " + name + "</LI>\n");
243       }
244 
245       file.print("</UL>\n");
246       break;
247 
248     default: // Such as Unknown attribute or Deprecated
249       file.print("<P>" + attribute.toString());
250     }
251 
252     file.println("</TD></TR>");
253     file.flush();
254   }                  
255 }
This page was automatically generated by Maven