1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.util;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.nio.charset.Charset;
24 import java.util.BitSet;
25
26 import org.apache.bcel.Const;
27 import org.apache.bcel.classfile.Attribute;
28 import org.apache.bcel.classfile.ClassFormatException;
29 import org.apache.bcel.classfile.Code;
30 import org.apache.bcel.classfile.CodeException;
31 import org.apache.bcel.classfile.ConstantFieldref;
32 import org.apache.bcel.classfile.ConstantInterfaceMethodref;
33 import org.apache.bcel.classfile.ConstantInvokeDynamic;
34 import org.apache.bcel.classfile.ConstantMethodref;
35 import org.apache.bcel.classfile.ConstantNameAndType;
36 import org.apache.bcel.classfile.ConstantPool;
37 import org.apache.bcel.classfile.LocalVariableTable;
38 import org.apache.bcel.classfile.Method;
39 import org.apache.bcel.classfile.Utility;
40
41
42
43
44 final class CodeHTML {
45
46 private static boolean wide;
47 private final String className;
48
49 private final PrintWriter printWriter;
50 private BitSet gotoSet;
51 private final ConstantPool constantPool;
52 private final ConstantHTML constantHtml;
53
54 CodeHTML(final String dir, final String className, final Method[] methods, final ConstantPool constantPool, final ConstantHTML constantHtml,
55 final Charset charset) throws IOException {
56 this.className = className;
57
58 this.constantPool = constantPool;
59 this.constantHtml = constantHtml;
60 try (PrintWriter newPrintWriter = new PrintWriter(dir + className + "_code.html", charset.name())) {
61 printWriter = newPrintWriter;
62 printWriter.print("<HTML><head><meta charset=\"");
63 printWriter.print(charset.name());
64 printWriter.println("\"></head>");
65 printWriter.println("<BODY BGCOLOR=\"#C0C0C0\">");
66 for (int i = 0; i < methods.length; i++) {
67 writeMethod(methods[i], i);
68 }
69 printWriter.println("</BODY></HTML>");
70 }
71 }
72
73
74
75
76
77
78
79 private String codeToHTML(final ByteSequence bytes, final int methodNumber) throws IOException {
80 final short opcode = (short) bytes.readUnsignedByte();
81 String name;
82 final String signature;
83 int defaultOffset = 0;
84 final int low;
85 final int high;
86 int index;
87 final int classIndex;
88 final int vindex;
89 final int constant;
90 final int[] jumpTable;
91 int noPadBytes = 0;
92 final int offset;
93 final StringBuilder buf = new StringBuilder(256);
94 buf.append("<TT>").append(Const.getOpcodeName(opcode)).append("</TT></TD><TD>");
95
96
97
98 if (opcode == Const.TABLESWITCH || opcode == Const.LOOKUPSWITCH) {
99 final int remainder = bytes.getIndex() % 4;
100 noPadBytes = remainder == 0 ? 0 : 4 - remainder;
101 for (int i = 0; i < noPadBytes; i++) {
102 bytes.readByte();
103 }
104
105 defaultOffset = bytes.readInt();
106 }
107 switch (opcode) {
108 case Const.TABLESWITCH:
109 low = bytes.readInt();
110 high = bytes.readInt();
111 offset = bytes.getIndex() - 12 - noPadBytes - 1;
112 defaultOffset += offset;
113
114
115
116 final long jumpTableLength = (long) high - low + 1;
117 if (jumpTableLength < 0 || jumpTableLength * 4 > bytes.available()) {
118 throw new ClassFormatException("Invalid TABLESWITCH: low = " + low + ", high = " + high + " but only " + bytes.available() + " bytes remain");
119 }
120 buf.append("<TABLE BORDER=1><TR>");
121
122 jumpTable = new int[(int) jumpTableLength];
123 for (int i = 0; i < jumpTable.length; i++) {
124 jumpTable[i] = offset + bytes.readInt();
125 buf.append("<TH>").append(low + i).append("</TH>");
126 }
127 buf.append("<TH>default</TH></TR>\n<TR>");
128
129 for (final int element : jumpTable) {
130 buf.append("<TD><A HREF=\"#code").append(methodNumber).append("@").append(element).append("\">").append(element).append("</A></TD>");
131 }
132 buf.append("<TD><A HREF=\"#code").append(methodNumber).append("@").append(defaultOffset).append("\">").append(defaultOffset)
133 .append("</A></TD></TR>\n</TABLE>\n");
134 break;
135
136
137
138 case Const.LOOKUPSWITCH:
139 final int npairs = bytes.readInt();
140 offset = bytes.getIndex() - 8 - noPadBytes - 1;
141
142 if (npairs < 0 || (long) npairs * 8 > bytes.available()) {
143 throw new ClassFormatException("Invalid LOOKUPSWITCH: npairs = " + npairs + " but only " + bytes.available() + " bytes remain");
144 }
145 jumpTable = new int[npairs];
146 defaultOffset += offset;
147 buf.append("<TABLE BORDER=1><TR>");
148
149 for (int i = 0; i < npairs; i++) {
150 final int match = bytes.readInt();
151 jumpTable[i] = offset + bytes.readInt();
152 buf.append("<TH>").append(match).append("</TH>");
153 }
154 buf.append("<TH>default</TH></TR>\n<TR>");
155
156 for (int i = 0; i < npairs; i++) {
157 buf.append("<TD><A HREF=\"#code").append(methodNumber).append("@").append(jumpTable[i]).append("\">").append(jumpTable[i])
158 .append("</A></TD>");
159 }
160 buf.append("<TD><A HREF=\"#code").append(methodNumber).append("@").append(defaultOffset).append("\">").append(defaultOffset)
161 .append("</A></TD></TR>\n</TABLE>\n");
162 break;
163
164
165
166 case Const.GOTO:
167 case Const.IFEQ:
168 case Const.IFGE:
169 case Const.IFGT:
170 case Const.IFLE:
171 case Const.IFLT:
172 case Const.IFNE:
173 case Const.IFNONNULL:
174 case Const.IFNULL:
175 case Const.IF_ACMPEQ:
176 case Const.IF_ACMPNE:
177 case Const.IF_ICMPEQ:
178 case Const.IF_ICMPGE:
179 case Const.IF_ICMPGT:
180 case Const.IF_ICMPLE:
181 case Const.IF_ICMPLT:
182 case Const.IF_ICMPNE:
183 case Const.JSR:
184 index = bytes.getIndex() + bytes.readShort() - 1;
185 buf.append("<A HREF=\"#code").append(methodNumber).append("@").append(index).append("\">").append(index).append("</A>");
186 break;
187
188
189
190 case Const.GOTO_W:
191 case Const.JSR_W:
192 final int windex = bytes.getIndex() + bytes.readInt() - 1;
193 buf.append("<A HREF=\"#code").append(methodNumber).append("@").append(windex).append("\">").append(windex).append("</A>");
194 break;
195
196
197
198 case Const.ALOAD:
199 case Const.ASTORE:
200 case Const.DLOAD:
201 case Const.DSTORE:
202 case Const.FLOAD:
203 case Const.FSTORE:
204 case Const.ILOAD:
205 case Const.ISTORE:
206 case Const.LLOAD:
207 case Const.LSTORE:
208 case Const.RET:
209 if (wide) {
210 vindex = bytes.readUnsignedShort();
211 wide = false;
212 } else {
213 vindex = bytes.readUnsignedByte();
214 }
215 buf.append("%").append(vindex);
216 break;
217
218
219
220
221 case Const.WIDE:
222 wide = true;
223 buf.append("(wide)");
224 break;
225
226
227
228 case Const.NEWARRAY:
229 buf.append("<FONT COLOR=\"#00FF00\">").append(Const.getTypeName(bytes.readByte())).append("</FONT>");
230 break;
231
232
233
234 case Const.GETFIELD:
235 case Const.GETSTATIC:
236 case Const.PUTFIELD:
237 case Const.PUTSTATIC:
238 index = bytes.readUnsignedShort();
239 final ConstantFieldref c1 = constantPool.getConstant(index, Const.CONSTANT_Fieldref, ConstantFieldref.class);
240 classIndex = c1.getClassIndex();
241 name = constantPool.getConstantString(classIndex, Const.CONSTANT_Class);
242 name = Utility.compactClassName(name, false);
243 index = c1.getNameAndTypeIndex();
244 final String fieldName = constantPool.constantToString(index, Const.CONSTANT_NameAndType);
245 if (name.equals(className)) {
246 buf.append("<A HREF=\"").append(className).append("_methods.html#field").append(Class2HTML.toHTML(fieldName)).append("\" TARGET=Methods>")
247 .append(Class2HTML.toHTML(fieldName)).append("</A>\n");
248 } else {
249 buf.append(constantHtml.referenceConstant(classIndex)).append(".").append(Class2HTML.toHTML(fieldName));
250 }
251 break;
252
253
254
255 case Const.CHECKCAST:
256 case Const.INSTANCEOF:
257 case Const.NEW:
258 index = bytes.readUnsignedShort();
259 buf.append(constantHtml.referenceConstant(index));
260 break;
261
262
263
264 case Const.INVOKESPECIAL:
265 case Const.INVOKESTATIC:
266 case Const.INVOKEVIRTUAL:
267 case Const.INVOKEINTERFACE:
268 case Const.INVOKEDYNAMIC:
269 final int mIndex = bytes.readUnsignedShort();
270 final String str;
271 if (opcode == Const.INVOKEINTERFACE) {
272 bytes.readUnsignedByte();
273 bytes.readUnsignedByte();
274
275
276 final ConstantInterfaceMethodref c = constantPool.getConstant(mIndex, Const.CONSTANT_InterfaceMethodref, ConstantInterfaceMethodref.class);
277 classIndex = c.getClassIndex();
278 index = c.getNameAndTypeIndex();
279 name = Class2HTML.referenceClass(classIndex);
280 } else if (opcode == Const.INVOKEDYNAMIC) {
281 bytes.readUnsignedByte();
282 bytes.readUnsignedByte();
283 final ConstantInvokeDynamic c = constantPool.getConstant(mIndex, Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
284 index = c.getNameAndTypeIndex();
285 name = "#" + c.getBootstrapMethodAttrIndex();
286 } else {
287
288
289
290 final ConstantMethodref c = constantPool.getConstant(mIndex, Const.CONSTANT_Methodref, ConstantMethodref.class);
291 classIndex = c.getClassIndex();
292 index = c.getNameAndTypeIndex();
293 name = Class2HTML.referenceClass(classIndex);
294 }
295 str = Class2HTML.toHTML(constantPool.constantToString(constantPool.getConstant(index, Const.CONSTANT_NameAndType)));
296
297 final ConstantNameAndType c2 = constantPool.getConstant(index, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
298 signature = constantPool.constantToString(c2.getSignatureIndex(), Const.CONSTANT_Utf8);
299 final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
300 final String type = Utility.methodSignatureReturnType(signature, false);
301 buf.append(name).append(".<A HREF=\"").append(className).append("_cp.html#cp").append(mIndex).append("\" TARGET=ConstantPool>").append(str)
302 .append("</A>").append("(");
303
304 for (int i = 0; i < args.length; i++) {
305 buf.append(Class2HTML.referenceType(args[i]));
306 if (i < args.length - 1) {
307 buf.append(", ");
308 }
309 }
310
311 buf.append("):").append(Class2HTML.referenceType(type));
312 break;
313
314
315
316 case Const.LDC_W:
317 case Const.LDC2_W:
318 index = bytes.readUnsignedShort();
319 buf.append("<A HREF=\"").append(className).append("_cp.html#cp").append(index).append("\" TARGET=\"ConstantPool\">")
320 .append(Class2HTML.toHTML(constantPool.constantToString(index, constantPool.getConstant(index).getTag()))).append("</a>");
321 break;
322 case Const.LDC:
323 index = bytes.readUnsignedByte();
324 buf.append("<A HREF=\"").append(className).append("_cp.html#cp").append(index).append("\" TARGET=\"ConstantPool\">")
325 .append(Class2HTML.toHTML(constantPool.constantToString(index, constantPool.getConstant(index).getTag()))).append("</a>");
326 break;
327
328
329
330 case Const.ANEWARRAY:
331 index = bytes.readUnsignedShort();
332 buf.append(constantHtml.referenceConstant(index));
333 break;
334
335
336
337 case Const.MULTIANEWARRAY:
338 index = bytes.readUnsignedShort();
339 final int dimensions = bytes.readUnsignedByte();
340 buf.append(constantHtml.referenceConstant(index)).append(":").append(dimensions).append("-dimensional");
341 break;
342
343
344
345 case Const.IINC:
346 if (wide) {
347 vindex = bytes.readUnsignedShort();
348 constant = bytes.readShort();
349 wide = false;
350 } else {
351 vindex = bytes.readUnsignedByte();
352 constant = bytes.readByte();
353 }
354 buf.append("%").append(vindex).append(" ").append(constant);
355 break;
356 default:
357 if (Const.getNoOfOperands(opcode) > 0) {
358 for (int i = 0; i < Const.getOperandTypeCount(opcode); i++) {
359 switch (Const.getOperandType(opcode, i)) {
360 case Const.T_BYTE:
361 buf.append(bytes.readUnsignedByte());
362 break;
363 case Const.T_SHORT:
364 buf.append(bytes.readShort());
365 break;
366 case Const.T_INT:
367 buf.append(bytes.readInt());
368 break;
369 default:
370 throw new IllegalStateException("Unreachable default case reached! " + Const.getOperandType(opcode, i));
371 }
372 buf.append(" ");
373 }
374 }
375 }
376 buf.append("</TD>");
377 return buf.toString();
378 }
379
380
381
382
383
384 private void findGotos(final ByteSequence bytes, final Code code) throws IOException {
385 int index;
386 gotoSet = new BitSet(bytes.available());
387 int opcode;
388
389
390
391
392 if (code != null) {
393 final CodeException[] ce = code.getExceptionTable();
394 for (final CodeException cex : ce) {
395 gotoSet.set(cex.getStartPC());
396 gotoSet.set(cex.getEndPC());
397 gotoSet.set(cex.getHandlerPC());
398 }
399
400 final Attribute[] attributes = code.getAttributes();
401 for (final Attribute attribute : attributes) {
402 if (attribute.getTag() == Const.ATTR_LOCAL_VARIABLE_TABLE) {
403 ((LocalVariableTable) attribute).forEach(var -> {
404 final int start = var.getStartPC();
405 gotoSet.set(start);
406 gotoSet.set(start + var.getLength());
407 });
408 break;
409 }
410 }
411 }
412
413 while (bytes.available() > 0) {
414 opcode = bytes.readUnsignedByte();
415
416 switch (opcode) {
417 case Const.TABLESWITCH:
418 case Const.LOOKUPSWITCH:
419
420 final int remainder = bytes.getIndex() % 4;
421 final int noPadBytes = remainder == 0 ? 0 : 4 - remainder;
422 int defaultOffset;
423 final int offset;
424 for (int j = 0; j < noPadBytes; j++) {
425 bytes.readByte();
426 }
427
428 defaultOffset = bytes.readInt();
429 if (opcode == Const.TABLESWITCH) {
430 final int low = bytes.readInt();
431 final int high = bytes.readInt();
432 offset = bytes.getIndex() - 12 - noPadBytes - 1;
433 defaultOffset += offset;
434 gotoSet.set(defaultOffset);
435 for (int j = 0; j < high - low + 1; j++) {
436 index = offset + bytes.readInt();
437 gotoSet.set(index);
438 }
439 } else {
440 final int npairs = bytes.readInt();
441 offset = bytes.getIndex() - 8 - noPadBytes - 1;
442 defaultOffset += offset;
443 gotoSet.set(defaultOffset);
444 for (int j = 0; j < npairs; j++) {
445
446 bytes.readInt();
447 index = offset + bytes.readInt();
448 gotoSet.set(index);
449 }
450 }
451 break;
452 case Const.GOTO:
453 case Const.IFEQ:
454 case Const.IFGE:
455 case Const.IFGT:
456 case Const.IFLE:
457 case Const.IFLT:
458 case Const.IFNE:
459 case Const.IFNONNULL:
460 case Const.IFNULL:
461 case Const.IF_ACMPEQ:
462 case Const.IF_ACMPNE:
463 case Const.IF_ICMPEQ:
464 case Const.IF_ICMPGE:
465 case Const.IF_ICMPGT:
466 case Const.IF_ICMPLE:
467 case Const.IF_ICMPLT:
468 case Const.IF_ICMPNE:
469 case Const.JSR:
470
471 index = bytes.getIndex() + bytes.readShort() - 1;
472 gotoSet.set(index);
473 break;
474 case Const.GOTO_W:
475 case Const.JSR_W:
476
477 index = bytes.getIndex() + bytes.readInt() - 1;
478 gotoSet.set(index);
479 break;
480 default:
481 bytes.unreadByte();
482 codeToHTML(bytes, 0);
483 }
484 }
485 }
486
487
488
489
490 private void writeMethod(final Method method, final int methodNumber) throws IOException {
491
492 final String signature = method.getSignature();
493
494 final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
495
496 final String type = Utility.methodSignatureReturnType(signature, false);
497
498 final String name = method.getName();
499 final String htmlName = Class2HTML.toHTML(name);
500
501 String access = Utility.accessToString(method.getAccessFlags());
502 access = Utility.replace(access, " ", " ");
503
504 final Attribute[] attributes = method.getAttributes();
505 printWriter.print("<P><B><FONT COLOR=\"#FF0000\">" + access + "</FONT> <A NAME=method" + methodNumber + ">" + Class2HTML.referenceType(type)
506 + "</A> <A HREF=\"" + className + "_methods.html#method" + methodNumber + "\" TARGET=Methods>" + htmlName + "</A>(");
507 for (int i = 0; i < args.length; i++) {
508 printWriter.print(Class2HTML.referenceType(args[i]));
509 if (i < args.length - 1) {
510 printWriter.print(", ");
511 }
512 }
513 printWriter.println(")</B></P>");
514 Code c = null;
515 byte[] code = null;
516 if (attributes.length > 0) {
517 printWriter.print("<H4>Attributes</H4><UL>\n");
518 for (int i = 0; i < attributes.length; i++) {
519 byte tag = attributes[i].getTag();
520 if (tag != Const.ATTR_UNKNOWN) {
521 printWriter.print("<LI><A HREF=\"" + className + "_attributes.html#method" + methodNumber + "@" + i + "\" TARGET=Attributes>"
522 + Const.getAttributeName(tag) + "</A></LI>\n");
523 } else {
524 printWriter.print("<LI>" + attributes[i] + "</LI>");
525 }
526 if (tag == Const.ATTR_CODE) {
527 c = (Code) attributes[i];
528 final Attribute[] attributes2 = c.getAttributes();
529 code = c.getCode();
530 printWriter.print("<UL>");
531 for (int j = 0; j < attributes2.length; j++) {
532 tag = attributes2[j].getTag();
533 printWriter.print("<LI><A HREF=\"" + className + "_attributes.html#method" + methodNumber + "@" + i + "@" + j
534 + "\" TARGET=Attributes>" + Const.getAttributeName(tag) + "</A></LI>\n");
535 }
536 printWriter.print("</UL>");
537 }
538 }
539 printWriter.println("</UL>");
540 }
541 if (code != null) {
542
543
544 try (ByteSequence stream = new ByteSequence(code)) {
545 stream.mark(stream.available());
546 findGotos(stream, c);
547 stream.reset();
548 printWriter.println("<TABLE BORDER=0><TR><TH ALIGN=LEFT>Byte<BR>offset</TH><TH ALIGN=LEFT>Instruction</TH><TH ALIGN=LEFT>Argument</TH>");
549 while (stream.available() > 0) {
550 final int offset = stream.getIndex();
551 final String str = codeToHTML(stream, methodNumber);
552 String anchor = "";
553
554
555
556
557 if (gotoSet.get(offset)) {
558 anchor = "<A NAME=code" + methodNumber + "@" + offset + "></A>";
559 }
560 final String anchor2;
561 if (stream.getIndex() == code.length) {
562 anchor2 = "<A NAME=code" + methodNumber + "@" + code.length + ">" + offset + "</A>";
563 } else {
564 anchor2 = "" + offset;
565 }
566 printWriter.println("<TR VALIGN=TOP><TD>" + anchor2 + "</TD><TD>" + anchor + str + "</TR>");
567 }
568 }
569
570 printWriter.println("<TR><TD> </A></TD></TR>");
571 printWriter.println("</TABLE>");
572 }
573 }
574 }