View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.util;
18  
19  import java.io.FileNotFoundException;
20  import java.io.PrintWriter;
21  import java.io.UnsupportedEncodingException;
22  import java.nio.charset.Charset;
23  
24  import org.apache.bcel.Const;
25  import org.apache.bcel.classfile.Constant;
26  import org.apache.bcel.classfile.ConstantClass;
27  import org.apache.bcel.classfile.ConstantFieldref;
28  import org.apache.bcel.classfile.ConstantInterfaceMethodref;
29  import org.apache.bcel.classfile.ConstantMethodref;
30  import org.apache.bcel.classfile.ConstantNameAndType;
31  import org.apache.bcel.classfile.ConstantPool;
32  import org.apache.bcel.classfile.ConstantString;
33  import org.apache.bcel.classfile.Method;
34  import org.apache.bcel.classfile.Utility;
35  
36  /**
37   * Convert constant pool into HTML file.
38   */
39  final class ConstantHTML {
40  
41      private final String className; // name of current class
42      private final String classPackage; // name of package
43      private final ConstantPool constantPool; // reference to constant pool
44      private final PrintWriter printWriter; // file to write to
45      private final String[] constantRef; // String to return for cp[i]
46      private final Constant[] constants; // The constants in the cp
47      private final Method[] methods;
48  
49      ConstantHTML(final String dir, final String className, final String classPackage, final Method[] methods, final ConstantPool constantPool,
50          final Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
51          this.className = className;
52          this.classPackage = classPackage;
53          this.constantPool = constantPool;
54          this.methods = methods;
55          this.constants = constantPool.getConstantPool();
56          try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_cp.html", charset.name())) {
57              printWriter = newPrintWriter;
58              constantRef = new String[constants.length];
59              constantRef[0] = "<unknown>";
60              printWriter.print("<HTML><head><meta charset=\"");
61              printWriter.print(charset.name());
62              printWriter.println("\"></head>");
63              printWriter.println("<BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
64              // Loop through constants, constants[0] is reserved
65              for (int i = 1; i < constants.length; i++) {
66                  if (i % 2 == 0) {
67                      printWriter.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
68                  } else {
69                      printWriter.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
70                  }
71                  if (constants[i] != null) {
72                      writeConstant(i);
73                  }
74                  printWriter.print("</TD></TR>\n");
75              }
76              printWriter.println("</TABLE></BODY></HTML>");
77          }
78      }
79  
80      private int getMethodNumber(final String str) {
81          for (int i = 0; i < methods.length; i++) {
82              final String cmp = methods[i].getName() + methods[i].getSignature();
83              if (cmp.equals(str)) {
84                  return i;
85              }
86          }
87          return -1;
88      }
89  
90      String referenceConstant(final int index) {
91          return constantRef[index];
92      }
93  
94      private void writeConstant(final int index) {
95          final byte tag = constants[index].getTag();
96          int classIndex;
97          int nameIndex;
98          String ref;
99          // The header is always the same
100         printWriter.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag) + "</H4>");
101         /*
102          * For every constant type get the needed parameters and print them appropriately
103          */
104         switch (tag) {
105         case Const.CONSTANT_InterfaceMethodref:
106         case Const.CONSTANT_Methodref:
107             // Get class_index and name_and_type_index, depending on type
108             if (tag == Const.CONSTANT_Methodref) {
109                 final ConstantMethodref c = constantPool.getConstant(index, Const.CONSTANT_Methodref, ConstantMethodref.class);
110                 classIndex = c.getClassIndex();
111                 nameIndex = c.getNameAndTypeIndex();
112             } else {
113                 final ConstantInterfaceMethodref c1 = constantPool.getConstant(index, Const.CONSTANT_InterfaceMethodref, ConstantInterfaceMethodref.class);
114                 classIndex = c1.getClassIndex();
115                 nameIndex = c1.getNameAndTypeIndex();
116             }
117             // Get method name and its class
118             final String methodName = constantPool.constantToString(nameIndex, Const.CONSTANT_NameAndType);
119             final String htmlMethodName = Class2HTML.toHTML(methodName);
120             // Partially compacted class name, i.e., / -> .
121             final String methodClass = constantPool.constantToString(classIndex, Const.CONSTANT_Class);
122             String shortMethodClass = Utility.compactClassName(methodClass); // I.e., remove java.lang.
123             shortMethodClass = Utility.compactClassName(shortMethodClass, classPackage + ".", true); // Remove class package prefix
124             // Get method signature
125             final ConstantNameAndType c2 = constantPool.getConstant(nameIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
126             final String signature = constantPool.constantToString(c2.getSignatureIndex(), Const.CONSTANT_Utf8);
127             // Get array of strings containing the argument types
128             final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
129             // Get return type string
130             final String type = Utility.methodSignatureReturnType(signature, false);
131             final String retType = Class2HTML.referenceType(type);
132             final StringBuilder buf = new StringBuilder("(");
133             for (int i = 0; i < args.length; i++) {
134                 buf.append(Class2HTML.referenceType(args[i]));
135                 if (i < args.length - 1) {
136                     buf.append(",&nbsp;");
137                 }
138             }
139             buf.append(")");
140             final String argTypes = buf.toString();
141             if (methodClass.equals(className)) {
142                 ref = "<A HREF=\"" + className + "_code.html#method" + getMethodNumber(methodName + signature) + "\" TARGET=Code>" + htmlMethodName + "</A>";
143             } else {
144                 ref = "<A HREF=\"" + methodClass + ".html" + "\" TARGET=_top>" + shortMethodClass + "</A>." + htmlMethodName;
145             }
146             constantRef[index] = retType + "&nbsp;<A HREF=\"" + className + "_cp.html#cp" + classIndex + "\" TARGET=Constants>" + shortMethodClass
147                 + "</A>.<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + htmlMethodName + "</A>&nbsp;" + argTypes;
148             printWriter.println("<P><TT>" + retType + "&nbsp;" + ref + argTypes + "&nbsp;</TT>\n<UL>" + "<LI><A HREF=\"#cp" + classIndex + "\">Class index("
149                 + classIndex + ")</A>\n" + "<LI><A HREF=\"#cp" + nameIndex + "\">NameAndType index(" + nameIndex + ")</A></UL>");
150             break;
151         case Const.CONSTANT_Fieldref:
152             // Get class_index and name_and_type_index
153             final ConstantFieldref c3 = constantPool.getConstant(index, Const.CONSTANT_Fieldref, ConstantFieldref.class);
154             classIndex = c3.getClassIndex();
155             nameIndex = c3.getNameAndTypeIndex();
156             // Get method name and its class (compacted)
157             final String fieldClass = constantPool.constantToString(classIndex, Const.CONSTANT_Class);
158             String shortFieldClass = Utility.compactClassName(fieldClass); // I.e., remove java.lang.
159             shortFieldClass = Utility.compactClassName(shortFieldClass, classPackage + ".", true); // Remove class package prefix
160             final String fieldName = constantPool.constantToString(nameIndex, Const.CONSTANT_NameAndType);
161             if (fieldClass.equals(className)) {
162                 ref = "<A HREF=\"" + fieldClass + "_methods.html#field" + fieldName + "\" TARGET=Methods>" + fieldName + "</A>";
163             } else {
164                 ref = "<A HREF=\"" + fieldClass + ".html\" TARGET=_top>" + shortFieldClass + "</A>." + fieldName + "\n";
165             }
166             constantRef[index] = "<A HREF=\"" + className + "_cp.html#cp" + classIndex + "\" TARGET=Constants>" + shortFieldClass + "</A>.<A HREF=\""
167                 + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + fieldName + "</A>";
168             printWriter.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp" + classIndex + "\">Class(" + classIndex + ")</A><BR>\n"
169                 + "<LI><A HREF=\"#cp" + nameIndex + "\">NameAndType(" + nameIndex + ")</A></UL>");
170             break;
171         case Const.CONSTANT_Class:
172             final ConstantClass c4 = constantPool.getConstant(index, Const.CONSTANT_Class, ConstantClass.class);
173             nameIndex = c4.getNameIndex();
174             final String className2 = constantPool.constantToString(index, tag); // / -> .
175             String shortClassName = Utility.compactClassName(className2); // I.e., remove java.lang.
176             shortClassName = Utility.compactClassName(shortClassName, classPackage + ".", true); // Remove class package prefix
177             ref = "<A HREF=\"" + className2 + ".html\" TARGET=_top>" + shortClassName + "</A>";
178             constantRef[index] = "<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + shortClassName + "</A>";
179             printWriter.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + nameIndex + "\">Name index(" + nameIndex + ")</A></UL>\n");
180             break;
181         case Const.CONSTANT_String:
182             final ConstantString c5 = constantPool.getConstant(index, Const.CONSTANT_String, ConstantString.class);
183             nameIndex = c5.getStringIndex();
184             final String str = Class2HTML.toHTML(constantPool.constantToString(index, tag));
185             printWriter.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + nameIndex + "\">Name index(" + nameIndex + ")</A></UL>\n");
186             break;
187         case Const.CONSTANT_NameAndType:
188             final ConstantNameAndType c6 = constantPool.getConstant(index, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
189             nameIndex = c6.getNameIndex();
190             final int signatureIndex = c6.getSignatureIndex();
191             printWriter.println("<P><TT>" + Class2HTML.toHTML(constantPool.constantToString(index, tag)) + "</TT><UL>" + "<LI><A HREF=\"#cp" + nameIndex
192                 + "\">Name index(" + nameIndex + ")</A>\n" + "<LI><A HREF=\"#cp" + signatureIndex + "\">Signature index(" + signatureIndex + ")</A></UL>\n");
193             break;
194         default:
195             printWriter.println("<P><TT>" + Class2HTML.toHTML(constantPool.constantToString(index, tag)) + "</TT>\n");
196         } // switch
197     }
198 }