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

ConstantHTML.java

00001 package de.fub.bytecode.util;
00002 
00003 
00004 import de.fub.bytecode.classfile.*;
00005 import java.io.*;
00006 
00007 /**
00008  * Convert constant pool into HTML file.
00009  *
00010  * @version $Id: ConstantHTML.java,v 1.1.1.1 2002/01/24 03:41:42 pserver Exp $
00011  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00012  * 
00013  */
00014 final class ConstantHTML implements de.fub.bytecode.Constants {
00015   private String    class_name;     // name of current class
00016   private String    class_package;  // name of package
00017   private ConstantPool  constant_pool;  // reference to constant pool
00018   private PrintWriter   file;       // file to write to
00019   private String[]      constant_ref;   // String to return for cp[i]
00020   private Constant[]    constants;  // The constants in the cp
00021   private Method[]      methods;
00022 
00023   ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
00024            ConstantPool constant_pool) throws IOException
00025   {
00026     this.class_name     = class_name;
00027     this.class_package  = class_package;
00028     this.constant_pool  = constant_pool;
00029     this.methods        = methods;
00030     constants       = constant_pool.getConstantPool();
00031     file        = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
00032     constant_ref    = new String[constants.length];
00033     constant_ref[0]     = "&lt;unknown&gt;";
00034     
00035     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
00036     
00037     // Loop through constants, constants[0] is reserved
00038     for(int i=1; i < constants.length; i++) {
00039       if(i % 2 == 0)
00040     file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
00041       else
00042     file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
00043 
00044       if(constants[i] != null)
00045     writeConstant(i);
00046       
00047       file.print("</TD></TR>\n");
00048     }
00049         
00050     file.println("</TABLE></BODY></HTML>");
00051     file.close();
00052   }  
00053   private final int getMethodNumber(String str) {
00054     for(int i=0; i < methods.length; i++) {
00055       String cmp = methods[i].getName() + methods[i].getSignature();
00056       if(cmp.equals(str))
00057     return i;
00058     }
00059     return -1;
00060   }  
00061   String referenceConstant(int index) {
00062     return constant_ref[index];
00063   }  
00064   private void writeConstant(int index) {
00065     byte   tag = constants[index].getTag();
00066     int    class_index, name_index;
00067     String ref; 
00068     
00069     // The header is always the same
00070     file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>");
00071 
00072     /* For every constant type get the needed parameters and print them appropiately 
00073      */
00074     switch(tag) {
00075     case CONSTANT_InterfaceMethodref:
00076     case CONSTANT_Methodref:
00077       // Get class_index and name_and_type_index, depending on type
00078       if(tag == CONSTANT_Methodref) {
00079     ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref);
00080     class_index = c.getClassIndex();
00081     name_index  = c.getNameAndTypeIndex();
00082       }
00083       else {
00084     ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref);
00085     class_index = c1.getClassIndex();
00086     name_index  = c1.getNameAndTypeIndex();
00087       }
00088 
00089       // Get method name and its class
00090       String method_name    = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
00091       String html_method_name = Class2HTML.toHTML(method_name);
00092       
00093       // Partially compacted class name, i.e., / -> .
00094       String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
00095       String short_method_class     = Utility.compactClassName(method_class); // I.e., remove java.lang.
00096       short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
00097       short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); // Remove class package prefix
00098 
00099       // Get method signature
00100       ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType);
00101       String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8);
00102       // Get array of strings containing the argument types
00103       String[] args = Utility.methodSignatureArgumentTypes(signature, false);
00104             
00105       // Get return type string
00106       String type = Utility.methodSignatureReturnType(signature, false);
00107       String ret_type = Class2HTML.referenceType(type);
00108 
00109       StringBuffer buf = new StringBuffer("(");
00110       for(int i=0; i < args.length; i++) {
00111     buf.append(Class2HTML.referenceType(args[i]));
00112     if(i < args.length - 1)
00113       buf.append(",&nbsp;");
00114       }         
00115       buf.append(")");
00116 
00117       String arg_types = buf.toString();
00118             
00119       if(method_class.equals(class_name)) // Method is local to class
00120     ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) +
00121       "\" TARGET=Code>" + html_method_name + "</A>";
00122       else
00123     ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class +
00124       "</A>." + html_method_name;
00125 
00126       constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp" + class_index +
00127     "\" TARGET=Constants>" +
00128     short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
00129     index + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
00130 
00131       file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types + "&nbsp;</TT>\n<UL>" +
00132            "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index +    ")</A>\n" +
00133            "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
00134       break;
00135 
00136     case CONSTANT_Fieldref:
00137       // Get class_index and name_and_type_index
00138       ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref);
00139       class_index = c3.getClassIndex();
00140       name_index  = c3.getNameAndTypeIndex();
00141 
00142       // Get method name and its class (compacted)
00143       String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
00144       String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
00145       short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); // Remove class package prefix
00146 
00147       String field_name  = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
00148 
00149       if(field_class.equals(class_name)) // Field is local to class
00150     ref = "<A HREF=\"" + field_class + "_methods.html#field" + 
00151       field_name + "\" TARGET=Methods>" + field_name + "</A>";
00152       else
00153     ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" +
00154       short_field_class + "</A>." + field_name + "\n";
00155 
00156       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index +   "\" TARGET=Constants>" +
00157     short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
00158     index + "\" TARGET=ConstantPool>" + field_name + "</A>";
00159                                                 
00160       file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" +
00161            "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index +  ")</A><BR>\n" +
00162            "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>");
00163       break;
00164             
00165     case CONSTANT_Class:
00166       ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class);
00167       name_index  = c4.getNameIndex();
00168       String class_name2  = constant_pool.constantToString(index, tag); // / -> .
00169       String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
00170       short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); // Remove class package prefix
00171 
00172       ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>";
00173       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index +
00174     "\" TARGET=ConstantPool>" + short_class_name + "</A>";
00175                                             
00176       file.println("<P><TT>" + ref + "</TT><UL>" +
00177            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index +   ")</A></UL>\n");
00178       break;
00179     
00180     case CONSTANT_String:
00181       ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String);
00182       name_index = c5.getStringIndex();
00183 
00184       String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
00185   
00186       file.println("<P><TT>" + str + "</TT><UL>" + 
00187            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index +   ")</A></UL>\n");
00188       break;
00189     
00190     case CONSTANT_NameAndType:
00191       ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType);
00192       name_index = c6.getNameIndex();
00193       int signature_index = c6.getSignatureIndex();
00194 
00195       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" +
00196            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" +
00197            "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" + 
00198            signature_index + ")</A></UL>\n");
00199       break;
00200 
00201     default:
00202       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n");
00203     } // switch
00204   }  
00205 }

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