Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

AttributeHTML.java

00001 package de.fub.bytecode.util;
00002 
00003 import de.fub.bytecode.classfile.*;
00004 import java.io.*;
00005 
00006 /**
00007  * Convert found attributes into HTML file.
00008  *
00009  * @version $Id: AttributeHTML.java,v 1.1.1.1 2002/01/24 03:41:42 pserver Exp $
00010  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00011  * 
00012  */
00013 final class AttributeHTML implements de.fub.bytecode.Constants {
00014   private String       class_name;     // name of current class
00015   private PrintWriter  file;                    // file to write to
00016   private int          attr_count = 0;
00017   private ConstantHTML constant_html;
00018   private ConstantPool constant_pool;
00019 
00020   AttributeHTML(String dir, String class_name, ConstantPool constant_pool,
00021         ConstantHTML constant_html) throws IOException
00022   {
00023     this.class_name    = class_name;
00024     this.constant_pool = constant_pool;
00025     this.constant_html = constant_html;
00026 
00027     file = new PrintWriter(new FileOutputStream(dir + class_name + "_attributes.html"));
00028     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
00029   }  
00030   final void close() {  
00031     file.println("</TABLE></BODY></HTML>");
00032     file.close();
00033   }  
00034   private final String codeLink(int link, int method_number) {
00035     return "<A HREF=\"" + class_name + "_code.html#code" +
00036       method_number + "@" + link + "\" TARGET=Code>" +
00037       link + "</A>";
00038   }  
00039   final void writeAttribute(Attribute attribute, String anchor) throws IOException {    
00040     writeAttribute(attribute, anchor, 0);
00041   }  
00042   final void writeAttribute(Attribute attribute, String anchor, int method_number) throws IOException { 
00043     byte         tag = attribute.getTag();
00044     int        index;
00045  
00046     if(tag == ATTR_UNKNOWN) // Don't know what to do about this one
00047       return;
00048 
00049     attr_count++; // Increment number of attributes found so far
00050 
00051     if(attr_count % 2 == 0)
00052       file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
00053     else
00054       file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
00055 
00056     file.println("<H4><A NAME=\"" + anchor + "\">" + attr_count + " " + ATTRIBUTE_NAMES[tag] + "</A></H4>");
00057 
00058     /* Handle different attributes
00059      */
00060     switch(tag) {
00061     case ATTR_CODE:
00062       Code        c          = (Code)attribute;
00063       Attribute[] attributes = c.getAttributes();
00064 
00065       // Some directly printable values
00066       file.print("<UL><LI>Maximum stack size = " + c.getMaxStack() + 
00067          "</LI>\n<LI>Number of local variables = " + 
00068          c.getMaxLocals() + "</LI>\n<LI><A HREF=\"" + class_name +
00069          "_code.html#method" + method_number + "\" TARGET=Code>Byte code</A></LI></UL>\n");
00070 
00071       // Get handled exceptions and list them
00072       CodeException[] ce  = c.getExceptionTable();
00073       int             len = ce.length;
00074 
00075       if(len > 0) {
00076     file.print("<P><B>Exceptions handled</B><UL>");
00077 
00078     for(int i=0; i < len; i++) {
00079       int catch_type = ce[i].getCatchType(); // Index in constant pool
00080     
00081       file.print("<LI>");
00082 
00083       if(catch_type != 0)
00084         file.print(constant_html.referenceConstant(catch_type)); // Create Link to _cp.html
00085       else
00086         file.print("Any Exception");
00087 
00088       file.print("<BR>(Ranging from lines " + codeLink(ce[i].getStartPC(), method_number) +
00089              " to " + codeLink(ce[i].getEndPC(), method_number) + ", handled at line " + 
00090              codeLink(ce[i].getHandlerPC(), method_number) + ")</LI>");
00091     }
00092     file.print("</UL>");
00093       }
00094       break;
00095 
00096     case ATTR_CONSTANT_VALUE:
00097       index = ((ConstantValue)attribute).getConstantValueIndex();
00098 
00099       // Reference _cp.html
00100       file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
00101          "\" TARGET=\"ConstantPool\">Constant value index(" + index +")</A></UL>\n");
00102       break;
00103 
00104     case ATTR_SOURCE_FILE:
00105       index = ((SourceFile)attribute).getSourceFileIndex();
00106 
00107       // Reference _cp.html
00108       file.print("<UL><LI><A HREF=\"" + class_name + "_cp.html#cp" + index + 
00109          "\" TARGET=\"ConstantPool\">Source file index(" + index +")</A></UL>\n");
00110       break;
00111       
00112     case ATTR_EXCEPTIONS:
00113       // List thrown exceptions
00114       int[] indices = ((ExceptionTable)attribute).getExceptionIndexTable();
00115 
00116       file.print("<UL>");
00117 
00118       for(int i=0; i < indices.length; i++)
00119     file.print("<LI><A HREF=\"" + class_name + "_cp.html#cp" + indices[i] +
00120            "\" TARGET=\"ConstantPool\">Exception class index(" + indices[i] + ")</A>\n");
00121 
00122       file.print("</UL>\n");
00123       break;
00124 
00125     case ATTR_LINE_NUMBER_TABLE:
00126       LineNumber[] line_numbers =((LineNumberTable)attribute).getLineNumberTable();
00127 
00128       // List line number pairs
00129       file.print("<P>");
00130 
00131       for(int i=0; i < line_numbers.length; i++) {
00132     file.print("(" + line_numbers[i].getStartPC() + ",&nbsp;" + line_numbers[i].getLineNumber() + ")");
00133 
00134     if(i < line_numbers.length - 1)
00135       file.print(", "); // breakable
00136       }
00137       break;
00138 
00139     case ATTR_LOCAL_VARIABLE_TABLE:
00140       LocalVariable[] vars = ((LocalVariableTable)attribute).getLocalVariableTable();
00141 
00142       // List name, range and type
00143       file.print("<UL>");
00144 
00145       for(int i=0; i < vars.length; i++) {
00146     index = vars[i].getSignatureIndex();
00147     String signature = ((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
00148     signature = Utility.signatureToString(signature, false);
00149     int  start = vars[i].getStartPC();
00150     int  end   = (start + vars[i].getLength());
00151 
00152     file.println("<LI>" + Class2HTML.referenceType(signature) +
00153              "&nbsp;<B>" + vars[i].getName() + "</B> in slot %" + vars[i].getIndex() +
00154              "<BR>Valid from lines " +
00155              "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + start + "\" TARGET=Code>" +
00156              start + "</A> to " +
00157              "<A HREF=\"" + class_name + "_code.html#code" + method_number + "@" + end + "\" TARGET=Code>" +
00158              end + "</A></LI>");
00159       }
00160       file.print("</UL>\n");
00161 
00162       break;
00163 
00164     case ATTR_INNER_CLASSES:
00165       InnerClass[] classes = ((InnerClasses)attribute).getInnerClasses();
00166 
00167       // List inner classes
00168       file.print("<UL>");
00169 
00170       for(int i=0; i < classes.length; i++) {
00171     String name, access;
00172     
00173     index = classes[i].getInnerNameIndex();
00174     if(index > 0)
00175       name =((ConstantUtf8)constant_pool.getConstant(index, CONSTANT_Utf8)).getBytes();
00176     else
00177       name = "&lt;anonymous&gt;";
00178 
00179     access = Utility.accessToString(classes[i].getInnerAccessFlags());
00180 
00181     file.print("<LI><FONT COLOR=\"#FF0000\">" + access + "</FONT> "+
00182            constant_html.referenceConstant(classes[i].getInnerClassIndex()) +
00183            " in&nbsp;class " + 
00184            constant_html.referenceConstant(classes[i].getOuterClassIndex()) +
00185            " named " + name + "</LI>\n");
00186       }
00187 
00188       file.print("</UL>\n");
00189       break;
00190 
00191     default: // Such as Unknown attribute or Deprecated
00192       file.print("<P>" + attribute.toString());
00193     }
00194 
00195     file.println("</TD></TR>");
00196     file.flush();
00197   }  
00198 }

Generated at Thu Feb 7 06:40:17 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001