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.

CodeConverter.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import javassist.bytecode.*;
  17. import javassist.convert.*;
  18. /**
  19. * Simple translator of method bodies
  20. * (also see the <code>javassist.expr</code> package).
  21. *
  22. * <p>Instances of this class specifies how to instrument of the
  23. * bytecodes representing a method body. They are passed to
  24. * <code>CtClass.instrument()</code> or
  25. * <code>CtMethod.instrument()</code> as a parameter.
  26. *
  27. * <p>Example:
  28. * <ul><pre>
  29. * ClassPool cp = ClassPool.getDefault();
  30. * CtClass point = cp.get("Point");
  31. * CtClass singleton = cp.get("Singleton");
  32. * CtClass client = cp.get("Client");
  33. * CodeConverter conv = new CodeConverter();
  34. * conv.replaceNew(point, singleton, "makePoint");
  35. * client.instrument(conv);
  36. * </pre></ul>
  37. *
  38. * <p>This program substitutes "<code>Singleton.makePoint()</code>"
  39. * for all occurrences of "<code>new Point()</code>"
  40. * appearing in methods declared in a <code>Client</code> class.
  41. *
  42. * @see javassist.CtClass#instrument(CodeConverter)
  43. * @see javassist.CtMethod#instrument(CodeConverter)
  44. * @see javassist.expr.ExprEditor
  45. */
  46. public class CodeConverter {
  47. Transformer transformers = null;
  48. /**
  49. * Modify a method body so that instantiation of the specified class
  50. * is replaced with a call to the specified static method. For example,
  51. * <code>replaceNew(ctPoint, ctSingleton, "createPoint")</code>
  52. * (where <code>ctPoint</code> and <code>ctSingleton</code> are
  53. * compile-time classes for class <code>Point</code> and class
  54. * <code>Singleton</code>, respectively)
  55. * replaces all occurrences of:
  56. *
  57. * <ul><code>new Point(x, y)</code></ul>
  58. *
  59. * in the method body with:
  60. *
  61. * <ul><code>Singleton.createPoint(x, y)</code></ul>
  62. *
  63. * <p>This enables to intercept instantiation of <code>Point</code>
  64. * and change the samentics. For example, the following
  65. * <code>createPoint()</code> implements the singleton pattern:
  66. *
  67. * <ul><pre>public static Point createPoint(int x, int y) {
  68. * if (aPoint == null)
  69. * aPoint = new Point(x, y);
  70. * return aPoint;
  71. * }
  72. * </pre></ul>
  73. *
  74. * <p>The static method call substituted for the original <code>new</code>
  75. * expression must be
  76. * able to receive the same set of parameters as the original
  77. * constructor. If there are multiple constructors with different
  78. * parameter types, then there must be multiple static methods
  79. * with the same name but different parameter types.
  80. *
  81. * <p>The return type of the substituted static method must be
  82. * the exactly same as the type of the instantiated class specified by
  83. * <code>newClass</code>.
  84. *
  85. * @param newClass the instantiated class.
  86. * @param calledClass the class in which the static method is
  87. * declared.
  88. * @param calledMethod the name of the static method.
  89. */
  90. public void replaceNew(CtClass newClass,
  91. CtClass calledClass, String calledMethod) {
  92. transformers = new TransformNew(transformers, newClass.getName(),
  93. calledClass.getName(), calledMethod);
  94. }
  95. /**
  96. * Modify a method body so that field read/write expressions access
  97. * a different field from the original one.
  98. *
  99. * <p>Note that this method changes only the filed name and the class
  100. * declaring the field; the type of the target object does not change.
  101. * Therefore, the substituted field must be declared in the same class
  102. * or a superclass of the original class.
  103. *
  104. * <p>Also, <code>clazz</code> and <code>newClass</code> must specify
  105. * the class directly declaring the field. They must not specify
  106. * a subclass of that class.
  107. *
  108. * @param field the originally accessed field.
  109. * @param newClass the class declaring the substituted field.
  110. * @param newFieldname the name of the substituted field.
  111. */
  112. public void redirectFieldAccess(CtField field,
  113. CtClass newClass, String newFieldname) {
  114. transformers = new TransformFieldAccess(transformers, field,
  115. newClass.getName(),
  116. newFieldname);
  117. }
  118. /**
  119. * Modify a method body so that an expression reading the specified
  120. * field is replaced with a call to the specified <i>static</i> method.
  121. * This static method receives the target object of the original
  122. * read expression as a parameter. It must return a value of
  123. * the same type as the field.
  124. *
  125. * <p>For example, the program below
  126. *
  127. * <ul><pre>Point p = new Point();
  128. * int newX = p.x + 3;</pre></ul>
  129. *
  130. * <p>can be translated into:
  131. *
  132. * <ul><pre>Point p = new Point();
  133. * int newX = Accessor.readX(p) + 3;</pre></ul>
  134. *
  135. * <p>where
  136. *
  137. * <ul><pre>public class Accessor {
  138. * public static int readX(Object target) { ... }
  139. * }</pre></ul>
  140. *
  141. * <p>The type of the parameter of <code>readX()</code> must
  142. * be <code>java.lang.Object</code> independently of the actual
  143. * type of <code>target</code>. The return type must be the same
  144. * as the field type.
  145. *
  146. * @param field the field.
  147. * @param calledClass the class in which the static method is
  148. * declared.
  149. * @param calledMethod the name of the static method.
  150. */
  151. public void replaceFieldRead(CtField field,
  152. CtClass calledClass, String calledMethod) {
  153. transformers = new TransformReadField(transformers, field,
  154. calledClass.getName(),
  155. calledMethod);
  156. }
  157. /**
  158. * Modify a method body so that an expression writing the specified
  159. * field is replaced with a call to the specified static method.
  160. * This static method receives two parameters: the target object of
  161. * the original
  162. * write expression and the assigned value. The return type of the
  163. * static method is <code>void</code>.
  164. *
  165. * <p>For example, the program below
  166. *
  167. * <ul><pre>Point p = new Point();
  168. * p.x = 3;</pre></ul>
  169. *
  170. * <p>can be translated into:
  171. *
  172. * <ul><pre>Point p = new Point();
  173. * Accessor.writeX(3);</pre></ul>
  174. *
  175. * <p>where
  176. *
  177. * <ul><pre>public class Accessor {
  178. * public static void writeX(Object target, int value) { ... }
  179. * }</pre></ul>
  180. *
  181. * <p>The type of the first parameter of <code>writeX()</code> must
  182. * be <code>java.lang.Object</code> independently of the actual
  183. * type of <code>target</code>. The type of the second parameter
  184. * is the same as the field type.
  185. *
  186. * @param field the field.
  187. * @param calledClass the class in which the static method is
  188. * declared.
  189. * @param calledMethod the name of the static method.
  190. */
  191. public void replaceFieldWrite(CtField field,
  192. CtClass calledClass, String calledMethod) {
  193. transformers = new TransformWriteField(transformers, field,
  194. calledClass.getName(),
  195. calledMethod);
  196. }
  197. /**
  198. * Modify method invocations in a method body so that a different
  199. * method is invoked.
  200. *
  201. * <p>Note that the target object, the parameters, or
  202. * the type of invocation
  203. * (static method call, interface call, or private method call)
  204. * are not modified. Only the method name is changed. The substituted
  205. * method must have the same signature that the original one has.
  206. * If the original method is a static method, the substituted method
  207. * must be static.
  208. *
  209. * @param origMethod original method
  210. * @param substMethod substituted method
  211. */
  212. public void redirectMethodCall(CtMethod origMethod,
  213. CtMethod substMethod)
  214. throws CannotCompileException
  215. {
  216. String d1 = origMethod.getMethodInfo2().getDescriptor();
  217. String d2 = substMethod.getMethodInfo2().getDescriptor();
  218. if (!d1.equals(d2))
  219. throw new CannotCompileException("signature mismatch");
  220. transformers = new TransformCall(transformers, origMethod,
  221. substMethod);
  222. }
  223. /**
  224. * Insert a call to another method before an existing method call.
  225. * That "before" method must be static. The return type must be
  226. * <code>void</code>. As parameters, the before method receives
  227. * the target object and all the parameters to the originally invoked
  228. * method. For example, if the originally invoked method is
  229. * <code>move()</code>:
  230. *
  231. * <ul><pre>class Point {
  232. * Point move(int x, int y) { ... }
  233. * }</pre></ul>
  234. *
  235. * <p>Then the before method must be something like this:
  236. *
  237. * <ul><pre>class Verbose {
  238. * static void print(Point target, int x, int y) { ... }
  239. * }</pre></ul>
  240. *
  241. * <p>The <code>CodeConverter</code> would translate bytecode
  242. * equivalent to:
  243. *
  244. * <ul><pre>Point p2 = p.move(x + y, 0);</pre></ul>
  245. *
  246. * <p>into the bytecode equivalent to:
  247. *
  248. * <ul><pre>int tmp1 = x + y;
  249. * int tmp2 = 0;
  250. * Verbose.print(p, tmp1, tmp2);
  251. * Point p2 = p.move(tmp1, tmp2);</pre></ul>
  252. *
  253. * @param origMethod the method originally invoked.
  254. * @param beforeMethod the method invoked before
  255. * <code>origMethod</code>.
  256. */
  257. public void insertBeforeMethod(CtMethod origMethod,
  258. CtMethod beforeMethod)
  259. throws CannotCompileException
  260. {
  261. try {
  262. transformers = new TransformBefore(transformers, origMethod,
  263. beforeMethod);
  264. }
  265. catch (NotFoundException e) {
  266. throw new CannotCompileException(e);
  267. }
  268. }
  269. /**
  270. * Inserts a call to another method after an existing method call.
  271. * That "after" method must be static. The return type must be
  272. * <code>void</code>. As parameters, the after method receives
  273. * the target object and all the parameters to the originally invoked
  274. * method. For example, if the originally invoked method is
  275. * <code>move()</code>:
  276. *
  277. * <ul><pre>class Point {
  278. * Point move(int x, int y) { ... }
  279. * }</pre></ul>
  280. *
  281. * <p>Then the after method must be something like this:
  282. *
  283. * <ul><pre>class Verbose {
  284. * static void print(Point target, int x, int y) { ... }
  285. * }</pre></ul>
  286. *
  287. * <p>The <code>CodeConverter</code> would translate bytecode
  288. * equivalent to:
  289. *
  290. * <ul><pre>Point p2 = p.move(x + y, 0);</pre></ul>
  291. *
  292. * <p>into the bytecode equivalent to:
  293. *
  294. * <ul><pre>int tmp1 = x + y;
  295. * int tmp2 = 0;
  296. * Point p2 = p.move(tmp1, tmp2);
  297. * Verbose.print(p, tmp1, tmp2);</pre></ul>
  298. *
  299. * @param origMethod the method originally invoked.
  300. * @param afterMethod the method invoked after
  301. * <code>origMethod</code>.
  302. */
  303. public void insertAfterMethod(CtMethod origMethod,
  304. CtMethod afterMethod)
  305. throws CannotCompileException
  306. {
  307. try {
  308. transformers = new TransformAfter(transformers, origMethod,
  309. afterMethod);
  310. }
  311. catch (NotFoundException e) {
  312. throw new CannotCompileException(e);
  313. }
  314. }
  315. /**
  316. * Performs code conversion.
  317. */
  318. void doit(CtClass clazz, MethodInfo minfo, ConstPool cp)
  319. throws CannotCompileException
  320. {
  321. Transformer t;
  322. CodeAttribute codeAttr = minfo.getCodeAttribute();
  323. if (codeAttr == null || transformers == null)
  324. return;
  325. for (t = transformers; t != null; t = t.getNext())
  326. t.initialize(cp, codeAttr);
  327. CodeIterator iterator = codeAttr.iterator();
  328. while (iterator.hasNext()) {
  329. try {
  330. int pos = iterator.next();
  331. for (t = transformers; t != null; t = t.getNext())
  332. pos = t.transform(clazz, pos, iterator, cp);
  333. }
  334. catch (BadBytecode e) {
  335. throw new CannotCompileException(e);
  336. }
  337. }
  338. int locals = 0;
  339. for (t = transformers; t != null; t = t.getNext()) {
  340. int s = t.extraLocals();
  341. if (s > locals)
  342. locals = s;
  343. }
  344. for (t = transformers; t != null; t = t.getNext())
  345. t.clean();
  346. codeAttr.setMaxLocals(codeAttr.getMaxLocals() + locals);
  347. }
  348. }