1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39 final class ConstantHTML {
40
41 private final String className;
42 private final String classPackage;
43 private final ConstantPool constantPool;
44 private final PrintWriter printWriter;
45 private final String[] constantRef;
46 private final Constant[] constants;
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
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
100 printWriter.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + Const.getConstantName(tag) + "</H4>");
101
102
103
104 switch (tag) {
105 case Const.CONSTANT_InterfaceMethodref:
106 case Const.CONSTANT_Methodref:
107
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
118 final String methodName = constantPool.constantToString(nameIndex, Const.CONSTANT_NameAndType);
119 final String htmlMethodName = Class2HTML.toHTML(methodName);
120
121 final String methodClass = constantPool.constantToString(classIndex, Const.CONSTANT_Class);
122 String shortMethodClass = Utility.compactClassName(methodClass);
123 shortMethodClass = Utility.compactClassName(shortMethodClass, classPackage + ".", true);
124
125 final ConstantNameAndType c2 = constantPool.getConstant(nameIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
126 final String signature = constantPool.constantToString(c2.getSignatureIndex(), Const.CONSTANT_Utf8);
127
128 final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
129
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(", ");
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 + " <A HREF=\"" + className + "_cp.html#cp" + classIndex + "\" TARGET=Constants>" + shortMethodClass
147 + "</A>.<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + htmlMethodName + "</A> " + argTypes;
148 printWriter.println("<P><TT>" + retType + " " + ref + argTypes + " </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
153 final ConstantFieldref c3 = constantPool.getConstant(index, Const.CONSTANT_Fieldref, ConstantFieldref.class);
154 classIndex = c3.getClassIndex();
155 nameIndex = c3.getNameAndTypeIndex();
156
157 final String fieldClass = constantPool.constantToString(classIndex, Const.CONSTANT_Class);
158 String shortFieldClass = Utility.compactClassName(fieldClass);
159 shortFieldClass = Utility.compactClassName(shortFieldClass, classPackage + ".", true);
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);
176 shortClassName = Utility.compactClassName(shortClassName, classPackage + ".", true);
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 }
197 }
198 }