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.html 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. <!DOCTYPE HTML>
  2. <!-- NewPage -->
  3. <html lang="en">
  4. <head>
  5. <!-- Generated by javadoc (11) on Sat May 08 11:16:00 JST 2021 -->
  6. <title>CodeConverter (Javassist API)</title>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. <meta name="dc.created" content="2021-05-08">
  9. <link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">
  10. <link rel="stylesheet" type="text/css" href="../jquery/jquery-ui.css" title="Style">
  11. <script type="text/javascript" src="../script.js"></script>
  12. <script type="text/javascript" src="../jquery/jszip/dist/jszip.min.js"></script>
  13. <script type="text/javascript" src="../jquery/jszip-utils/dist/jszip-utils.min.js"></script>
  14. <!--[if IE]>
  15. <script type="text/javascript" src="../jquery/jszip-utils/dist/jszip-utils-ie.min.js"></script>
  16. <![endif]-->
  17. <script type="text/javascript" src="../jquery/jquery-3.3.1.js"></script>
  18. <script type="text/javascript" src="../jquery/jquery-migrate-3.0.1.js"></script>
  19. <script type="text/javascript" src="../jquery/jquery-ui.js"></script>
  20. </head>
  21. <body>
  22. <script type="text/javascript"><!--
  23. try {
  24. if (location.href.indexOf('is-external=true') == -1) {
  25. parent.document.title="CodeConverter (Javassist API)";
  26. }
  27. }
  28. catch(err) {
  29. }
  30. //-->
  31. var data = {"i0":10,"i1":10,"i2":10,"i3":10,"i4":10,"i5":10,"i6":10,"i7":10,"i8":10,"i9":10,"i10":10};
  32. var tabs = {65535:["t0","All Methods"],2:["t2","Instance Methods"],8:["t4","Concrete Methods"]};
  33. var altColor = "altColor";
  34. var rowColor = "rowColor";
  35. var tableTab = "tableTab";
  36. var activeTableTab = "activeTableTab";
  37. var pathtoroot = "../";
  38. var useModuleDirectories = true;
  39. loadScripts(document, 'script');</script>
  40. <noscript>
  41. <div>JavaScript is disabled on your browser.</div>
  42. </noscript>
  43. <header role="banner">
  44. <nav role="navigation">
  45. <div class="fixedNav">
  46. <!-- ========= START OF TOP NAVBAR ======= -->
  47. <div class="topNav"><a id="navbar.top">
  48. <!-- -->
  49. </a>
  50. <div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
  51. <a id="navbar.top.firstrow">
  52. <!-- -->
  53. </a>
  54. <ul class="navList" title="Navigation">
  55. <li><a href="../index.html">Overview</a></li>
  56. <li><a href="package-summary.html">Package</a></li>
  57. <li class="navBarCell1Rev">Class</li>
  58. <li><a href="class-use/CodeConverter.html">Use</a></li>
  59. <li><a href="package-tree.html">Tree</a></li>
  60. <li><a href="../deprecated-list.html">Deprecated</a></li>
  61. <li><a href="../index-all.html">Index</a></li>
  62. </ul>
  63. </div>
  64. <div class="subNav">
  65. <ul class="navList" id="allclasses_navbar_top">
  66. <li><a href="../allclasses.html">All&nbsp;Classes</a></li>
  67. </ul>
  68. <ul class="navListSearch">
  69. <li><label for="search">SEARCH:</label>
  70. <input type="text" id="search" value="search" disabled="disabled">
  71. <input type="reset" id="reset" value="reset" disabled="disabled">
  72. </li>
  73. </ul>
  74. <div>
  75. <script type="text/javascript"><!--
  76. allClassesLink = document.getElementById("allclasses_navbar_top");
  77. if(window==top) {
  78. allClassesLink.style.display = "block";
  79. }
  80. else {
  81. allClassesLink.style.display = "none";
  82. }
  83. //-->
  84. </script>
  85. <noscript>
  86. <div>JavaScript is disabled on your browser.</div>
  87. </noscript>
  88. </div>
  89. <div>
  90. <ul class="subNavList">
  91. <li>Summary:&nbsp;</li>
  92. <li><a href="#nested.class.summary">Nested</a>&nbsp;|&nbsp;</li>
  93. <li>Field&nbsp;|&nbsp;</li>
  94. <li><a href="#constructor.summary">Constr</a>&nbsp;|&nbsp;</li>
  95. <li><a href="#method.summary">Method</a></li>
  96. </ul>
  97. <ul class="subNavList">
  98. <li>Detail:&nbsp;</li>
  99. <li>Field&nbsp;|&nbsp;</li>
  100. <li><a href="#constructor.detail">Constr</a>&nbsp;|&nbsp;</li>
  101. <li><a href="#method.detail">Method</a></li>
  102. </ul>
  103. </div>
  104. <a id="skip.navbar.top">
  105. <!-- -->
  106. </a></div>
  107. <!-- ========= END OF TOP NAVBAR ========= -->
  108. </div>
  109. <div class="navPadding">&nbsp;</div>
  110. <script type="text/javascript"><!--
  111. $('.navPadding').css('padding-top', $('.fixedNav').css("height"));
  112. //-->
  113. </script>
  114. </nav>
  115. </header>
  116. <!-- ======== START OF CLASS DATA ======== -->
  117. <main role="main">
  118. <div class="header">
  119. <div class="subTitle"><span class="packageLabelInType">Package</span>&nbsp;<a href="package-summary.html">javassist</a></div>
  120. <h2 title="Class CodeConverter" class="title">Class CodeConverter</h2>
  121. </div>
  122. <div class="contentContainer">
  123. <ul class="inheritance">
  124. <li>java.lang.Object</li>
  125. <li>
  126. <ul class="inheritance">
  127. <li>javassist.CodeConverter</li>
  128. </ul>
  129. </li>
  130. </ul>
  131. <div class="description">
  132. <ul class="blockList">
  133. <li class="blockList">
  134. <hr>
  135. <pre>public class <span class="typeNameLabel">CodeConverter</span>
  136. extends java.lang.Object</pre>
  137. <div class="block">Simple translator of method bodies
  138. (also see the <code>javassist.expr</code> package).
  139. <p>Instances of this class specifies how to instrument of the
  140. bytecodes representing a method body. They are passed to
  141. <code>CtClass.instrument()</code> or
  142. <code>CtMethod.instrument()</code> as a parameter.
  143. <p>Example:
  144. <pre>
  145. ClassPool cp = ClassPool.getDefault();
  146. CtClass point = cp.get("Point");
  147. CtClass singleton = cp.get("Singleton");
  148. CtClass client = cp.get("Client");
  149. CodeConverter conv = new CodeConverter();
  150. conv.replaceNew(point, singleton, "makePoint");
  151. client.instrument(conv);
  152. </pre>
  153. <p>This program substitutes "<code>Singleton.makePoint()</code>"
  154. for all occurrences of "<code>new Point()</code>"
  155. appearing in methods declared in a <code>Client</code> class.</div>
  156. <dl>
  157. <dt><span class="seeLabel">See Also:</span></dt>
  158. <dd><a href="CtClass.html#instrument(javassist.CodeConverter)"><code>CtClass.instrument(CodeConverter)</code></a>,
  159. <a href="CtBehavior.html#instrument(javassist.CodeConverter)"><code>CtBehavior.instrument(CodeConverter)</code></a>,
  160. <a href="expr/ExprEditor.html" title="class in javassist.expr"><code>ExprEditor</code></a></dd>
  161. </dl>
  162. </li>
  163. </ul>
  164. </div>
  165. <div class="summary">
  166. <ul class="blockList">
  167. <li class="blockList">
  168. <!-- ======== NESTED CLASS SUMMARY ======== -->
  169. <section role="region">
  170. <ul class="blockList">
  171. <li class="blockList"><a id="nested.class.summary">
  172. <!-- -->
  173. </a>
  174. <h3>Nested Class Summary</h3>
  175. <table class="memberSummary">
  176. <caption><span>Nested Classes</span><span class="tabEnd">&nbsp;</span></caption>
  177. <tr>
  178. <th class="colFirst" scope="col">Modifier and Type</th>
  179. <th class="colSecond" scope="col">Class</th>
  180. <th class="colLast" scope="col">Description</th>
  181. </tr>
  182. <tr class="altColor">
  183. <td class="colFirst"><code>static interface&nbsp;</code></td>
  184. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="CodeConverter.ArrayAccessReplacementMethodNames.html" title="interface in javassist">CodeConverter.ArrayAccessReplacementMethodNames</a></span></code></th>
  185. <td class="colLast">
  186. <div class="block">Interface containing the method names to be used
  187. as array access replacements.</div>
  188. </td>
  189. </tr>
  190. <tr class="rowColor">
  191. <td class="colFirst"><code>static class&nbsp;</code></td>
  192. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="CodeConverter.DefaultArrayAccessReplacementMethodNames.html" title="class in javassist">CodeConverter.DefaultArrayAccessReplacementMethodNames</a></span></code></th>
  193. <td class="colLast">
  194. <div class="block">Default implementation of the <code>ArrayAccessReplacementMethodNames</code>
  195. interface giving default values for method names to be used for replacing
  196. accesses to array elements.</div>
  197. </td>
  198. </tr>
  199. </table>
  200. </li>
  201. </ul>
  202. </section>
  203. <!-- ======== CONSTRUCTOR SUMMARY ======== -->
  204. <section role="region">
  205. <ul class="blockList">
  206. <li class="blockList"><a id="constructor.summary">
  207. <!-- -->
  208. </a>
  209. <h3>Constructor Summary</h3>
  210. <table class="memberSummary">
  211. <caption><span>Constructors</span><span class="tabEnd">&nbsp;</span></caption>
  212. <tr>
  213. <th class="colFirst" scope="col">Constructor</th>
  214. <th class="colLast" scope="col">Description</th>
  215. </tr>
  216. <tr class="altColor">
  217. <th class="colConstructorName" scope="row"><code><span class="memberNameLink"><a href="#%3Cinit%3E()">CodeConverter</a></span>()</code></th>
  218. <td class="colLast">&nbsp;</td>
  219. </tr>
  220. </table>
  221. </li>
  222. </ul>
  223. </section>
  224. <!-- ========== METHOD SUMMARY =========== -->
  225. <section role="region">
  226. <ul class="blockList">
  227. <li class="blockList"><a id="method.summary">
  228. <!-- -->
  229. </a>
  230. <h3>Method Summary</h3>
  231. <table class="memberSummary">
  232. <caption><span id="t0" class="activeTableTab"><span>All Methods</span><span class="tabEnd">&nbsp;</span></span><span id="t2" class="tableTab"><span><a href="javascript:show(2);">Instance Methods</a></span><span class="tabEnd">&nbsp;</span></span><span id="t4" class="tableTab"><span><a href="javascript:show(8);">Concrete Methods</a></span><span class="tabEnd">&nbsp;</span></span></caption>
  233. <tr>
  234. <th class="colFirst" scope="col">Modifier and Type</th>
  235. <th class="colSecond" scope="col">Method</th>
  236. <th class="colLast" scope="col">Description</th>
  237. </tr>
  238. <tr id="i0" class="altColor">
  239. <td class="colFirst"><code>void</code></td>
  240. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#insertAfterMethod(javassist.CtMethod,javassist.CtMethod)">insertAfterMethod</a></span>&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  241. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;afterMethod)</code></th>
  242. <td class="colLast">
  243. <div class="block">Inserts a call to another method after an existing method call.</div>
  244. </td>
  245. </tr>
  246. <tr id="i1" class="rowColor">
  247. <td class="colFirst"><code>void</code></td>
  248. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#insertBeforeMethod(javassist.CtMethod,javassist.CtMethod)">insertBeforeMethod</a></span>&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  249. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;beforeMethod)</code></th>
  250. <td class="colLast">
  251. <div class="block">Insert a call to another method before an existing method call.</div>
  252. </td>
  253. </tr>
  254. <tr id="i2" class="altColor">
  255. <td class="colFirst"><code>void</code></td>
  256. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#redirectFieldAccess(javassist.CtField,javassist.CtClass,java.lang.String)">redirectFieldAccess</a></span>&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  257. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass,
  258. java.lang.String&nbsp;newFieldname)</code></th>
  259. <td class="colLast">
  260. <div class="block">Modify a method body so that field read/write expressions access
  261. a different field from the original one.</div>
  262. </td>
  263. </tr>
  264. <tr id="i3" class="rowColor">
  265. <td class="colFirst"><code>void</code></td>
  266. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#redirectMethodCall(java.lang.String,javassist.CtMethod)">redirectMethodCall</a></span>&#8203;(java.lang.String&nbsp;oldMethodName,
  267. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;newMethod)</code></th>
  268. <td class="colLast">
  269. <div class="block">Correct invocations to a method that has been renamed.</div>
  270. </td>
  271. </tr>
  272. <tr id="i4" class="altColor">
  273. <td class="colFirst"><code>void</code></td>
  274. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#redirectMethodCall(javassist.CtMethod,javassist.CtMethod)">redirectMethodCall</a></span>&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  275. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;substMethod)</code></th>
  276. <td class="colLast">
  277. <div class="block">Modify method invocations in a method body so that a different
  278. method will be invoked.</div>
  279. </td>
  280. </tr>
  281. <tr id="i5" class="rowColor">
  282. <td class="colFirst"><code>void</code></td>
  283. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#redirectMethodCallToStatic(javassist.CtMethod,javassist.CtMethod)">redirectMethodCallToStatic</a></span>&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  284. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;staticMethod)</code></th>
  285. <td class="colLast">
  286. <div class="block">Redirect non-static method invocations in a method body to a static
  287. method.</div>
  288. </td>
  289. </tr>
  290. <tr id="i6" class="altColor">
  291. <td class="colFirst"><code>void</code></td>
  292. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#replaceArrayAccess(javassist.CtClass,javassist.CodeConverter.ArrayAccessReplacementMethodNames)">replaceArrayAccess</a></span>&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  293. <a href="CodeConverter.ArrayAccessReplacementMethodNames.html" title="interface in javassist">CodeConverter.ArrayAccessReplacementMethodNames</a>&nbsp;names)</code></th>
  294. <td class="colLast">
  295. <div class="block">Modify a method body, so that ALL accesses to an array are replaced with
  296. calls to static methods within another class.</div>
  297. </td>
  298. </tr>
  299. <tr id="i7" class="rowColor">
  300. <td class="colFirst"><code>void</code></td>
  301. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#replaceFieldRead(javassist.CtField,javassist.CtClass,java.lang.String)">replaceFieldRead</a></span>&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  302. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  303. java.lang.String&nbsp;calledMethod)</code></th>
  304. <td class="colLast">
  305. <div class="block">Modify a method body so that an expression reading the specified
  306. field is replaced with a call to the specified <i>static</i> method.</div>
  307. </td>
  308. </tr>
  309. <tr id="i8" class="altColor">
  310. <td class="colFirst"><code>void</code></td>
  311. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#replaceFieldWrite(javassist.CtField,javassist.CtClass,java.lang.String)">replaceFieldWrite</a></span>&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  312. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  313. java.lang.String&nbsp;calledMethod)</code></th>
  314. <td class="colLast">
  315. <div class="block">Modify a method body so that an expression writing the specified
  316. field is replaced with a call to the specified static method.</div>
  317. </td>
  318. </tr>
  319. <tr id="i9" class="rowColor">
  320. <td class="colFirst"><code>void</code></td>
  321. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#replaceNew(javassist.CtClass,javassist.CtClass)">replaceNew</a></span>&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;oldClass,
  322. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass)</code></th>
  323. <td class="colLast">
  324. <div class="block">Modify a method body so that instantiation of the class
  325. specified by <code>oldClass</code>
  326. is replaced with instantiation of another class <code>newClass</code>.</div>
  327. </td>
  328. </tr>
  329. <tr id="i10" class="altColor">
  330. <td class="colFirst"><code>void</code></td>
  331. <th class="colSecond" scope="row"><code><span class="memberNameLink"><a href="#replaceNew(javassist.CtClass,javassist.CtClass,java.lang.String)">replaceNew</a></span>&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass,
  332. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  333. java.lang.String&nbsp;calledMethod)</code></th>
  334. <td class="colLast">
  335. <div class="block">Modify a method body so that instantiation of the specified class
  336. is replaced with a call to the specified static method.</div>
  337. </td>
  338. </tr>
  339. </table>
  340. <ul class="blockList">
  341. <li class="blockList"><a id="methods.inherited.from.class.java.lang.Object">
  342. <!-- -->
  343. </a>
  344. <h3>Methods inherited from class&nbsp;java.lang.Object</h3>
  345. <code>equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait</code></li>
  346. </ul>
  347. </li>
  348. </ul>
  349. </section>
  350. </li>
  351. </ul>
  352. </div>
  353. <div class="details">
  354. <ul class="blockList">
  355. <li class="blockList">
  356. <!-- ========= CONSTRUCTOR DETAIL ======== -->
  357. <section role="region">
  358. <ul class="blockList">
  359. <li class="blockList"><a id="constructor.detail">
  360. <!-- -->
  361. </a>
  362. <h3>Constructor Detail</h3>
  363. <a id="&lt;init&gt;()">
  364. <!-- -->
  365. </a>
  366. <ul class="blockListLast">
  367. <li class="blockList">
  368. <h4>CodeConverter</h4>
  369. <pre>public&nbsp;CodeConverter()</pre>
  370. </li>
  371. </ul>
  372. </li>
  373. </ul>
  374. </section>
  375. <!-- ============ METHOD DETAIL ========== -->
  376. <section role="region">
  377. <ul class="blockList">
  378. <li class="blockList"><a id="method.detail">
  379. <!-- -->
  380. </a>
  381. <h3>Method Detail</h3>
  382. <a id="replaceNew(javassist.CtClass,javassist.CtClass,java.lang.String)">
  383. <!-- -->
  384. </a>
  385. <ul class="blockList">
  386. <li class="blockList">
  387. <h4>replaceNew</h4>
  388. <pre class="methodSignature">public&nbsp;void&nbsp;replaceNew&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass,
  389. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  390. java.lang.String&nbsp;calledMethod)</pre>
  391. <div class="block">Modify a method body so that instantiation of the specified class
  392. is replaced with a call to the specified static method. For example,
  393. <code>replaceNew(ctPoint, ctSingleton, "createPoint")</code>
  394. (where <code>ctPoint</code> and <code>ctSingleton</code> are
  395. compile-time classes for class <code>Point</code> and class
  396. <code>Singleton</code>, respectively)
  397. replaces all occurrences of:
  398. <pre>new Point(x, y)</pre>
  399. in the method body with:
  400. <pre>Singleton.createPoint(x, y)</pre>
  401. <p>This enables to intercept instantiation of <code>Point</code>
  402. and change the samentics. For example, the following
  403. <code>createPoint()</code> implements the singleton pattern:
  404. <pre>public static Point createPoint(int x, int y) {
  405. if (aPoint == null)
  406. aPoint = new Point(x, y);
  407. return aPoint;
  408. }
  409. </pre>
  410. <p>The static method call substituted for the original <code>new</code>
  411. expression must be
  412. able to receive the same set of parameters as the original
  413. constructor. If there are multiple constructors with different
  414. parameter types, then there must be multiple static methods
  415. with the same name but different parameter types.
  416. <p>The return type of the substituted static method must be
  417. the exactly same as the type of the instantiated class specified by
  418. <code>newClass</code>.</div>
  419. <dl>
  420. <dt><span class="paramLabel">Parameters:</span></dt>
  421. <dd><code>newClass</code> - the instantiated class.</dd>
  422. <dd><code>calledClass</code> - the class in which the static method is
  423. declared.</dd>
  424. <dd><code>calledMethod</code> - the name of the static method.</dd>
  425. </dl>
  426. </li>
  427. </ul>
  428. <a id="replaceNew(javassist.CtClass,javassist.CtClass)">
  429. <!-- -->
  430. </a>
  431. <ul class="blockList">
  432. <li class="blockList">
  433. <h4>replaceNew</h4>
  434. <pre class="methodSignature">public&nbsp;void&nbsp;replaceNew&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;oldClass,
  435. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass)</pre>
  436. <div class="block">Modify a method body so that instantiation of the class
  437. specified by <code>oldClass</code>
  438. is replaced with instantiation of another class <code>newClass</code>.
  439. For example,
  440. <code>replaceNew(ctPoint, ctPoint2)</code>
  441. (where <code>ctPoint</code> and <code>ctPoint2</code> are
  442. compile-time classes for class <code>Point</code> and class
  443. <code>Point2</code>, respectively)
  444. replaces all occurrences of:
  445. <pre>new Point(x, y)</pre>
  446. in the method body with:
  447. <pre>new Point2(x, y)</pre>
  448. <p>Note that <code>Point2</code> must be type-compatible with <code>Point</code>.
  449. It must have the same set of methods, fields, and constructors as the
  450. replaced class.</div>
  451. </li>
  452. </ul>
  453. <a id="redirectFieldAccess(javassist.CtField,javassist.CtClass,java.lang.String)">
  454. <!-- -->
  455. </a>
  456. <ul class="blockList">
  457. <li class="blockList">
  458. <h4>redirectFieldAccess</h4>
  459. <pre class="methodSignature">public&nbsp;void&nbsp;redirectFieldAccess&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  460. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;newClass,
  461. java.lang.String&nbsp;newFieldname)</pre>
  462. <div class="block">Modify a method body so that field read/write expressions access
  463. a different field from the original one.
  464. <p>Note that this method changes only the filed name and the class
  465. declaring the field; the type of the target object does not change.
  466. Therefore, the substituted field must be declared in the same class
  467. or a superclass of the original class.
  468. <p>Also, <code>clazz</code> and <code>newClass</code> must specify
  469. the class directly declaring the field. They must not specify
  470. a subclass of that class.</div>
  471. <dl>
  472. <dt><span class="paramLabel">Parameters:</span></dt>
  473. <dd><code>field</code> - the originally accessed field.</dd>
  474. <dd><code>newClass</code> - the class declaring the substituted field.</dd>
  475. <dd><code>newFieldname</code> - the name of the substituted field.</dd>
  476. </dl>
  477. </li>
  478. </ul>
  479. <a id="replaceFieldRead(javassist.CtField,javassist.CtClass,java.lang.String)">
  480. <!-- -->
  481. </a>
  482. <ul class="blockList">
  483. <li class="blockList">
  484. <h4>replaceFieldRead</h4>
  485. <pre class="methodSignature">public&nbsp;void&nbsp;replaceFieldRead&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  486. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  487. java.lang.String&nbsp;calledMethod)</pre>
  488. <div class="block">Modify a method body so that an expression reading the specified
  489. field is replaced with a call to the specified <i>static</i> method.
  490. This static method receives the target object of the original
  491. read expression as a parameter. It must return a value of
  492. the same type as the field.
  493. <p>For example, the program below
  494. <pre>Point p = new Point();
  495. int newX = p.x + 3;</pre>
  496. <p>can be translated into:
  497. <pre>Point p = new Point();
  498. int newX = Accessor.readX(p) + 3;</pre>
  499. <p>where
  500. <pre>public class Accessor {
  501. public static int readX(Object target) { ... }
  502. }</pre>
  503. <p>The type of the parameter of <code>readX()</code> must
  504. be <code>java.lang.Object</code> independently of the actual
  505. type of <code>target</code>. The return type must be the same
  506. as the field type.</div>
  507. <dl>
  508. <dt><span class="paramLabel">Parameters:</span></dt>
  509. <dd><code>field</code> - the field.</dd>
  510. <dd><code>calledClass</code> - the class in which the static method is
  511. declared.</dd>
  512. <dd><code>calledMethod</code> - the name of the static method.</dd>
  513. </dl>
  514. </li>
  515. </ul>
  516. <a id="replaceFieldWrite(javassist.CtField,javassist.CtClass,java.lang.String)">
  517. <!-- -->
  518. </a>
  519. <ul class="blockList">
  520. <li class="blockList">
  521. <h4>replaceFieldWrite</h4>
  522. <pre class="methodSignature">public&nbsp;void&nbsp;replaceFieldWrite&#8203;(<a href="CtField.html" title="class in javassist">CtField</a>&nbsp;field,
  523. <a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  524. java.lang.String&nbsp;calledMethod)</pre>
  525. <div class="block">Modify a method body so that an expression writing the specified
  526. field is replaced with a call to the specified static method.
  527. This static method receives two parameters: the target object of
  528. the original
  529. write expression and the assigned value. The return type of the
  530. static method is <code>void</code>.
  531. <p>For example, the program below
  532. <pre>Point p = new Point();
  533. p.x = 3;</pre>
  534. <p>can be translated into:
  535. <pre>Point p = new Point();
  536. Accessor.writeX(3);</pre>
  537. <p>where
  538. <pre>public class Accessor {
  539. public static void writeX(Object target, int value) { ... }
  540. }</pre>
  541. <p>The type of the first parameter of <code>writeX()</code> must
  542. be <code>java.lang.Object</code> independently of the actual
  543. type of <code>target</code>. The type of the second parameter
  544. is the same as the field type.</div>
  545. <dl>
  546. <dt><span class="paramLabel">Parameters:</span></dt>
  547. <dd><code>field</code> - the field.</dd>
  548. <dd><code>calledClass</code> - the class in which the static method is
  549. declared.</dd>
  550. <dd><code>calledMethod</code> - the name of the static method.</dd>
  551. </dl>
  552. </li>
  553. </ul>
  554. <a id="replaceArrayAccess(javassist.CtClass,javassist.CodeConverter.ArrayAccessReplacementMethodNames)">
  555. <!-- -->
  556. </a>
  557. <ul class="blockList">
  558. <li class="blockList">
  559. <h4>replaceArrayAccess</h4>
  560. <pre class="methodSignature">public&nbsp;void&nbsp;replaceArrayAccess&#8203;(<a href="CtClass.html" title="class in javassist">CtClass</a>&nbsp;calledClass,
  561. <a href="CodeConverter.ArrayAccessReplacementMethodNames.html" title="interface in javassist">CodeConverter.ArrayAccessReplacementMethodNames</a>&nbsp;names)
  562. throws <a href="NotFoundException.html" title="class in javassist">NotFoundException</a></pre>
  563. <div class="block">Modify a method body, so that ALL accesses to an array are replaced with
  564. calls to static methods within another class. In the case of reading an
  565. element from the array, this is replaced with a call to a static method with
  566. the array and the index as arguments, the return value is the value read from
  567. the array. If writing to an array, this is replaced with a call to a static
  568. method with the array, index and new value as parameters, the return value of
  569. the static method is <code>void</code>.
  570. <p>The <code>calledClass</code> parameter is the class containing the static methods to be used
  571. for array replacement. The <code>names</code> parameter points to an implementation of
  572. <code>ArrayAccessReplacementMethodNames</code> which specifies the names of the method to be
  573. used for access for each type of array. For example reading from an <code>int[]</code> will
  574. require a different method than if writing to an <code>int[]</code>, and writing to a <code>long[]</code>
  575. will require a different method than if writing to a <code>byte[]</code>. If the implementation
  576. of <code>ArrayAccessReplacementMethodNames</code> does not contain the name for access for a
  577. type of array, that access is not replaced.
  578. <p>A default implementation of <code>ArrayAccessReplacementMethodNames</code> called
  579. <code>DefaultArrayAccessReplacementMethodNames</code> has been provided and is what is used in the
  580. following example. This also assumes that <code>'foo.ArrayAdvisor'</code> is the name of the
  581. <code>CtClass</code> passed in.
  582. <p>If we have the following class:
  583. <pre>class POJO{
  584. int[] ints = new int[]{1, 2, 3, 4, 5};
  585. long[] longs = new int[]{10, 20, 30};
  586. Object objects = new Object[]{true, false};
  587. Integer[] integers = new Integer[]{new Integer(10)};
  588. }
  589. </pre>
  590. and this is accessed as:
  591. <pre>POJO p = new POJO();
  592. //Write to int array
  593. p.ints[2] = 7;
  594. //Read from int array
  595. int i = p.ints[2];
  596. //Write to long array
  597. p.longs[2] = 1000L;
  598. //Read from long array
  599. long l = p.longs[2];
  600. //Write to Object array
  601. p.objects[2] = "Hello";
  602. //Read from Object array
  603. Object o = p.objects[2];
  604. //Write to Integer array
  605. Integer integer = new Integer(5);
  606. p.integers[0] = integer;
  607. //Read from Object array
  608. integer = p.integers[0];
  609. </pre>
  610. Following instrumentation we will have
  611. <pre>POJO p = new POJO();
  612. //Write to int array
  613. ArrayAdvisor.arrayWriteInt(p.ints, 2, 7);
  614. //Read from int array
  615. int i = ArrayAdvisor.arrayReadInt(p.ints, 2);
  616. //Write to long array
  617. ArrayAdvisor.arrayWriteLong(p.longs, 2, 1000L);
  618. //Read from long array
  619. long l = ArrayAdvisor.arrayReadLong(p.longs, 2);
  620. //Write to Object array
  621. ArrayAdvisor.arrayWriteObject(p.objects, 2, "Hello");
  622. //Read from Object array
  623. Object o = ArrayAdvisor.arrayReadObject(p.objects, 2);
  624. //Write to Integer array
  625. Integer integer = new Integer(5);
  626. ArrayAdvisor.arrayWriteObject(p.integers, 0, integer);
  627. //Read from Object array
  628. integer = ArrayAdvisor.arrayWriteObject(p.integers, 0);
  629. </pre></div>
  630. <dl>
  631. <dt><span class="paramLabel">Parameters:</span></dt>
  632. <dd><code>calledClass</code> - the class containing the static methods.</dd>
  633. <dd><code>names</code> - contains the names of the methods to replace
  634. the different kinds of array access with.</dd>
  635. <dt><span class="throwsLabel">Throws:</span></dt>
  636. <dd><code><a href="NotFoundException.html" title="class in javassist">NotFoundException</a></code></dd>
  637. <dt><span class="seeLabel">See Also:</span></dt>
  638. <dd><a href="CodeConverter.DefaultArrayAccessReplacementMethodNames.html" title="class in javassist"><code>CodeConverter.DefaultArrayAccessReplacementMethodNames</code></a></dd>
  639. </dl>
  640. </li>
  641. </ul>
  642. <a id="redirectMethodCall(javassist.CtMethod,javassist.CtMethod)">
  643. <!-- -->
  644. </a>
  645. <ul class="blockList">
  646. <li class="blockList">
  647. <h4>redirectMethodCall</h4>
  648. <pre class="methodSignature">public&nbsp;void&nbsp;redirectMethodCall&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  649. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;substMethod)
  650. throws <a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></pre>
  651. <div class="block">Modify method invocations in a method body so that a different
  652. method will be invoked.
  653. <p>Note that the target object, the parameters, or
  654. the type of invocation
  655. (static method call, interface call, or private method call)
  656. are not modified. Only the method name is changed. The substituted
  657. method must have the same signature that the original one has.
  658. If the original method is a static method, the substituted method
  659. must be static.</div>
  660. <dl>
  661. <dt><span class="paramLabel">Parameters:</span></dt>
  662. <dd><code>origMethod</code> - original method</dd>
  663. <dd><code>substMethod</code> - substituted method</dd>
  664. <dt><span class="throwsLabel">Throws:</span></dt>
  665. <dd><code><a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></code></dd>
  666. </dl>
  667. </li>
  668. </ul>
  669. <a id="redirectMethodCall(java.lang.String,javassist.CtMethod)">
  670. <!-- -->
  671. </a>
  672. <ul class="blockList">
  673. <li class="blockList">
  674. <h4>redirectMethodCall</h4>
  675. <pre class="methodSignature">public&nbsp;void&nbsp;redirectMethodCall&#8203;(java.lang.String&nbsp;oldMethodName,
  676. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;newMethod)
  677. throws <a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></pre>
  678. <div class="block">Correct invocations to a method that has been renamed.
  679. If a method is renamed, calls to that method must be also
  680. modified so that the method with the new name will be called.
  681. <p>The method must be declared in the same class before and
  682. after it is renamed.
  683. <p>Note that the target object, the parameters, or
  684. the type of invocation
  685. (static method call, interface call, or private method call)
  686. are not modified. Only the method name is changed.</div>
  687. <dl>
  688. <dt><span class="paramLabel">Parameters:</span></dt>
  689. <dd><code>oldMethodName</code> - the old name of the method.</dd>
  690. <dd><code>newMethod</code> - the method with the new name.</dd>
  691. <dt><span class="throwsLabel">Throws:</span></dt>
  692. <dd><code><a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></code></dd>
  693. <dt><span class="seeLabel">See Also:</span></dt>
  694. <dd><a href="CtMethod.html#setName(java.lang.String)"><code>CtMethod.setName(String)</code></a></dd>
  695. </dl>
  696. </li>
  697. </ul>
  698. <a id="redirectMethodCallToStatic(javassist.CtMethod,javassist.CtMethod)">
  699. <!-- -->
  700. </a>
  701. <ul class="blockList">
  702. <li class="blockList">
  703. <h4>redirectMethodCallToStatic</h4>
  704. <pre class="methodSignature">public&nbsp;void&nbsp;redirectMethodCallToStatic&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  705. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;staticMethod)</pre>
  706. <div class="block">Redirect non-static method invocations in a method body to a static
  707. method. The return type must be same with the originally invoked method.
  708. As parameters, the static method receives
  709. the target object and all the parameters to the originally invoked
  710. method. For example, if the originally invoked method is
  711. <code>move()</code>:
  712. <pre>class Point {
  713. Point move(int x, int y) { ... }
  714. }</pre>
  715. <p>Then the static method must be something like this:
  716. <pre>class Verbose {
  717. static Point print(Point target, int x, int y) { ... }
  718. }</pre>
  719. <p>The <code>CodeConverter</code> would translate bytecode
  720. equivalent to:
  721. <pre>Point p2 = p.move(x + y, 0);</pre>
  722. <p>into the bytecode equivalent to:
  723. <pre>Point p2 = Verbose.print(p, x + y, 0);</pre></div>
  724. <dl>
  725. <dt><span class="paramLabel">Parameters:</span></dt>
  726. <dd><code>origMethod</code> - original method</dd>
  727. <dd><code>staticMethod</code> - static method</dd>
  728. </dl>
  729. </li>
  730. </ul>
  731. <a id="insertBeforeMethod(javassist.CtMethod,javassist.CtMethod)">
  732. <!-- -->
  733. </a>
  734. <ul class="blockList">
  735. <li class="blockList">
  736. <h4>insertBeforeMethod</h4>
  737. <pre class="methodSignature">public&nbsp;void&nbsp;insertBeforeMethod&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  738. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;beforeMethod)
  739. throws <a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></pre>
  740. <div class="block">Insert a call to another method before an existing method call.
  741. That "before" method must be static. The return type must be
  742. <code>void</code>. As parameters, the before method receives
  743. the target object and all the parameters to the originally invoked
  744. method. For example, if the originally invoked method is
  745. <code>move()</code>:
  746. <pre>class Point {
  747. Point move(int x, int y) { ... }
  748. }</pre>
  749. <p>Then the before method must be something like this:
  750. <pre>class Verbose {
  751. static void print(Point target, int x, int y) { ... }
  752. }</pre>
  753. <p>The <code>CodeConverter</code> would translate bytecode
  754. equivalent to:
  755. <pre>Point p2 = p.move(x + y, 0);</pre>
  756. <p>into the bytecode equivalent to:
  757. <pre>int tmp1 = x + y;
  758. int tmp2 = 0;
  759. Verbose.print(p, tmp1, tmp2);
  760. Point p2 = p.move(tmp1, tmp2);</pre></div>
  761. <dl>
  762. <dt><span class="paramLabel">Parameters:</span></dt>
  763. <dd><code>origMethod</code> - the method originally invoked.</dd>
  764. <dd><code>beforeMethod</code> - the method invoked before
  765. <code>origMethod</code>.</dd>
  766. <dt><span class="throwsLabel">Throws:</span></dt>
  767. <dd><code><a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></code></dd>
  768. </dl>
  769. </li>
  770. </ul>
  771. <a id="insertAfterMethod(javassist.CtMethod,javassist.CtMethod)">
  772. <!-- -->
  773. </a>
  774. <ul class="blockListLast">
  775. <li class="blockList">
  776. <h4>insertAfterMethod</h4>
  777. <pre class="methodSignature">public&nbsp;void&nbsp;insertAfterMethod&#8203;(<a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;origMethod,
  778. <a href="CtMethod.html" title="class in javassist">CtMethod</a>&nbsp;afterMethod)
  779. throws <a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></pre>
  780. <div class="block">Inserts a call to another method after an existing method call.
  781. That "after" method must be static. The return type must be
  782. <code>void</code>. As parameters, the after method receives
  783. the target object and all the parameters to the originally invoked
  784. method. For example, if the originally invoked method is
  785. <code>move()</code>:
  786. <pre>class Point {
  787. Point move(int x, int y) { ... }
  788. }</pre>
  789. <p>Then the after method must be something like this:
  790. <pre>class Verbose {
  791. static void print(Point target, int x, int y) { ... }
  792. }</pre>
  793. <p>The <code>CodeConverter</code> would translate bytecode
  794. equivalent to:
  795. <pre>Point p2 = p.move(x + y, 0);</pre>
  796. <p>into the bytecode equivalent to:
  797. <pre>
  798. int tmp1 = x + y;
  799. int tmp2 = 0;
  800. Point p2 = p.move(tmp1, tmp2);
  801. Verbose.print(p, tmp1, tmp2);</pre></div>
  802. <dl>
  803. <dt><span class="paramLabel">Parameters:</span></dt>
  804. <dd><code>origMethod</code> - the method originally invoked.</dd>
  805. <dd><code>afterMethod</code> - the method invoked after
  806. <code>origMethod</code>.</dd>
  807. <dt><span class="throwsLabel">Throws:</span></dt>
  808. <dd><code><a href="CannotCompileException.html" title="class in javassist">CannotCompileException</a></code></dd>
  809. </dl>
  810. </li>
  811. </ul>
  812. </li>
  813. </ul>
  814. </section>
  815. </li>
  816. </ul>
  817. </div>
  818. </div>
  819. </main>
  820. <!-- ========= END OF CLASS DATA ========= -->
  821. <footer role="contentinfo">
  822. <nav role="navigation">
  823. <!-- ======= START OF BOTTOM NAVBAR ====== -->
  824. <div class="bottomNav"><a id="navbar.bottom">
  825. <!-- -->
  826. </a>
  827. <div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
  828. <a id="navbar.bottom.firstrow">
  829. <!-- -->
  830. </a>
  831. <ul class="navList" title="Navigation">
  832. <li><a href="../index.html">Overview</a></li>
  833. <li><a href="package-summary.html">Package</a></li>
  834. <li class="navBarCell1Rev">Class</li>
  835. <li><a href="class-use/CodeConverter.html">Use</a></li>
  836. <li><a href="package-tree.html">Tree</a></li>
  837. <li><a href="../deprecated-list.html">Deprecated</a></li>
  838. <li><a href="../index-all.html">Index</a></li>
  839. </ul>
  840. </div>
  841. <div class="subNav">
  842. <ul class="navList" id="allclasses_navbar_bottom">
  843. <li><a href="../allclasses.html">All&nbsp;Classes</a></li>
  844. </ul>
  845. <div>
  846. <script type="text/javascript"><!--
  847. allClassesLink = document.getElementById("allclasses_navbar_bottom");
  848. if(window==top) {
  849. allClassesLink.style.display = "block";
  850. }
  851. else {
  852. allClassesLink.style.display = "none";
  853. }
  854. //-->
  855. </script>
  856. <noscript>
  857. <div>JavaScript is disabled on your browser.</div>
  858. </noscript>
  859. </div>
  860. <div>
  861. <ul class="subNavList">
  862. <li>Summary:&nbsp;</li>
  863. <li><a href="#nested.class.summary">Nested</a>&nbsp;|&nbsp;</li>
  864. <li>Field&nbsp;|&nbsp;</li>
  865. <li><a href="#constructor.summary">Constr</a>&nbsp;|&nbsp;</li>
  866. <li><a href="#method.summary">Method</a></li>
  867. </ul>
  868. <ul class="subNavList">
  869. <li>Detail:&nbsp;</li>
  870. <li>Field&nbsp;|&nbsp;</li>
  871. <li><a href="#constructor.detail">Constr</a>&nbsp;|&nbsp;</li>
  872. <li><a href="#method.detail">Method</a></li>
  873. </ul>
  874. </div>
  875. <a id="skip.navbar.bottom">
  876. <!-- -->
  877. </a></div>
  878. <!-- ======== END OF BOTTOM NAVBAR ======= -->
  879. </nav>
  880. <p class="legalCopy"><small><i>Javassist, a Java-bytecode translator toolkit.<br>
  881. Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.</i></small></p>
  882. </footer>
  883. </body>
  884. </html>