jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/ConstantHTML.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
    19  * limitations under the License.
    19  * limitations under the License.
    20  */
    20  */
    21 
    21 
    22 package com.sun.org.apache.bcel.internal.util;
    22 package com.sun.org.apache.bcel.internal.util;
    23 
    23 
    24 
    24 import java.io.FileOutputStream;
    25 import com.sun.org.apache.bcel.internal.classfile.*;
    25 import java.io.IOException;
    26 import java.io.*;
    26 import java.io.PrintWriter;
       
    27 
       
    28 import com.sun.org.apache.bcel.internal.Const;
       
    29 import com.sun.org.apache.bcel.internal.classfile.Constant;
       
    30 import com.sun.org.apache.bcel.internal.classfile.ConstantClass;
       
    31 import com.sun.org.apache.bcel.internal.classfile.ConstantFieldref;
       
    32 import com.sun.org.apache.bcel.internal.classfile.ConstantInterfaceMethodref;
       
    33 import com.sun.org.apache.bcel.internal.classfile.ConstantMethodref;
       
    34 import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType;
       
    35 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
       
    36 import com.sun.org.apache.bcel.internal.classfile.ConstantString;
       
    37 import com.sun.org.apache.bcel.internal.classfile.Method;
       
    38 import com.sun.org.apache.bcel.internal.classfile.Utility;
    27 
    39 
    28 /**
    40 /**
    29  * Convert constant pool into HTML file.
    41  * Convert constant pool into HTML file.
    30  *
    42  *
    31  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    43  * @version $Id: ConstantHTML.java 1749603 2016-06-21 20:50:19Z ggregory $
    32  *
    44  *
    33  */
    45  */
    34 final class ConstantHTML implements com.sun.org.apache.bcel.internal.Constants {
    46 final class ConstantHTML {
    35   private String        class_name;     // name of current class
    47 
    36   private String        class_package;  // name of package
    48     private final String class_name; // name of current class
    37   private ConstantPool  constant_pool;  // reference to constant pool
    49     private final String class_package; // name of package
    38   private PrintWriter   file;           // file to write to
    50     private final ConstantPool constant_pool; // reference to constant pool
    39   private String[]      constant_ref;   // String to return for cp[i]
    51     private final PrintWriter file; // file to write to
    40   private Constant[]    constants;      // The constants in the cp
    52     private final String[] constant_ref; // String to return for cp[i]
    41   private Method[]      methods;
    53     private final Constant[] constants; // The constants in the cp
    42 
    54     private final Method[] methods;
    43   ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
    55 
    44                ConstantPool constant_pool) throws IOException
    56 
    45   {
    57     ConstantHTML(final String dir, final String class_name, final String class_package,
    46     this.class_name     = class_name;
    58             final Method[] methods, final ConstantPool constant_pool) throws IOException {
    47     this.class_package  = class_package;
    59         this.class_name = class_name;
    48     this.constant_pool  = constant_pool;
    60         this.class_package = class_package;
    49     this.methods        = methods;
    61         this.constant_pool = constant_pool;
    50     constants           = constant_pool.getConstantPool();
    62         this.methods = methods;
    51     file                = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
    63         constants = constant_pool.getConstantPool();
    52     constant_ref        = new String[constants.length];
    64         file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
    53     constant_ref[0]     = "&lt;unknown&gt;";
    65         constant_ref = new String[constants.length];
    54 
    66         constant_ref[0] = "&lt;unknown&gt;";
    55     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
    67         file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
    56 
    68         // Loop through constants, constants[0] is reserved
    57     // Loop through constants, constants[0] is reserved
    69         for (int i = 1; i < constants.length; i++) {
    58     for(int i=1; i < constants.length; i++) {
    70             if (i % 2 == 0) {
    59       if(i % 2 == 0)
    71                 file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
    60         file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
    72             } else {
    61       else
    73                 file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
    62         file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
    74             }
    63 
    75             if (constants[i] != null) {
    64       if(constants[i] != null)
    76                 writeConstant(i);
    65         writeConstant(i);
    77             }
    66 
    78             file.print("</TD></TR>\n");
    67       file.print("</TD></TR>\n");
    79         }
    68     }
    80         file.println("</TABLE></BODY></HTML>");
    69 
    81         file.close();
    70     file.println("</TABLE></BODY></HTML>");
    82     }
    71     file.close();
    83 
    72   }
    84 
    73 
    85     String referenceConstant( final int index ) {
    74   String referenceConstant(int index) {
    86         return constant_ref[index];
    75     return constant_ref[index];
    87     }
    76   }
    88 
    77 
    89 
    78   private void writeConstant(int index) {
    90     private void writeConstant( final int index ) {
    79     byte   tag = constants[index].getTag();
    91         final byte tag = constants[index].getTag();
    80     int    class_index, name_index;
    92         int class_index;
    81     String ref;
    93         int name_index;
    82 
    94         String ref;
    83     // The header is always the same
    95         // The header is always the same
    84     file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>");
    96         file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag)
    85 
    97                 + "</H4>");
    86     /* For every constant type get the needed parameters and print them appropiately
    98         /* For every constant type get the needed parameters and print them appropiately
    87      */
    99          */
    88     switch(tag) {
   100         switch (tag) {
    89     case CONSTANT_InterfaceMethodref:
   101             case Const.CONSTANT_InterfaceMethodref:
    90     case CONSTANT_Methodref:
   102             case Const.CONSTANT_Methodref:
    91       // Get class_index and name_and_type_index, depending on type
   103                 // Get class_index and name_and_type_index, depending on type
    92       if(tag == CONSTANT_Methodref) {
   104                 if (tag == Const.CONSTANT_Methodref) {
    93         ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref);
   105                     final ConstantMethodref c = (ConstantMethodref) constant_pool.getConstant(index,
    94         class_index = c.getClassIndex();
   106                             Const.CONSTANT_Methodref);
    95         name_index  = c.getNameAndTypeIndex();
   107                     class_index = c.getClassIndex();
    96       }
   108                     name_index = c.getNameAndTypeIndex();
    97       else {
   109                 } else {
    98         ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref);
   110                     final ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref) constant_pool
    99         class_index = c1.getClassIndex();
   111                             .getConstant(index, Const.CONSTANT_InterfaceMethodref);
   100         name_index  = c1.getNameAndTypeIndex();
   112                     class_index = c1.getClassIndex();
   101       }
   113                     name_index = c1.getNameAndTypeIndex();
   102 
   114                 }
   103       // Get method name and its class
   115                 // Get method name and its class
   104       String method_name        = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
   116                 final String method_name = constant_pool.constantToString(name_index,
   105       String html_method_name = Class2HTML.toHTML(method_name);
   117                         Const.CONSTANT_NameAndType);
   106 
   118                 final String html_method_name = Class2HTML.toHTML(method_name);
   107       // Partially compacted class name, i.e., / -> .
   119                 // Partially compacted class name, i.e., / -> .
   108       String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
   120                 final String method_class = constant_pool.constantToString(class_index, Const.CONSTANT_Class);
   109       String short_method_class         = Utility.compactClassName(method_class); // I.e., remove java.lang.
   121                 String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
   110       short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
   122                 short_method_class = Utility.compactClassName(short_method_class, class_package
   111       short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); // Remove class package prefix
   123                         + ".", true); // Remove class package prefix
   112 
   124                 // Get method signature
   113       // Get method signature
   125                 final ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(
   114       ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType);
   126                         name_index, Const.CONSTANT_NameAndType);
   115       String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8);
   127                 final String signature = constant_pool.constantToString(c2.getSignatureIndex(),
   116       // Get array of strings containing the argument types
   128                         Const.CONSTANT_Utf8);
   117       String[] args = Utility.methodSignatureArgumentTypes(signature, false);
   129                 // Get array of strings containing the argument types
   118 
   130                 final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
   119       // Get return type string
   131                 // Get return type string
   120       String type = Utility.methodSignatureReturnType(signature, false);
   132                 final String type = Utility.methodSignatureReturnType(signature, false);
   121       String ret_type = Class2HTML.referenceType(type);
   133                 final String ret_type = Class2HTML.referenceType(type);
   122 
   134                 final StringBuilder buf = new StringBuilder("(");
   123       StringBuffer buf = new StringBuffer("(");
   135                 for (int i = 0; i < args.length; i++) {
   124       for(int i=0; i < args.length; i++) {
   136                     buf.append(Class2HTML.referenceType(args[i]));
   125         buf.append(Class2HTML.referenceType(args[i]));
   137                     if (i < args.length - 1) {
   126         if(i < args.length - 1)
   138                         buf.append(",&nbsp;");
   127           buf.append(",&nbsp;");
   139                     }
   128       }
   140                 }
   129       buf.append(")");
   141                 buf.append(")");
   130 
   142                 final String arg_types = buf.toString();
   131       String arg_types = buf.toString();
   143                 if (method_class.equals(class_name)) {
   132 
   144                     ref = "<A HREF=\"" + class_name + "_code.html#method"
   133       if(method_class.equals(class_name)) // Method is local to class
   145                             + getMethodNumber(method_name + signature) + "\" TARGET=Code>"
   134         ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) +
   146                             + html_method_name + "</A>";
   135           "\" TARGET=Code>" + html_method_name + "</A>";
   147                 } else {
   136       else
   148                     ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>"
   137         ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class +
   149                             + short_method_class + "</A>." + html_method_name;
   138           "</A>." + html_method_name;
   150                 }
   139 
   151                 constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp"
   140       constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp" + class_index +
   152                         + class_index + "\" TARGET=Constants>" + short_method_class
   141         "\" TARGET=Constants>" +
   153                         + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + index
   142         short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
   154                         + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
   143         index + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
   155                 file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types
   144 
   156                         + "&nbsp;</TT>\n<UL>" + "<LI><A HREF=\"#cp" + class_index
   145       file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types + "&nbsp;</TT>\n<UL>" +
   157                         + "\">Class index(" + class_index + ")</A>\n" + "<LI><A HREF=\"#cp"
   146                    "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index +        ")</A>\n" +
   158                         + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
   147                    "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
   159                 break;
   148       break;
   160             case Const.CONSTANT_Fieldref:
   149 
   161                 // Get class_index and name_and_type_index
   150     case CONSTANT_Fieldref:
   162                 final ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index,
   151       // Get class_index and name_and_type_index
   163                         Const.CONSTANT_Fieldref);
   152       ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref);
   164                 class_index = c3.getClassIndex();
   153       class_index = c3.getClassIndex();
   165                 name_index = c3.getNameAndTypeIndex();
   154       name_index  = c3.getNameAndTypeIndex();
   166                 // Get method name and its class (compacted)
   155 
   167                 final String field_class = constant_pool.constantToString(class_index, Const.CONSTANT_Class);
   156       // Get method name and its class (compacted)
   168                 String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
   157       String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
   169                 short_field_class = Utility.compactClassName(short_field_class,
   158       String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
   170                         class_package + ".", true); // Remove class package prefix
   159       short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); // Remove class package prefix
   171                 final String field_name = constant_pool
   160 
   172                         .constantToString(name_index, Const.CONSTANT_NameAndType);
   161       String field_name  = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
   173                 if (field_class.equals(class_name)) {
   162 
   174                     ref = "<A HREF=\"" + field_class + "_methods.html#field" + field_name
   163       if(field_class.equals(class_name)) // Field is local to class
   175                             + "\" TARGET=Methods>" + field_name + "</A>";
   164         ref = "<A HREF=\"" + field_class + "_methods.html#field" +
   176                 } else {
   165           field_name + "\" TARGET=Methods>" + field_name + "</A>";
   177                     ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + short_field_class
   166       else
   178                             + "</A>." + field_name + "\n";
   167         ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" +
   179                 }
   168           short_field_class + "</A>." + field_name + "\n";
   180                 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index
   169 
   181                         + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\""
   170       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index +   "\" TARGET=Constants>" +
   182                         + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>"
   171         short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
   183                         + field_name + "</A>";
   172         index + "\" TARGET=ConstantPool>" + field_name + "</A>";
   184                 file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp"
   173 
   185                         + class_index + "\">Class(" + class_index + ")</A><BR>\n"
   174       file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" +
   186                         + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index
   175                    "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index +      ")</A><BR>\n" +
   187                         + ")</A></UL>");
   176                    "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>");
   188                 break;
   177       break;
   189             case Const.CONSTANT_Class:
   178 
   190                 final ConstantClass c4 = (ConstantClass) constant_pool.getConstant(index, Const.CONSTANT_Class);
   179     case CONSTANT_Class:
   191                 name_index = c4.getNameIndex();
   180       ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class);
   192                 final String class_name2 = constant_pool.constantToString(index, tag); // / -> .
   181       name_index  = c4.getNameIndex();
   193                 String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
   182       String class_name2  = constant_pool.constantToString(index, tag); // / -> .
   194                 short_class_name = Utility.compactClassName(short_class_name, class_package + ".",
   183       String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
   195                         true); // Remove class package prefix
   184       short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); // Remove class package prefix
   196                 ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name
   185 
   197                         + "</A>";
   186       ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>";
   198                 constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index
   187       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index +
   199                         + "\" TARGET=ConstantPool>" + short_class_name + "</A>";
   188         "\" TARGET=ConstantPool>" + short_class_name + "</A>";
   200                 file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
   189 
   201                         + "\">Name index(" + name_index + ")</A></UL>\n");
   190       file.println("<P><TT>" + ref + "</TT><UL>" +
   202                 break;
   191                    "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index +   ")</A></UL>\n");
   203             case Const.CONSTANT_String:
   192       break;
   204                 final ConstantString c5 = (ConstantString) constant_pool.getConstant(index,
   193 
   205                         Const.CONSTANT_String);
   194     case CONSTANT_String:
   206                 name_index = c5.getStringIndex();
   195       ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String);
   207                 final String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
   196       name_index = c5.getStringIndex();
   208                 file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
   197 
   209                         + "\">Name index(" + name_index + ")</A></UL>\n");
   198       String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
   210                 break;
   199 
   211             case Const.CONSTANT_NameAndType:
   200       file.println("<P><TT>" + str + "</TT><UL>" +
   212                 final ConstantNameAndType c6 = (ConstantNameAndType) constant_pool.getConstant(index,
   201                    "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index +   ")</A></UL>\n");
   213                         Const.CONSTANT_NameAndType);
   202       break;
   214                 name_index = c6.getNameIndex();
   203 
   215                 final int signature_index = c6.getSignatureIndex();
   204     case CONSTANT_NameAndType:
   216                 file.println("<P><TT>"
   205       ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType);
   217                         + Class2HTML.toHTML(constant_pool.constantToString(index, tag))
   206       name_index = c6.getNameIndex();
   218                         + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index("
   207       int signature_index = c6.getSignatureIndex();
   219                         + name_index + ")</A>\n" + "<LI><A HREF=\"#cp" + signature_index
   208 
   220                         + "\">Signature index(" + signature_index + ")</A></UL>\n");
   209       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" +
   221                 break;
   210                    "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" +
   222             default:
   211                    "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" +
   223                 file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n");
   212                    signature_index + ")</A></UL>\n");
   224         } // switch
   213       break;
   225     }
   214 
   226 
   215     default:
   227 
   216       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n");
   228     private int getMethodNumber( final String str ) {
   217     } // switch
   229         for (int i = 0; i < methods.length; i++) {
   218   }
   230             final String cmp = methods[i].getName() + methods[i].getSignature();
   219 
   231             if (cmp.equals(str)) {
   220   private final int getMethodNumber(String str) {
   232                 return i;
   221     for(int i=0; i < methods.length; i++) {
   233             }
   222       String cmp = methods[i].getName() + methods[i].getSignature();
   234         }
   223       if(cmp.equals(str))
   235         return -1;
   224         return i;
   236     }
   225     }
       
   226     return -1;
       
   227   }
       
   228 }
   237 }