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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 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. public InstructionPrinter(PrintStream stream) {
  28. this.stream = stream;
  29. }
  30. public static void print(CtMethod method, PrintStream stream) {
  31. (new InstructionPrinter(stream)).print(method);
  32. }
  33. public void print(CtMethod method) {
  34. MethodInfo info = method.getMethodInfo2();
  35. ConstPool pool = info.getConstPool();
  36. CodeAttribute code = info.getCodeAttribute();
  37. if (code == null)
  38. return;
  39. CodeIterator iterator = code.iterator();
  40. while (iterator.hasNext()) {
  41. int pos;
  42. try {
  43. pos = iterator.next();
  44. } catch (BadBytecode e) {
  45. throw new RuntimeException(e);
  46. }
  47. stream.println(pos + ": " + instructionString(iterator, pos, pool));
  48. }
  49. }
  50. public static String instructionString(CodeIterator iter, int pos, ConstPool pool) {
  51. int opcode = iter.byteAt(pos);
  52. if (opcode > opcodes.length || opcode < 0)
  53. throw new IllegalArgumentException("Invalid opcode, opcode: " + opcode + " pos: "+ pos);
  54. String opstring = opcodes[opcode];
  55. switch (opcode) {
  56. case BIPUSH:
  57. return opstring + " " + iter.byteAt(pos + 1);
  58. case SIPUSH:
  59. return opstring + " " + iter.s16bitAt(pos + 1);
  60. case LDC:
  61. return opstring + " " + ldc(pool, iter.byteAt(pos + 1));
  62. case LDC_W :
  63. case LDC2_W :
  64. return opstring + " " + ldc(pool, iter.u16bitAt(pos + 1));
  65. case ILOAD:
  66. case LLOAD:
  67. case FLOAD:
  68. case DLOAD:
  69. case ALOAD:
  70. case ISTORE:
  71. case LSTORE:
  72. case FSTORE:
  73. case DSTORE:
  74. case ASTORE:
  75. return opstring + " " + iter.byteAt(pos + 1);
  76. case IFEQ:
  77. case IFGE:
  78. case IFGT:
  79. case IFLE:
  80. case IFLT:
  81. case IFNE:
  82. case IFNONNULL:
  83. case IFNULL:
  84. case IF_ACMPEQ:
  85. case IF_ACMPNE:
  86. case IF_ICMPEQ:
  87. case IF_ICMPGE:
  88. case IF_ICMPGT:
  89. case IF_ICMPLE:
  90. case IF_ICMPLT:
  91. case IF_ICMPNE:
  92. return opstring + " " + (iter.s16bitAt(pos + 1) + pos);
  93. case IINC:
  94. return opstring + " " + iter.byteAt(pos + 1);
  95. case GOTO:
  96. case JSR:
  97. return opstring + " " + (iter.s16bitAt(pos + 1) + pos);
  98. case RET:
  99. return opstring + " " + iter.byteAt(pos + 1);
  100. case TABLESWITCH:
  101. return tableSwitch(iter, pos);
  102. case LOOKUPSWITCH:
  103. return lookupSwitch(iter, pos);
  104. case GETSTATIC:
  105. case PUTSTATIC:
  106. case GETFIELD:
  107. case PUTFIELD:
  108. return opstring + " " + fieldInfo(pool, iter.u16bitAt(pos + 1));
  109. case INVOKEVIRTUAL:
  110. case INVOKESPECIAL:
  111. case INVOKESTATIC:
  112. return opstring + " " + methodInfo(pool, iter.u16bitAt(pos + 1));
  113. case INVOKEINTERFACE:
  114. return opstring + " " + interfaceMethodInfo(pool, iter.u16bitAt(pos + 1));
  115. case 186:
  116. throw new RuntimeException("Bad opcode 186");
  117. case NEW:
  118. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  119. case NEWARRAY:
  120. return opstring + " " + arrayInfo(iter.byteAt(pos + 1));
  121. case ANEWARRAY:
  122. case CHECKCAST:
  123. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  124. case WIDE:
  125. return wide(iter, pos);
  126. case MULTIANEWARRAY:
  127. return opstring + " " + classInfo(pool, iter.u16bitAt(pos + 1));
  128. case GOTO_W:
  129. case JSR_W:
  130. return opstring + " " + (iter.s32bitAt(pos + 1)+ pos);
  131. default:
  132. return opstring;
  133. }
  134. }
  135. private static String wide(CodeIterator iter, int pos) {
  136. int opcode = iter.byteAt(pos + 1);
  137. int index = iter.u16bitAt(pos + 2);
  138. switch (opcode) {
  139. case ILOAD:
  140. case LLOAD:
  141. case FLOAD:
  142. case DLOAD:
  143. case ALOAD:
  144. case ISTORE:
  145. case LSTORE:
  146. case FSTORE:
  147. case DSTORE:
  148. case ASTORE:
  149. case IINC:
  150. case RET:
  151. return opcodes[opcode] + " " + index;
  152. default:
  153. throw new RuntimeException("Invalid WIDE operand");
  154. }
  155. }
  156. private static String arrayInfo(int type) {
  157. switch (type) {
  158. case T_BOOLEAN:
  159. return "boolean";
  160. case T_CHAR:
  161. return "char";
  162. case T_BYTE:
  163. return "byte";
  164. case T_SHORT:
  165. return "short";
  166. case T_INT:
  167. return "int";
  168. case T_LONG:
  169. return "long";
  170. case T_FLOAT:
  171. return "float";
  172. case T_DOUBLE:
  173. return "double";
  174. default:
  175. throw new RuntimeException("Invalid array type");
  176. }
  177. }
  178. private static String classInfo(ConstPool pool, int index) {
  179. return "#" + index + " = Class " + pool.getClassInfo(index);
  180. }
  181. private static String interfaceMethodInfo(ConstPool pool, int index) {
  182. return "#" + index + " = Method "
  183. + pool.getInterfaceMethodrefClassName(index) + "."
  184. + pool.getInterfaceMethodrefName(index) + "("
  185. + pool.getInterfaceMethodrefType(index) + ")";
  186. }
  187. private static String methodInfo(ConstPool pool, int index) {
  188. return "#" + index + " = Method "
  189. + pool.getMethodrefClassName(index) + "."
  190. + pool.getMethodrefName(index) + "("
  191. + pool.getMethodrefType(index) + ")";
  192. }
  193. private static String fieldInfo(ConstPool pool, int index) {
  194. return "#" + index + " = Field "
  195. + pool.getFieldrefClassName(index) + "."
  196. + pool.getFieldrefName(index) + "("
  197. + pool.getFieldrefType(index) + ")";
  198. }
  199. private static String lookupSwitch(CodeIterator iter, int pos) {
  200. StringBuffer buffer = new StringBuffer("lookupswitch {\n");
  201. int index = (pos & ~3) + 4;
  202. // default
  203. buffer.append("\t\tdefault: ").append(pos + iter.s32bitAt(index)).append("\n");
  204. int npairs = iter.s32bitAt(index += 4);
  205. int end = npairs * 8 + (index += 4);
  206. for (; index < end; index += 8) {
  207. int match = iter.s32bitAt(index);
  208. int target = iter.s32bitAt(index + 4) + pos;
  209. buffer.append("\t\t").append(match).append(": ").append(target).append("\n");
  210. }
  211. buffer.setCharAt(buffer.length() - 1, '}');
  212. return buffer.toString();
  213. }
  214. private static String tableSwitch(CodeIterator iter, int pos) {
  215. StringBuffer buffer = new StringBuffer("tableswitch {\n");
  216. int index = (pos & ~3) + 4;
  217. // default
  218. buffer.append("\t\tdefault: ").append(pos + iter.s32bitAt(index)).append("\n");
  219. int low = iter.s32bitAt(index += 4);
  220. int high = iter.s32bitAt(index += 4);
  221. int end = (high - low + 1) * 4 + (index += 4);
  222. // Offset table
  223. for (int key = low; index < end; index += 4, key++) {
  224. int target = iter.s32bitAt(index) + pos;
  225. buffer.append("\t\t").append(key).append(": ").append(target).append("\n");
  226. }
  227. buffer.setCharAt(buffer.length() - 1, '}');
  228. return buffer.toString();
  229. }
  230. private static String ldc(ConstPool pool, int index) {
  231. int tag = pool.getTag(index);
  232. switch (tag) {
  233. case ConstPool.CONST_String:
  234. return "#" + index + " = \"" + pool.getStringInfo(index) + "\"";
  235. case ConstPool.CONST_Integer:
  236. return "#" + index + " = int " + pool.getIntegerInfo(index);
  237. case ConstPool.CONST_Float:
  238. return "#" + index + " = float " + pool.getFloatInfo(index);
  239. case ConstPool.CONST_Long:
  240. return "#" + index + " = long " + pool.getLongInfo(index);
  241. case ConstPool.CONST_Double:
  242. return "#" + index + " = int " + pool.getDoubleInfo(index);
  243. case ConstPool.CONST_Class:
  244. return classInfo(pool, index);
  245. default:
  246. throw new RuntimeException("bad LDC: " + tag);
  247. }
  248. }
  249. }