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.

TypedBlock.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.stackmap;
  17. import javassist.bytecode.*;
  18. public class TypedBlock extends BasicBlock {
  19. public int stackTop, numLocals;
  20. // localsTypes is set to non-null when this block is first visited by a MapMaker.
  21. // see alreadySet().
  22. public TypeData[] localsTypes;
  23. public TypeData[] stackTypes;
  24. /**
  25. * Divides the method body into basic blocks.
  26. * The type information of the first block is initialized.
  27. *
  28. * @param optmize if it is true and the method does not include
  29. * branches, this method returns null.
  30. */
  31. public static TypedBlock[] makeBlocks(MethodInfo minfo, CodeAttribute ca,
  32. boolean optimize)
  33. throws BadBytecode
  34. {
  35. TypedBlock[] blocks = (TypedBlock[])new Maker().make(minfo);
  36. if (optimize && blocks.length < 2)
  37. if (blocks.length == 0 || blocks[0].incoming == 0)
  38. return null;
  39. ConstPool pool = minfo.getConstPool();
  40. boolean isStatic = (minfo.getAccessFlags() & AccessFlag.STATIC) != 0;
  41. blocks[0].initFirstBlock(ca.getMaxStack(), ca.getMaxLocals(),
  42. pool.getClassName(), minfo.getDescriptor(),
  43. isStatic, minfo.isConstructor());
  44. return blocks;
  45. }
  46. protected TypedBlock(int pos) {
  47. super(pos);
  48. localsTypes = null;
  49. }
  50. protected void toString2(StringBuffer sbuf) {
  51. super.toString2(sbuf);
  52. sbuf.append(",\n stack={");
  53. printTypes(sbuf, stackTop, stackTypes);
  54. sbuf.append("}, locals={");
  55. printTypes(sbuf, numLocals, localsTypes);
  56. sbuf.append('}');
  57. }
  58. private void printTypes(StringBuffer sbuf, int size,
  59. TypeData[] types) {
  60. if (types == null)
  61. return;
  62. for (int i = 0; i < size; i++) {
  63. if (i > 0)
  64. sbuf.append(", ");
  65. TypeData td = types[i];
  66. sbuf.append(td == null ? "<>" : td.toString());
  67. }
  68. }
  69. public boolean alreadySet() {
  70. return localsTypes != null;
  71. }
  72. public void setStackMap(int st, TypeData[] stack, int nl, TypeData[] locals)
  73. throws BadBytecode
  74. {
  75. stackTop = st;
  76. stackTypes = stack;
  77. numLocals = nl;
  78. localsTypes = locals;
  79. }
  80. /*
  81. * Computes the correct value of numLocals.
  82. */
  83. public void resetNumLocals() {
  84. if (localsTypes != null) {
  85. int nl = localsTypes.length;
  86. while (nl > 0 && localsTypes[nl - 1].isBasicType() == TypeTag.TOP) {
  87. if (nl > 1) {
  88. if (localsTypes[nl - 2].is2WordType())
  89. break;
  90. }
  91. --nl;
  92. }
  93. numLocals = nl;
  94. }
  95. }
  96. public static class Maker extends BasicBlock.Maker {
  97. protected BasicBlock makeBlock(int pos) {
  98. return new TypedBlock(pos);
  99. }
  100. protected BasicBlock[] makeArray(int size) {
  101. return new TypedBlock[size];
  102. }
  103. }
  104. /**
  105. * Initializes the first block by the given method descriptor.
  106. *
  107. * @param block the first basic block that this method initializes.
  108. * @param className a dot-separated fully qualified class name.
  109. * For example, <code>javassist.bytecode.stackmap.BasicBlock</code>.
  110. * @param methodDesc method descriptor.
  111. * @param isStatic true if the method is a static method.
  112. * @param isConstructor true if the method is a constructor.
  113. */
  114. void initFirstBlock(int maxStack, int maxLocals, String className,
  115. String methodDesc, boolean isStatic, boolean isConstructor)
  116. throws BadBytecode
  117. {
  118. if (methodDesc.charAt(0) != '(')
  119. throw new BadBytecode("no method descriptor: " + methodDesc);
  120. stackTop = 0;
  121. stackTypes = TypeData.make(maxStack);
  122. TypeData[] locals = TypeData.make(maxLocals);
  123. if (isConstructor)
  124. locals[0] = new TypeData.UninitThis(className);
  125. else if (!isStatic)
  126. locals[0] = new TypeData.ClassName(className);
  127. int n = isStatic ? -1 : 0;
  128. int i = 1;
  129. try {
  130. while ((i = descToTag(methodDesc, i, ++n, locals)) > 0)
  131. if (locals[n].is2WordType())
  132. locals[++n] = TypeTag.TOP;
  133. }
  134. catch (StringIndexOutOfBoundsException e) {
  135. throw new BadBytecode("bad method descriptor: "
  136. + methodDesc);
  137. }
  138. numLocals = n;
  139. localsTypes = locals;
  140. }
  141. private static int descToTag(String desc, int i,
  142. int n, TypeData[] types)
  143. throws BadBytecode
  144. {
  145. int i0 = i;
  146. int arrayDim = 0;
  147. char c = desc.charAt(i);
  148. if (c == ')')
  149. return 0;
  150. while (c == '[') {
  151. ++arrayDim;
  152. c = desc.charAt(++i);
  153. }
  154. if (c == 'L') {
  155. int i2 = desc.indexOf(';', ++i);
  156. if (arrayDim > 0)
  157. types[n] = new TypeData.ClassName(desc.substring(i0, ++i2));
  158. else
  159. types[n] = new TypeData.ClassName(desc.substring(i0 + 1, ++i2 - 1)
  160. .replace('/', '.'));
  161. return i2;
  162. }
  163. else if (arrayDim > 0) {
  164. types[n] = new TypeData.ClassName(desc.substring(i0, ++i));
  165. return i;
  166. }
  167. else {
  168. TypeData t = toPrimitiveTag(c);
  169. if (t == null)
  170. throw new BadBytecode("bad method descriptor: " + desc);
  171. types[n] = t;
  172. return i + 1;
  173. }
  174. }
  175. private static TypeData toPrimitiveTag(char c) {
  176. switch (c) {
  177. case 'Z' :
  178. case 'C' :
  179. case 'B' :
  180. case 'S' :
  181. case 'I' :
  182. return TypeTag.INTEGER;
  183. case 'J' :
  184. return TypeTag.LONG;
  185. case 'F' :
  186. return TypeTag.FLOAT;
  187. case 'D' :
  188. return TypeTag.DOUBLE;
  189. case 'V' :
  190. default :
  191. return null;
  192. }
  193. }
  194. public static String getRetType(String desc) {
  195. int i = desc.indexOf(')');
  196. if (i < 0)
  197. return "java.lang.Object";
  198. char c = desc.charAt(i + 1);
  199. if (c == '[')
  200. return desc.substring(i + 1);
  201. else if (c == 'L')
  202. return desc.substring(i + 2, desc.length() - 1).replace('/', '.');
  203. else
  204. return "java.lang.Object";
  205. }
  206. }