You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InstructionPrinter.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.PrintStream;
  18. import javassist.CtMethod;
  19. /**
  20. * Simple utility class for printing the bytecode instructions of a method.
  21. *
  22. * @author Jason T. Greene
  23. */
  24. public class InstructionPrinter implements Opcode {
  25. private final static String opcodes[] = Mnemonic.OPCODE;
  26. private final PrintStream stream;
  27. /**
  28. * Constructs a <code>InstructionPrinter</code> object.
  29. */
  30. public InstructionPrinter(PrintStream stream) {
  31. this.stream = stream;
  32. }
  33. /**
  34. * Prints the bytecode instructions of a given method.
  35. */
  36. public static void print(CtMethod method, PrintStream stream) {
  37. (new InstructionPrinter(stream)).print(method);
  38. }
  39. /**
  40. * Prints the bytecode instructions of a given method.
  41. */
  42. public void print(CtMethod method) {
  43. MethodInfo info = method.getMethodInfo2();
  44. ConstPool pool = info.getConstPool();
  45. CodeAttribute code = info.getCodeAttribute();
  46. if (code == null)
  47. return;
  48. CodeIterator iterator = code.iterator();
  49. while (iterator.hasNext()) {
  50. int pos;
  51. try {
  52. pos = iterator.next();
  53. } catch (BadBytecode e) {
  54. throw new RuntimeException(e);
  55. }
  56. stream.println(pos + ": " + instructionString(iterator, pos, pool));
  57. }
  58. }
  59. /**
  60. * Gets a string representation of the bytecode instruction at the specified
  61. * position.
  62. */
  63. public static String instructionString(CodeIterator iter, int pos, ConstPool pool) {
  64. int opcode = iter.byteAt(pos);
  65. if (opcode > opcodes.length || opcode < 0)
  66. throw new IllegalArgumentException("Invalid opcode, opcode: " + opcode + " pos: "+ pos);
  67. String opstring = opcodes[opcode];
  68. switch (opcode) {
  69. case BIPUSH:
  70. return opstring + " " + iter.byteAt(pos + 1);
  71. case SIPUSH:
  72. return opstring + " " + iter.s16bitAt(pos + 1);
  73. case LDC:
  74. return opstring + " " + ldc(pool, iter.byteAt(pos + 1));
  75. case LDC_W :
  76. case LDC2_W :
  77. return opstring + " " + ldc(pool, iter.u16bitAt(pos + 1));
  78. case ILOAD:
  79. case LLOAD:
  80. case FLOAD:
  81. case DLOAD:
  82. case ALOAD:
  83. case ISTORE:
  84. case LSTORE:
  85. case FSTORE:
  86. case DSTORE:
  87. case ASTORE:
  88. return opstring + " " + iter.byteAt(pos + 1);
  89. case IFEQ:
  90. case IFGE:
  91. case IFGT:
  92. case IFLE:
  93. case IFLT:
  94. case IFNE:
  95. case IFNONNULL:
  96. case IFNULL:
  97. case IF_ACMPEQ:
  98. case IF_ACMPNE:
  99. case IF_ICMPEQ:
  100. case IF_ICMPGE:
  101. case IF_ICMPGT:
  102. case IF_ICMPLE:
  103. case IF_ICMPLT:
  104. case IF_ICMPNE:
  105. return opstring + " " + (iter.s16bitAt(pos + 1) + pos);
  106. case IINC:
  107. return opstring + " " + iter.byteAt(pos + 1) + ", " + iter.signedByteAt(pos + 2);
  108. case GOTO:
  109. case JSR:
  110. return opstring + " " + (iter.s16bitAt(pos + 1) + pos);
  111. case RET:
  112. return opstring + " " + iter.byteAt(pos + 1);
  113. case TABLESWITCH:
  114. return tableSwitch(iter, pos);
  115. case LOOKUPSWITCH:
  116. return lookupSwitch(iter, pos);
  117. case GETSTATIC:
  118. case PUTSTATIC:
  119. case GETFIELD:
  120. case PUTFIELD:
  121. return opstring + " " + fieldInfo(pool, iter.u16bitAt(pos + 1));
  122. case INVOKEVIRTUAL:
  123. case INVOKESPECIAL:
  124. case INVOKESTATIC:
  125. return opstring + " " + methodInfo(pool, iter.u16bitAt(pos + 1));
  126. case INVOKEINTERFACE:
  127. return opstring + " " + interfaceMethodInfo(pool, iter.u16bitAt(pos + 1));
  128. case INVOKEDYNAMIC:
  129. return opstring + " " + iter.u16bitAt(pos + 1);
  130. case NEW:
  131. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  132. case NEWARRAY:
  133. return opstring + " " + arrayInfo(iter.byteAt(pos + 1));
  134. case ANEWARRAY:
  135. case CHECKCAST:
  136. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  137. case WIDE:
  138. return wide(iter, pos);
  139. case MULTIANEWARRAY:
  140. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  141. case GOTO_W:
  142. case JSR_W:
  143. return opstring + " " + (iter.s32bitAt(pos + 1)+ pos);
  144. default:
  145. return opstring;
  146. }
  147. }
  148. private static String wide(CodeIterator iter, int pos) {
  149. int opcode = iter.byteAt(pos + 1);
  150. int index = iter.u16bitAt(pos + 2);
  151. switch (opcode) {
  152. case ILOAD:
  153. case LLOAD:
  154. case FLOAD:
  155. case DLOAD:
  156. case ALOAD:
  157. case ISTORE:
  158. case LSTORE:
  159. case FSTORE:
  160. case DSTORE:
  161. case ASTORE:
  162. case IINC:
  163. case RET:
  164. return opcodes[opcode] + " " + index;
  165. default:
  166. throw new RuntimeException("Invalid WIDE operand");
  167. }
  168. }
  169. private static String arrayInfo(int type) {
  170. switch (type) {
  171. case T_BOOLEAN:
  172. return "boolean";
  173. case T_CHAR:
  174. return "char";
  175. case T_BYTE:
  176. return "byte";
  177. case T_SHORT:
  178. return "short";
  179. case T_INT:
  180. return "int";
  181. case T_LONG:
  182. return "long";
  183. case T_FLOAT:
  184. return "float";
  185. case T_DOUBLE:
  186. return "double";
  187. default:
  188. throw new RuntimeException("Invalid array type");
  189. }
  190. }
  191. private static String classInfo(ConstPool pool, int index) {
  192. return "#" + index + " = Class " + pool.getClassInfo(index);
  193. }
  194. private static String interfaceMethodInfo(ConstPool pool, int index) {
  195. return "#" + index + " = Method "
  196. + pool.getInterfaceMethodrefClassName(index) + "."
  197. + pool.getInterfaceMethodrefName(index) + "("
  198. + pool.getInterfaceMethodrefType(index) + ")";
  199. }
  200. private static String methodInfo(ConstPool pool, int index) {
  201. return "#" + index + " = Method "
  202. + pool.getMethodrefClassName(index) + "."
  203. + pool.getMethodrefName(index) + "("
  204. + pool.getMethodrefType(index) + ")";
  205. }
  206. private static String fieldInfo(ConstPool pool, int index) {
  207. return "#" + index + " = Field "
  208. + pool.getFieldrefClassName(index) + "."
  209. + pool.getFieldrefName(index) + "("
  210. + pool.getFieldrefType(index) + ")";
  211. }
  212. private static String lookupSwitch(CodeIterator iter, int pos) {
  213. StringBuffer buffer = new StringBuffer("lookupswitch {\n");
  214. int index = (pos & ~3) + 4;
  215. // default
  216. buffer.append("\t\tdefault: ").append(pos + iter.s32bitAt(index)).append("\n");
  217. int npairs = iter.s32bitAt(index += 4);
  218. int end = npairs * 8 + (index += 4);
  219. for (; index < end; index += 8) {
  220. int match = iter.s32bitAt(index);
  221. int target = iter.s32bitAt(index + 4) + pos;
  222. buffer.append("\t\t").append(match).append(": ").append(target).append("\n");
  223. }
  224. buffer.setCharAt(buffer.length() - 1, '}');
  225. return buffer.toString();
  226. }
  227. private static String tableSwitch(CodeIterator iter, int pos) {
  228. StringBuffer buffer = new StringBuffer("tableswitch {\n");
  229. int index = (pos & ~3) + 4;
  230. // default
  231. buffer.append("\t\tdefault: ").append(pos + iter.s32bitAt(index)).append("\n");
  232. int low = iter.s32bitAt(index += 4);
  233. int high = iter.s32bitAt(index += 4);
  234. int end = (high - low + 1) * 4 + (index += 4);
  235. // Offset table
  236. for (int key = low; index < end; index += 4, key++) {
  237. int target = iter.s32bitAt(index) + pos;
  238. buffer.append("\t\t").append(key).append(": ").append(target).append("\n");
  239. }
  240. buffer.setCharAt(buffer.length() - 1, '}');
  241. return buffer.toString();
  242. }
  243. private static String ldc(ConstPool pool, int index) {
  244. int tag = pool.getTag(index);
  245. switch (tag) {
  246. case ConstPool.CONST_String:
  247. return "#" + index + " = \"" + pool.getStringInfo(index) + "\"";
  248. case ConstPool.CONST_Integer:
  249. return "#" + index + " = int " + pool.getIntegerInfo(index);
  250. case ConstPool.CONST_Float:
  251. return "#" + index + " = float " + pool.getFloatInfo(index);
  252. case ConstPool.CONST_Long:
  253. return "#" + index + " = long " + pool.getLongInfo(index);
  254. case ConstPool.CONST_Double:
  255. return "#" + index + " = double " + pool.getDoubleInfo(index);
  256. case ConstPool.CONST_Class:
  257. return classInfo(pool, index);
  258. default:
  259. throw new RuntimeException("bad LDC: " + tag);
  260. }
  261. }
  262. }