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.

JvstTest.java 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. package javassist;
  2. import junit.framework.*;
  3. import test1.DefineClassCapability;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStream;
  7. import java.lang.reflect.Method;
  8. import javassist.bytecode.*;
  9. import javassist.expr.*;
  10. import javassist.runtime.*;
  11. @SuppressWarnings({"rawtypes","unused", "resource"})
  12. public class JvstTest extends JvstTestRoot {
  13. public static boolean java9;
  14. static {
  15. //javassist.bytecode.MethodInfo.doPreverify = true;
  16. java9 = javassist.bytecode.ClassFile.MAJOR_VERSION
  17. >= javassist.bytecode.ClassFile.JAVA_9;
  18. }
  19. public JvstTest(String name) {
  20. super(name);
  21. }
  22. public void testConfig() {
  23. // is the value of PATH correct?
  24. assertTrue("not found " + PATH, new java.io.File(PATH).exists());
  25. }
  26. public void testLoader() throws Exception {
  27. Loader loader = new Loader(sloader);
  28. loader.delegateLoadingOf("test1.");
  29. assertEquals(loader.loadClass("test1.Cflow").getClassLoader(),
  30. loader.getParent());
  31. assertEquals(loader.loadClass("javassist.Loader").getClassLoader(),
  32. loader.getParent());
  33. assertEquals(loader.loadClass("javassist.CtClass").getClassLoader(),
  34. loader);
  35. }
  36. public void testDefreeze() throws Exception {
  37. CtClass cc = sloader.get("test1.Freeze");
  38. cc.stopPruning(true);
  39. cc.addInterface(sloader.get("java.io.Serializable"));
  40. assertTrue(!cc.isFrozen());
  41. cc.writeFile();
  42. assertTrue(cc.isFrozen());
  43. cc.defrost();
  44. assertTrue(!cc.isFrozen());
  45. }
  46. public void testClassPath() throws Exception {
  47. ClassPool pool = new ClassPool(null);
  48. ClassPath cp1 = pool.appendClassPath("d1");
  49. ClassPath cp2 = pool.appendClassPath("d2");
  50. ClassPath cp3 = pool.appendClassPath("d3");
  51. ClassPath cp4 = pool.appendClassPath("d4");
  52. print(pool.toString());
  53. pool.removeClassPath(cp3);
  54. print(pool.toString());
  55. pool.removeClassPath(cp4);
  56. print(pool.toString());
  57. pool.removeClassPath(cp2);
  58. print(pool.toString());
  59. pool.removeClassPath(cp1);
  60. assertTrue("[class path: ]".equals(pool.toString()));
  61. }
  62. public void testReleaseJarClassPathFileHandle() throws Exception {
  63. String jarFileName = "./empty.jar";
  64. ClassLoader classLoader = getClass().getClassLoader();
  65. File jarFile = new File(classLoader.getResource(jarFileName).getFile());
  66. assertTrue(jarFile.exists());
  67. // Prepare class pool and force it to open the Jar file
  68. ClassPool pool = ClassPool.getDefault();
  69. ClassPath cp = pool.appendClassPath(jarFile.getAbsolutePath());
  70. assertNull(cp.openClassfile("nothere.Dummy"));
  71. // Assert that it is possible to delete the jar file.
  72. // On Windows deleting an open file will fail, while on on Mac/Linux this is always possible.
  73. // This check will thus only fail on Windows if the file is still open.
  74. assertTrue(jarFile.delete());
  75. }
  76. public void testJarClassPath() throws Exception {
  77. String jarFileName = "./simple.jar";
  78. ClassLoader classLoader = getClass().getClassLoader();
  79. File jarFile = new File(classLoader.getResource(jarFileName).getFile());
  80. assertTrue(jarFile.exists());
  81. ClassPool pool = ClassPool.getDefault();
  82. ClassPath cp = pool.appendClassPath(jarFile.getAbsolutePath());
  83. InputStream is = cp.openClassfile("com.test.Test");
  84. assertNotNull(is);
  85. is.close();
  86. }
  87. public void testSubtype() throws Exception {
  88. CtClass cc = sloader.get("test1.Subtype");
  89. assertTrue(cc.subtypeOf(cc));
  90. assertTrue(cc.subtypeOf(sloader.get("test1.SubtypeA")));
  91. assertTrue(cc.subtypeOf(sloader.get("test1.SubtypeB")));
  92. assertTrue(cc.subtypeOf(sloader.get("test1.SubtypeC")));
  93. assertTrue(cc.subtypeOf(sloader.get("java.lang.Object")));
  94. assertTrue(!cc.subtypeOf(sloader.get("java.lang.String")));
  95. }
  96. public void testClassPoolGet() throws Exception {
  97. ClassPool pool = ClassPool.getDefault();
  98. CtClass cc = pool.makeClass("test1.Point");
  99. CtClass cc1 = pool.get("test1.Point"); // cc1 is identical to cc.
  100. cc.setName("test1.Pair");
  101. CtClass cc2 = pool.get("test1.Pair"); // cc2 is identical to cc.
  102. CtClass cc3 = pool.get("test1.Point"); // cc3 is not identical to cc.
  103. assertTrue(cc == cc1);
  104. assertTrue(cc == cc2);
  105. assertTrue(cc != cc3);
  106. assertEquals("test1.Pair", cc.getName());
  107. assertEquals("test1.Point", cc3.getName());
  108. }
  109. public static long testFieldInitHash;
  110. /* test CodeIterator.insertExGap().
  111. * The result of this test is checked again by JvstTest3#testFieldInitAgain().
  112. */
  113. public void testFieldInit() throws Exception {
  114. CtClass cc = sloader.get("test1.FieldInit");
  115. CtField f1 = new CtField(CtClass.intType, "f1", cc);
  116. cc.addField(f1, CtField.Initializer.byCall(cc, "get"));
  117. CtField f2 = CtField.make("public int f2 = 3;", cc);
  118. cc.addField(f2);
  119. CtField f3 = CtField.make("public int f3;", cc);
  120. cc.addField(f3);
  121. CtField f4 = CtField.make("public int f4 = this.f2 + 3;", cc);
  122. cc.addField(f4);
  123. CtField fi = CtField.make("public test1.FieldInit.FI fi = new test1.FieldInit.FI(this);", cc);
  124. cc.addField(fi);
  125. testFieldInitHash = f1.hashCode();
  126. cc.writeFile();
  127. Object obj = make(cc.getName());
  128. int value = obj.getClass().getField("counter").getInt(obj);
  129. assertEquals(1, value);
  130. int value2 = obj.getClass().getField("f2").getInt(obj);
  131. assertEquals(3, value2);
  132. int value3 = obj.getClass().getField("f3").getInt(obj);
  133. assertEquals(0, value3);
  134. int value4 = obj.getClass().getField("f4").getInt(obj);
  135. assertEquals(6, value4);
  136. Object obfi = obj.getClass().getField("fi").get(obj);
  137. assertTrue(obfi.getClass().getField("fi").get(obfi) == obj);
  138. }
  139. /* test CodeIterator.insertExGap().
  140. */
  141. public void testFieldInit2() throws Exception {
  142. CtClass cc = sloader.get("test1.FieldInit2");
  143. CtField f = new CtField(CtClass.intType, "f1", cc);
  144. cc.addField(f, CtField.Initializer.byCall(cc, "get"));
  145. cc.writeFile();
  146. try {
  147. Object obj = make(cc.getName());
  148. fail();
  149. }
  150. catch (Exception e) {
  151. print("testFieldInit2: catch");
  152. }
  153. }
  154. public static CtMethod testCalleeBeforeMethod;
  155. public static long testCalleeBeforeMethod2;
  156. /* The test result is checked again by JvstTest3#testCalleeBeforeAgain().
  157. */
  158. public void testCalleeBefore() throws Exception {
  159. CtClass cc = sloader.get("test1.CalleeBefore");
  160. CtMethod m1 = cc.getDeclaredMethod("m1");
  161. m1.insertBefore("{ int k = 1; p = k; }");
  162. CtMethod m2 = cc.getDeclaredMethod("m2");
  163. testCalleeBeforeMethod = m1;
  164. testCalleeBeforeMethod2 = m2.getMethodInfo2().hashCode();
  165. m2.insertBefore("{ int k = 3; q = k; }");
  166. CtConstructor[] cons = cc.getDeclaredConstructors();
  167. for (int i = 0; i < cons.length; ++i) {
  168. MethodInfo minfo = cons[i].getMethodInfo();
  169. CodeAttribute ca = minfo.getCodeAttribute();
  170. CodeIterator iterator = ca.iterator();
  171. if (cons[i].getParameterTypes().length == 0) {
  172. assertTrue(iterator.skipThisConstructor() >= 0);
  173. assertTrue(iterator.skipSuperConstructor() < 0);
  174. assertTrue(iterator.skipConstructor() >= 0);
  175. }
  176. else {
  177. assertTrue(iterator.skipThisConstructor() < 0);
  178. assertTrue(iterator.skipSuperConstructor() >= 0);
  179. assertTrue(iterator.skipConstructor() >= 0);
  180. }
  181. cons[i].insertBeforeBody("{ int k = 1; counter += k; }");
  182. }
  183. cc.writeFile();
  184. Object obj = make(cc.getName());
  185. assertEquals(0, invoke(obj, "getr"));
  186. assertEquals(17, invoke(obj, "test"));
  187. }
  188. public void testCalleeAfter() throws Exception {
  189. CtClass cc = sloader.get("test1.CalleeAfter");
  190. CtMethod m1 = cc.getDeclaredMethod("m1");
  191. m1.insertAfter("{ int k = 1; $_ = $_ + k; }", false);
  192. CtMethod m2 = cc.getDeclaredMethod("m2");
  193. m2.insertAfter("{ char k = 1; $_ = $_ + k; }", false);
  194. CtConstructor[] cons = cc.getDeclaredConstructors();
  195. cons[0].insertAfter("{ ++p; $_ = ($r)null; }", false);
  196. cc.writeFile();
  197. Object obj = make(cc.getName());
  198. assertEquals(15, invoke(obj, "test"));
  199. }
  200. public void testCalleeAfter2() throws Exception {
  201. CtClass cc = sloader.get("test1.CalleeAfter2");
  202. CtMethod m1 = cc.getDeclaredMethod("m1");
  203. m1.insertAfter("$_ = 7; $_ = ($r)k1(0);", false);
  204. CtMethod m2 = cc.getDeclaredMethod("m2");
  205. m2.insertAfter("$_ = ($r)k2(0);", false);
  206. CtMethod m3 = cc.getDeclaredMethod("m3");
  207. m3.insertAfter("$_ = ($r)k3(0);", false);
  208. CtMethod m4 = cc.getDeclaredMethod("m4");
  209. try {
  210. m4.insertAfter("$_ = ($r)1;", false);
  211. assertTrue(false);
  212. }
  213. catch (CannotCompileException e) {
  214. }
  215. CtMethod m5 = cc.getDeclaredMethod("m5");
  216. m5.insertAfter("$_ = ($r)k5(0);", false);
  217. cc.writeFile();
  218. Object obj = make(cc.getName());
  219. assertEquals(17, invoke(obj, "test"));
  220. }
  221. public void testCalleeAfter3() throws Exception {
  222. CtClass cc = sloader.get("test1.CalleeAfter3");
  223. CtMethod m1 = cc.getDeclaredMethod("m1");
  224. m1.insertAfter("value++;", true);
  225. CtMethod m2 = cc.getDeclaredMethod("m2");
  226. m2.insertAfter("value++;", true);
  227. CtMethod m3 = cc.getDeclaredMethod("m3");
  228. m3.insertAfter("value++;", true);
  229. CtMethod m4 = cc.getDeclaredMethod("m4");
  230. m4.insertAfter("value++;", true);
  231. cc.writeFile();
  232. Object obj = make(cc.getName());
  233. assertEquals(22, invoke(obj, "test"));
  234. }
  235. public void testCalleeCatch() throws Exception {
  236. CtClass cc = sloader.get("test1.CalleeCatch");
  237. CtMethod m1 = cc.getDeclaredMethod("m1");
  238. m1.addCatch("{ System.out.println($e); return p; }",
  239. sloader.get("java.lang.Exception"));
  240. cc.writeFile();
  241. Object obj = make(cc.getName());
  242. assertEquals(3, invoke(obj, "test"));
  243. }
  244. public void testSuperclass() throws Exception {
  245. CtClass cc = sloader.get("java.lang.Object");
  246. assertEquals(null, cc.getSuperclass());
  247. }
  248. public void testProceed() throws Exception {
  249. CtClass cc = sloader.get("test1.Proceed");
  250. CtMethod m1 = CtNewMethod.make(
  251. "public int m1() { return $proceed(3); }",
  252. cc, "this", "k1");
  253. CtMethod m2 = CtNewMethod.make(
  254. "public int m2() { return $proceed(3); }",
  255. cc, "another", "k2");
  256. CtMethod m3 = CtNewMethod.make(
  257. "public int q(int i) { return p($1 + 1, $$); }", cc);
  258. cc.addMethod(m1);
  259. cc.addMethod(m2);
  260. cc.addMethod(m3);
  261. CtMethod m4 = CtNewMethod.make(
  262. "public int q2() { return q(4); }", cc);
  263. cc.addMethod(m4);
  264. cc.writeFile();
  265. Object obj = make(cc.getName());
  266. assertEquals(3, invoke(obj, "m1"));
  267. assertEquals(4, invoke(obj, "m2"));
  268. assertEquals(9, invoke(obj, "q2"));
  269. }
  270. public void testProceed2() throws Exception {
  271. CtClass cc = sloader.get("test1.Proceed2");
  272. CtMethod m1 = cc.getDeclaredMethod("k1");
  273. m1.instrument(new ExprEditor() {
  274. public void edit(MethodCall m) throws CannotCompileException {
  275. m.replace("{ $_ = $proceed($$); }");
  276. }
  277. public void edit(NewExpr m) throws CannotCompileException {
  278. m.replace("{ $_ = $proceed($$); }");
  279. }
  280. public void edit(FieldAccess m) throws CannotCompileException {
  281. m.replace("{ $_ = $proceed($$); }");
  282. }
  283. public void edit(Instanceof i) throws CannotCompileException {
  284. i.replace("{ $_ = $proceed($$); }");
  285. }
  286. public void edit(Cast c) throws CannotCompileException {
  287. c.replace("{ $_ = $proceed($$); }");
  288. }
  289. });
  290. CtMethod m2 = cc.getDeclaredMethod("k2");
  291. m2.instrument(new ExprEditor() {
  292. public void edit(MethodCall m) throws CannotCompileException {
  293. m.replace("{ $proceed(); }");
  294. }
  295. public void edit(NewExpr m) throws CannotCompileException {
  296. m.replace("{ $_ = $proceed(); }");
  297. }
  298. public void edit(FieldAccess m) throws CannotCompileException {
  299. if (m.isReader())
  300. m.replace("{ $_ = $proceed(); }");
  301. else
  302. m.replace("{ $proceed($$); }");
  303. }
  304. });
  305. cc.writeFile();
  306. Object obj = make(cc.getName());
  307. assertEquals(2, invoke(obj, "k1"));
  308. }
  309. public void testProceed3() throws Exception {
  310. CtClass cc = sloader.get("test1.Proceed3");
  311. CtMethod m1 = cc.getDeclaredMethod("p");
  312. CtMethod m2 = CtNewMethod.copy(m1, cc, null);
  313. m1.setName(m1.getName() + "_orig");
  314. m2.setBody("{ return $proceed($1 + 1); }", "this", m1.getName());
  315. cc.addMethod(m2);
  316. cc.writeFile();
  317. Object obj = make(cc.getName());
  318. assertEquals(4, invoke(obj, "k1"));
  319. }
  320. public void testSetBody() throws Exception {
  321. CtClass cc = sloader.get("test1.SetBody");
  322. CtMethod m1 = cc.getDeclaredMethod("m1");
  323. m1.setBody("{ int i = $1 * $2; return i; }");
  324. CtMethod m2 = cc.getDeclaredMethod("m2");
  325. m2.setBody("System.out.println(\"setbody: \" + $1);");
  326. CtMethod m3 = cc.getDeclaredMethod("m3");
  327. try {
  328. m3.setBody("value = 1; System.out.println(\"setbody: \" + $1);");
  329. fail();
  330. }
  331. catch (CannotCompileException e) {
  332. // System.err.println(e);
  333. }
  334. CtConstructor cons
  335. = new CtConstructor(new CtClass[] { CtClass.intType }, cc);
  336. cons.setBody(null);
  337. cc.addConstructor(cons);
  338. cc.writeFile();
  339. Object obj = make(cc.getName());
  340. assertEquals(12, invoke(obj, "run"));
  341. }
  342. public void testSetStaticConsBody() throws Exception {
  343. CtClass cc = sloader.get("test1.StaticConsBody");
  344. CtConstructor cons = cc.getClassInitializer();
  345. cons.setBody(null);
  346. cons = cc.getConstructors()[0];
  347. cons.setBody(null);
  348. cc.writeFile();
  349. Object obj = make(cc.getName());
  350. assertEquals(0, invoke(obj, "run"));
  351. }
  352. public void testSetConsBody() throws Exception {
  353. CtClass superClazz = sloader.get("java.io.File");
  354. CtClass cc = sloader.makeClass("test1.SetConsBody");
  355. cc.setSuperclass(superClazz);
  356. CtConstructor constructor = new CtConstructor(new CtClass[0], cc);
  357. constructor.setBody("super(\"MyFile\");");
  358. cc.addConstructor(constructor);
  359. constructor = new CtConstructor(new CtClass[] { CtClass.intType },
  360. cc);
  361. constructor.setBody("{ super(\"MyFile\"); }");
  362. cc.addConstructor(constructor);
  363. cc.addMethod(CtNewMethod.make(CtClass.voidType, "m1",
  364. null, null, null, cc));
  365. cc.addMethod(CtNewMethod.make(CtClass.intType, "m2",
  366. null, null, null, cc));
  367. cc.addMethod(CtNewMethod.make(CtClass.byteType, "m3",
  368. null, null, null, cc));
  369. cc.addMethod(CtNewMethod.make(CtClass.longType, "m4",
  370. null, null, null, cc));
  371. cc.addMethod(CtNewMethod.make(CtClass.floatType, "m5",
  372. null, null, null, cc));
  373. cc.addMethod(CtNewMethod.make(CtClass.doubleType, "m6",
  374. null, null, null, cc));
  375. cc.addMethod(CtNewMethod.make(sloader.get("int[]"), "m7",
  376. null, null, null, cc));
  377. cc.addMethod(CtNewMethod.make(
  378. "public int run() {"
  379. + " return (int)(m2() + m3() + m4() + m5() + m6() + 3); }", cc));
  380. cc.writeFile();
  381. Object obj = make(cc.getName());
  382. assertEquals(3, invoke(obj, "run"));
  383. }
  384. public void testEmptyBody() throws Exception {
  385. String[] methods = { "m1", "m2", "m3", "m4" };
  386. boolean[] results = { true, false, false, false, true };
  387. boolean[] cResults = { true, true, false, false, false, true };
  388. CtClass cc = sloader.get("test1.EmptyBody");
  389. for (int i = 0; i < methods.length; ++i) {
  390. CtMethod m = cc.getDeclaredMethod(methods[i]);
  391. assertEquals(results[i], m.isEmpty());
  392. }
  393. CtConstructor[] cons = cc.getDeclaredConstructors();
  394. for (int j = 0; j < cons.length; ++j)
  395. assertEquals(cResults[j], cons[j].isEmpty());
  396. }
  397. public void testExprEditor() throws Exception {
  398. CtClass cc = sloader.get("test1.ExprEdit");
  399. CtMethod m1 = cc.getDeclaredMethod("k0");
  400. m1.instrument(new ExprEditor() {
  401. public void edit(MethodCall m) throws CannotCompileException {
  402. if (m.getClassName().equals("test1.ExprEdit")) {
  403. String name = m.getMethodName();
  404. if (name.equals("k1") || name.equals("k2")) {
  405. try {
  406. CtMethod cm = m.getMethod();
  407. print(cm.getParameterTypes()[0].getName());
  408. print(cm.getReturnType().getName());
  409. }
  410. catch (NotFoundException e) {
  411. throw new CannotCompileException(e);
  412. }
  413. m.replace("{ ++$1; $_ = $proceed($$); }");
  414. }
  415. else if (name.equals("k3"))
  416. m.replace("{ ++$1; $proceed($$); }");
  417. }
  418. }
  419. });
  420. cc.writeFile();
  421. Object obj = make(cc.getName());
  422. assertEquals(12, invoke(obj, "k0"));
  423. }
  424. public void testExprEditor2() throws Exception {
  425. CtClass cc = sloader.get("test1.ExprEdit2");
  426. CtMethod m1 = cc.getDeclaredMethod("k1");
  427. m1.instrument(new ExprEditor() {
  428. public void edit(FieldAccess m) throws CannotCompileException {
  429. if (m.getClassName().equals("test1.ExprEdit2")) {
  430. String name = m.getFieldName();
  431. try {
  432. CtField cf = m.getField();
  433. print(cf.getType().getName());
  434. print("file: " + m.getFileName());
  435. print("line: " + m.getLineNumber());
  436. }
  437. catch (NotFoundException e) {
  438. throw new CannotCompileException(e);
  439. }
  440. if (name.equals("df"))
  441. if (m.isReader())
  442. m.replace("{ $_ = $proceed() + 1; }");
  443. else
  444. m.replace("{ $proceed($1 + 1); }");
  445. else if (name.equals("sf"))
  446. if (m.isReader())
  447. m.replace("{ $_ = $proceed() + 2; }");
  448. else
  449. m.replace("{ $proceed($1 + 2); }");
  450. }
  451. }
  452. });
  453. cc.writeFile();
  454. Object obj = make(cc.getName());
  455. assertEquals(16, invoke(obj, "k1"));
  456. }
  457. public void testExprEditor3() throws Exception {
  458. CtClass cc = sloader.get("test1.ExprEdit3");
  459. CtMethod m1 = cc.getDeclaredMethod("k1");
  460. m1.instrument(new ExprEditor() {
  461. public void edit(NewExpr m) throws CannotCompileException {
  462. System.out.println("new " + m.getClassName());
  463. try {
  464. CtConstructor cc = m.getConstructor();
  465. print(cc.getParameterTypes()[0].getName());
  466. }
  467. catch (NotFoundException e) {
  468. throw new CannotCompileException(e);
  469. }
  470. if (m.getClassName().equals("test1.ExprEdit3")) {
  471. m.replace("{ ++$2; $_ = $proceed($$); }");
  472. }
  473. }
  474. });
  475. cc.writeFile();
  476. Object obj = make(cc.getName());
  477. assertEquals(4, invoke(obj, "k1"));
  478. }
  479. public void testExprEditor4() throws Exception {
  480. CtClass cc = sloader.get("test1.ExprEdit4");
  481. CtMethod m1 = cc.getDeclaredMethod("k1");
  482. m1.instrument(new ExprEditor() {
  483. public void edit(NewExpr m) throws CannotCompileException {
  484. System.out.println("new " + m.getClassName());
  485. if (m.getClassName().equals("test1.ExprEdit4"))
  486. m.replace("$_ = null;");
  487. }
  488. public void edit(MethodCall m) throws CannotCompileException {
  489. if (m.getClassName().equals("test1.ExprEdit4"))
  490. m.replace("{}");
  491. }
  492. });
  493. cc.writeFile();
  494. Object obj = make(cc.getName());
  495. assertEquals(1, invoke(obj, "k1"));
  496. }
  497. public void testExprEditor5() throws Exception {
  498. CtClass cc = sloader.get("test1.ExprEdit5");
  499. CtMethod m1 = cc.getDeclaredMethod("k1");
  500. m1.instrument(new ExprEditor() {
  501. public void edit(NewExpr m) throws CannotCompileException {
  502. m.replace("{ $_ = $proceed($$, \"test\"); }");
  503. }
  504. });
  505. cc.writeFile();
  506. Object obj = make(cc.getName());
  507. assertEquals(1, invoke(obj, "k1"));
  508. }
  509. public void testExprEditor6() throws Exception {
  510. CtClass cc = sloader.get("test1.ExprEdit6");
  511. cc.instrument(new ExprEditor() {
  512. public void edit(MethodCall m) throws CannotCompileException {
  513. assertTrue(m.where().getName().equals("k1"));
  514. m.replace("$_ = 3;");
  515. }
  516. });
  517. cc.writeFile();
  518. Object obj = make(cc.getName());
  519. assertEquals(3, invoke(obj, "k1"));
  520. }
  521. public void testExprEditor7() throws Exception {
  522. CtClass cc = sloader.get("test1.ExprEdit7");
  523. cc.instrument(new ExprEditor() {
  524. public void edit(Instanceof i) throws CannotCompileException {
  525. i.replace("{ this.c1 = $type; $_ = !$proceed($1); }");
  526. }
  527. public void edit(Cast c) throws CannotCompileException {
  528. c.replace("{ this.c2 = $type; $_ = ($r)$1; }");
  529. }
  530. });
  531. cc.writeFile();
  532. Object obj = make(cc.getName());
  533. assertEquals(7, invoke(obj, "k1"));
  534. }
  535. public void testExprEditor8() throws Exception {
  536. CtClass cc = sloader.get("test1.ExprEdit8");
  537. cc.instrument(new ExprEditor() {
  538. public void edit(ConstructorCall c) throws CannotCompileException {
  539. assertTrue(c.isSuper());
  540. c.replace("{ $_ = $proceed($$); value = 7; }");
  541. }
  542. });
  543. cc.writeFile();
  544. Object obj = make(cc.getName());
  545. assertEquals(7, invoke(obj, "k1"));
  546. }
  547. public void testCflow() throws Exception {
  548. CtClass cc = sloader.get("test1.Cflow");
  549. CtMethod m1 = cc.getDeclaredMethod("k1");
  550. m1.useCflow("cflow1");
  551. m1.insertBefore("System.out.println(\"$cflow1: \" + $cflow(cflow1));");
  552. m1.insertAfter("System.out.println(\"*$cflow1: \" + $cflow(cflow1));",
  553. true);
  554. CtMethod m2 = cc.getDeclaredMethod("k2");
  555. m2.useCflow("test1.t.cflow2");
  556. m2.insertBefore(
  557. "System.out.println(\"$cflow2: \" + $cflow(test1.t.cflow2));");
  558. CtMethod m3 = cc.getDeclaredMethod("fact");
  559. m3.useCflow("fact");
  560. m3.insertBefore("if ($cflow(fact) == 0)"
  561. + " System.out.println(\"fact \" + $1);");
  562. cc.writeFile();
  563. Object obj = make(cc.getName());
  564. assertEquals(1, invoke(obj, "run"));
  565. assertEquals(120, invoke(obj, "run2"));
  566. }
  567. public void testSigType() throws Exception {
  568. CtClass cc = sloader.get("test1.SigType");
  569. CtMethod m1 = cc.getDeclaredMethod("k1");
  570. m1.insertBefore("{ Class[] p = $sig; $1 += p.length; }");
  571. m1.insertAfter("System.out.println(\"testSigType: \""
  572. + " + $type.getName());", false);
  573. CtMethod m2 = cc.getDeclaredMethod("k2");
  574. m2.instrument(new ExprEditor() {
  575. public void edit(FieldAccess m) throws CannotCompileException {
  576. m.replace("{ $_ = $proceed($$) + $type.getName().length(); }");
  577. }
  578. public void edit(MethodCall m) throws CannotCompileException {
  579. m.replace("{ $_ = $proceed($$) + $sig.length; }");
  580. }
  581. });
  582. cc.writeFile();
  583. Object obj = make(cc.getName());
  584. assertEquals(19, invoke(obj, "run"));
  585. }
  586. public void testDollarClass() throws Exception {
  587. CtClass cc = sloader.get("test1.DollarClass");
  588. CtMethod m1 = cc.getDeclaredMethod("k1");
  589. m1.insertBefore("{ $1 += $class.getName().length(); }");
  590. CtMethod m2 = cc.getDeclaredMethod("k2");
  591. m2.instrument(new ExprEditor() {
  592. public void edit(MethodCall m) throws CannotCompileException {
  593. m.replace("{ $_ = $class.getName().length(); }");
  594. }
  595. });
  596. m2.insertBefore("{ $1 += $class.getName().length(); }");
  597. cc.writeFile();
  598. Object obj = make(cc.getName());
  599. assertEquals(58, invoke(obj, "run"));
  600. }
  601. public void testHandler() throws Exception {
  602. CtClass cc = sloader.get("test1.Handler");
  603. CtMethod m1 = cc.getDeclaredMethod("m1");
  604. m1.instrument(new ExprEditor() {
  605. public void edit(Handler h) throws CannotCompileException {
  606. try {
  607. print(h.getType().getName());
  608. h.insertBefore(
  609. "{ p = (($r)$1).getClass().getName().length()"
  610. + "+ $type.getName().length(); }");
  611. }
  612. catch (NotFoundException e) {
  613. throw new CannotCompileException(e);
  614. }
  615. }
  616. });
  617. cc.writeFile();
  618. Object obj = make(cc.getName());
  619. assertEquals(("java.lang.IndexOutOfBoundsException".length()
  620. + "java.lang.ClassNotFoundException".length())
  621. * 2 + 1,
  622. invoke(obj, "test"));
  623. }
  624. public void testInterface() throws Exception {
  625. String className = "test1.NewInterface";
  626. ClassPool pool = ClassPool.getDefault();
  627. CtClass targetCtClass = pool.get(className);
  628. CtClass ctInterface
  629. = pool.makeInterface(className + "2");
  630. CtMethod[] ctMethods = targetCtClass.getDeclaredMethods();
  631. for (int i = 0;i < ctMethods.length; i++) {
  632. String code = Modifier.toString(ctMethods[i].getModifiers())
  633. + " " + ctMethods[i].getReturnType().getName()
  634. + " " + ctMethods[i].getName() + "();";
  635. System.out.println(code);
  636. CtMethod m = CtNewMethod.make(code, ctInterface);
  637. ctInterface.addMethod(m);
  638. }
  639. targetCtClass.addInterface(ctInterface);
  640. targetCtClass.stopPruning(true);
  641. targetCtClass.writeFile();
  642. ctInterface.stopPruning(true);
  643. ctInterface.writeFile();
  644. ctInterface.toClass(DefineClassCapability.class);
  645. targetCtClass.toClass(DefineClassCapability.class);
  646. }
  647. public void testDispatch() throws Exception {
  648. CtClass cc = sloader.get("test1.Dispatch");
  649. CtMethod m1 = cc.getDeclaredMethod("run");
  650. m1.insertAfter("$_ += f(new Object[1]);");
  651. cc.writeFile();
  652. Object obj = make(cc.getName());
  653. assertEquals(7, invoke(obj, "run"));
  654. }
  655. public void testMakeClass()throws Exception {
  656. CtClass cc = sloader.makeClass(
  657. new FileInputStream(PATH + "test1/MakeClass.class"));
  658. assertEquals("test1.MakeClass", cc.getName());
  659. assertEquals(cc, sloader.get(cc.getName()));
  660. cc.toBytecode();
  661. assertTrue(cc.isFrozen());
  662. try {
  663. cc = sloader.makeClass(
  664. new FileInputStream(PATH + "test1/MakeClass.class"));
  665. assertTrue(false);
  666. }
  667. catch (RuntimeException e) {
  668. print(e.getMessage());
  669. }
  670. }
  671. public void testMakeMethod() throws Exception {
  672. CtClass cc = sloader.makeClass("test1.MakeMethod");
  673. cc.addField(new CtField(CtClass.intType, "i", cc));
  674. String cons_body = "{ i = 3; }";
  675. CtConstructor cons = CtNewConstructor.make(null, null,
  676. cons_body, cc);
  677. cc.addConstructor(cons);
  678. CtMethod m = CtNewMethod.make(CtClass.intType, "run", null, null,
  679. "{ return i; }", cc);
  680. cc.addMethod(m);
  681. cc.writeFile();
  682. Object obj = make(cc.getName());
  683. assertEquals(3, invoke(obj, "run"));
  684. }
  685. public void testDesc() throws Exception {
  686. Class[] sig;
  687. assertEquals(int.class, Desc.getType("I"));
  688. assertEquals(String.class, Desc.getType("Ljava/lang/String;"));
  689. assertEquals(String[].class, Desc.getType("[Ljava/lang/String;"));
  690. assertEquals(int[].class, Desc.getType("[I"));
  691. sig = Desc.getParams("()V");
  692. assertEquals(0, sig.length);
  693. sig = Desc.getParams("(I)V");
  694. assertEquals(int.class, sig[0]);
  695. assertEquals(1, sig.length);
  696. sig = Desc.getParams("(IJ)V");
  697. assertEquals(long.class, sig[1]);
  698. assertEquals(2, sig.length);
  699. sig = Desc.getParams("(Ljava/lang/String;)V");
  700. assertEquals(String.class, sig[0]);
  701. assertEquals(1, sig.length);
  702. sig = Desc.getParams("([Ljava/lang/String;I)V");
  703. assertEquals(String[].class, sig[0]);
  704. assertEquals(2, sig.length);
  705. sig = Desc.getParams("(Ljava/lang/String;[Ljava/lang/String;)V");
  706. assertEquals(String[].class, sig[1]);
  707. assertEquals(2, sig.length);
  708. }
  709. public void testCast() throws Exception {
  710. CtClass cc = sloader.makeClass("test1.CastTest");
  711. StringBuffer src = new StringBuffer();
  712. src.append("public void test(java.lang.String[] strValues)\n");
  713. src.append("{\n");
  714. src.append("\tObject[] values = new Object[2];");
  715. src.append("\tvalues[0] = strValues;");
  716. src.append("\tvalues[1] = strValues;");
  717. src.append("\tstrValues = (String[])values[0];");
  718. src.append("}\n");
  719. CtMethod m = CtNewMethod.make(src.toString(), cc);
  720. }
  721. static final long svUID = 6006955401253799668L;
  722. public void testSerialVUID() throws Exception {
  723. CtClass cc = sloader.get("test1.MySerializableClass");
  724. assertEquals(svUID, SerialVersionUID.calculateDefault(cc));
  725. SerialVersionUID.setSerialVersionUID(cc);
  726. cc.writeFile();
  727. }
  728. public void testInvokeInt() throws Exception {
  729. CtClass cc = sloader.get("test1.InvokeInt");
  730. CtMethod m1 = cc.getDeclaredMethod("check");
  731. m1.instrument(new ExprEditor() {
  732. public void edit(MethodCall m) throws CannotCompileException {
  733. m.replace("$_ = $proceed($$) + k(1);");
  734. }
  735. });
  736. cc.writeFile();
  737. Object obj = make(cc.getName());
  738. assertEquals(6, invoke(obj, "run"));
  739. }
  740. public void testSubtypeOf() throws Exception {
  741. testSubtypeOf2("java.lang.Object", "int", false);
  742. testSubtypeOf2("int[]", "java.lang.Object", true);
  743. testSubtypeOf2("int[]", "java.lang.Cloneable", true);
  744. testSubtypeOf2("java.lang.Object", "int[]", false);
  745. testSubtypeOf2("java.lang.Integer", "java.lang.Number", true);
  746. testSubtypeOf2("java.lang.Number", "java.lang.Integer", false);
  747. testSubtypeOf2("java.lang.Integer[]", "java.lang.Number[]", true);
  748. testSubtypeOf2("java.lang.Number[]", "java.lang.Integer[]", false);
  749. testSubtypeOf2("java.lang.Integer", "java.io.Serializable", true);
  750. testSubtypeOf2("java.lang.Integer", "java.lang.Object", true);
  751. }
  752. private void testSubtypeOf2(String s, String t, boolean b)
  753. throws Exception
  754. {
  755. assertTrue(sloader.get(s).subtypeOf(sloader.get(t)) == b);
  756. }
  757. public void testMakeInterface() throws Exception {
  758. CtClass cc = sloader.makeInterface("test1.MkInterface");
  759. CtMethod m = CtNewMethod.make("public abstract void ready();", cc);
  760. cc.addMethod(m);
  761. cc.writeFile();
  762. // cloader.loadClass(cc.getName());
  763. java.io.File genDir = new java.io.File(".");
  764. java.net.URLClassLoader ucl = new java.net.URLClassLoader(
  765. new java.net.URL[] { genDir.toURI().toURL() }, null);
  766. Class intf = ucl.loadClass("test1.MkInterface");
  767. }
  768. public void testCodeConv() throws Exception {
  769. CtClass cc = sloader.get("test1.CodeConv");
  770. CtClass pc = sloader.get("test1.CodeConvP");
  771. CodeConverter conv = new CodeConverter();
  772. conv.replaceFieldRead(pc.getDeclaredField("a1"), cc, "getA1");
  773. conv.replaceFieldRead(pc.getDeclaredField("a2"), cc, "getA2");
  774. conv.redirectFieldAccess(pc.getDeclaredField("a3"), cc, "a4");
  775. conv.replaceFieldWrite(pc.getDeclaredField("b1"), cc, "putB1");
  776. cc.instrument(conv);
  777. cc.writeFile();
  778. Object obj = make(cc.getName());
  779. assertEquals(51, invoke(obj, "run"));
  780. }
  781. public void testTryCatch() throws Exception {
  782. CtClass cc = sloader.get("test1.TryCatch");
  783. CtMethod m1 = cc.getDeclaredMethod("m1");
  784. m1.instrument(new ExprEditor() {
  785. public void edit(MethodCall m) throws CannotCompileException {
  786. m.replace(
  787. "try { doit(); }"
  788. + "catch(NullPointerException e){ init(); doit(); }");
  789. }
  790. });
  791. final String src =
  792. "try { doit(); }"
  793. + "catch(NullPointerException e){ init(); doit(); return a; }";
  794. CtMethod p1 = cc.getDeclaredMethod("p1");
  795. p1.insertAfter(src, true);
  796. cc.writeFile();
  797. Object obj = make(cc.getName());
  798. assertEquals(4, invoke(obj, "run"));
  799. Object obj2 = make(cc.getName());
  800. assertEquals(4, invoke(obj2, "p1"));
  801. }
  802. private CtClass[] throwablesList = null;
  803. public void testGetThrowables() throws Exception {
  804. CtClass cc = sloader.get("test1.GetThrowables");
  805. CtMethod m1 = cc.getDeclaredMethod("run");
  806. m1.instrument(new ExprEditor() {
  807. public void edit(MethodCall m) throws CannotCompileException {
  808. throwablesList = m.mayThrow();
  809. }
  810. });
  811. System.out.println(throwablesList[0].getName());
  812. System.out.println(throwablesList[1].getName());
  813. assertEquals(2, throwablesList.length);
  814. }
  815. public void testArrayAccess() throws Exception {
  816. CtClass cc = sloader.get("test1.ArrayAccess");
  817. CtMethod m1 = cc.getDeclaredMethod("test");
  818. m1.insertBefore("{ ia[0] += 1; iaa[1] = iaa[0]; }");
  819. cc.writeFile();
  820. Object obj = make(cc.getName());
  821. assertEquals(8, invoke(obj, "test"));
  822. }
  823. public void testClinit() throws Exception {
  824. CtClass cc = sloader.get("test1.Clinit");
  825. CtConstructor clinit = cc.getClassInitializer();
  826. assertTrue(clinit != null);
  827. try {
  828. clinit.insertBeforeBody(";");
  829. assertTrue(false);
  830. }
  831. catch (CannotCompileException e) {
  832. print(e.toString());
  833. assertEquals("class initializer", e.getReason());
  834. }
  835. CtConstructor[] init = cc.getConstructors();
  836. assertEquals(1, init.length);
  837. clinit.insertAfter("j += 1;");
  838. cc.writeFile();
  839. Object obj = make(cc.getName());
  840. assertEquals(457, invoke(obj, "run"));
  841. }
  842. public void testClinit2() throws Exception {
  843. CtClass cc = sloader.get("test1.Clinit2");
  844. CtConstructor clinit = cc.makeClassInitializer();
  845. clinit.insertAfter("j = 7;");
  846. cc.writeFile();
  847. Object obj = make(cc.getName());
  848. assertEquals(7, invoke(obj, "run"));
  849. }
  850. // by yamazaki
  851. public void testCondExpr() throws Exception {
  852. CtClass cc = sloader.makeClass("test1.CondExpr");
  853. CtMethod methodM = new CtMethod(CtClass.intType, "m",
  854. new CtClass[]{ CtClass.intType }, cc);
  855. methodM.setModifiers(methodM.getModifiers() | Modifier.STATIC);
  856. methodM.setBody("{if($1 <= 0) return 1; else return 0;}");
  857. cc.addMethod(methodM);
  858. cc.writeFile();
  859. Object obj = make(cc.getName());
  860. assertEquals(0, invoke(obj, "m", 3));
  861. }
  862. // by yamazaki
  863. public void testCondExpr2() throws Exception {
  864. CtClass cc = sloader.makeClass("test1.CondExpr2");
  865. CtMethod methodM = new CtMethod(CtClass.intType, "m",
  866. new CtClass[]{ CtClass.intType }, cc);
  867. methodM.setModifiers(methodM.getModifiers() | Modifier.STATIC);
  868. methodM.setBody("{return ($1 <= 0) ? 1 : (m($1 - 1) * $1);}");
  869. cc.addMethod(methodM);
  870. cc.writeFile();
  871. Object obj = make(cc.getName());
  872. assertEquals(6, invoke(obj, "m", 3));
  873. }
  874. // by yamazaki
  875. public void testCondExpr3() throws Exception {
  876. CtClass cc = sloader.makeClass("test1.CondExpr3");
  877. CtMethod methodM = CtNewMethod.make(
  878. "public abstract int m(int i);", cc);
  879. CtMethod methodN = CtNewMethod.make(
  880. "public abstract int n(int i);", cc);
  881. cc.addMethod(methodM);
  882. cc.addMethod(methodN);
  883. methodM.setBody("{return ($1 <= 0) ? 1 : (n($1 - 1) * $1);}");
  884. methodN.setBody("{return m($1);}");
  885. cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
  886. cc.writeFile();
  887. Object obj = make(cc.getName());
  888. assertEquals(6, invoke(obj, "m", 3));
  889. }
  890. public void testDelegator() throws Exception {
  891. CtClass cc = sloader.get("test1.Delegator");
  892. assertEquals("test1.SuperDelegator", cc.getSuperclass().getName());
  893. CtMethod f = sloader.getMethod("test1.SuperDelegator", "f");
  894. CtMethod g = sloader.getMethod("test1.SuperDelegator", "g");
  895. cc.addMethod(CtNewMethod.delegator(f, cc));
  896. cc.addMethod(CtNewMethod.delegator(g, cc));
  897. cc.writeFile();
  898. Object obj = make(cc.getName());
  899. assertEquals(15, invoke(obj, "run"));
  900. }
  901. public void testSetName() throws Exception {
  902. CtClass cc = sloader.get("test1.SetName");
  903. CtMethod m0 = cc.getDeclaredMethod("foo");
  904. cc.setName("test1.SetName2");
  905. assertEquals(cc, sloader.get("test1.SetName2"));
  906. assertEquals("foo(Ltest1/SetName2;)", m0.getStringRep());
  907. CtClass cc2 = sloader.makeClass("test1.SetName3");
  908. CtMethod m = CtNewMethod.make(
  909. "public int m(test1.SetName2 obj) { return ((test1.SetName2)obj).i; }",
  910. cc2);
  911. cc2.addMethod(m);
  912. cc.writeFile();
  913. cc2.writeFile();
  914. }
  915. public void testFieldModifier() throws Exception {
  916. CtClass cc = sloader.get("test1.FieldMod");
  917. CtField f = cc.getField("text");
  918. f.setModifiers(Modifier.PUBLIC);
  919. f = cc.getField("i");
  920. f.setName("j");
  921. cc.writeFile();
  922. Object obj = make(cc.getName());
  923. assertEquals(java.lang.reflect.Modifier.PUBLIC,
  924. obj.getClass().getField("text").getModifiers());
  925. assertTrue(obj.getClass().getField("j") != null);
  926. }
  927. public void testToString() throws Exception {
  928. System.out.println(sloader.get("test1.FieldMod"));
  929. System.out.println(sloader.get("java.lang.Object"));
  930. }
  931. public void testPackage() throws Exception {
  932. Object obj = new Loader().loadClass("test1.Pac").getConstructor().newInstance();
  933. assertEquals(1, invoke(obj, "run"));
  934. }
  935. public void testHoward() throws Exception {
  936. String head =
  937. "public Object lookup() throws java.rmi.RemoteException ";
  938. String src =
  939. "{ if (_remote != null) return _remote;"
  940. + " test1.HowardHome h = (test1.HowardHome)lookup(\"Howard\");"
  941. + " try { _remote = h.create(); }"
  942. + " catch (java.io.IOException e) { throw new java.rmi.RemoteException(e.getMessage(), e); }"
  943. + " return _remote; }";
  944. String src2 =
  945. "public void lookup2() {"
  946. + " try {}"
  947. + " catch (java.io.IOException e) { throw new Exception(e); }"
  948. + "}";
  949. CtClass cc = sloader.get("test1.Howard");
  950. CtMethod m = CtNewMethod.make(head + src, cc);
  951. cc.addMethod(m);
  952. try {
  953. CtMethod m2 = CtNewMethod.make(src2, cc);
  954. cc.addMethod(m2);
  955. assertTrue(false);
  956. }
  957. catch (CannotCompileException e) {}
  958. m = new CtMethod(sloader.get("java.lang.Object"),
  959. "lookup3", null, cc);
  960. m.setBody(src);
  961. m.setModifiers(Modifier.PUBLIC);
  962. m.setExceptionTypes(new CtClass[] {
  963. sloader.get("java.rmi.RemoteException") });
  964. cc.addMethod(m);
  965. cc.writeFile();
  966. Object target = make(cc.getName());
  967. Method mth = target.getClass().getMethod("lookup", new Class[0]);
  968. Object res = mth.invoke(target, new Object[0]);
  969. assertEquals("howard4", res);
  970. mth = target.getClass().getMethod("lookup3", new Class[0]);
  971. res = mth.invoke(target, new Object[0]);
  972. assertEquals("howard4", res);
  973. }
  974. public void testLoop() throws Exception {
  975. CtClass cc = sloader.makeClass("test1.Loop");
  976. CtMethod m = CtNewMethod.make(
  977. "public int run(int i) { int k = 0;"
  978. + "while (true) { if (k++ > 10) return i; } }",
  979. cc);
  980. cc.addMethod(m);
  981. cc.writeFile();
  982. Object obj = make(cc.getName());
  983. assertEquals(3, invoke(obj, "run", 3));
  984. }
  985. public static Test suite() {
  986. TestSuite suite = new TestSuite("Javassist Tests");
  987. suite.addTestSuite(JvstTest.class);
  988. suite.addTestSuite(JvstTest2.class);
  989. suite.addTestSuite(JvstTest3.class);
  990. suite.addTestSuite(JvstTest4.class);
  991. suite.addTestSuite(JvstTest5.class);
  992. suite.addTestSuite(LoaderTestByRandall.class);
  993. suite.addTestSuite(javassist.bytecode.BytecodeTest.class);
  994. suite.addTestSuite(javassist.bytecode.StackMapTest.class);
  995. suite.addTestSuite(javassist.compiler.CompTest.class);
  996. suite.addTestSuite(javassist.SetterTest.class);
  997. suite.addTestSuite(javassist.bytecode.InsertGap0.class);
  998. suite.addTestSuite(javassist.tools.reflect.LoaderTest.class);
  999. suite.addTestSuite(javassist.tools.CallbackTest.class);
  1000. suite.addTestSuite(testproxy.ProxyTester.class);
  1001. suite.addTestSuite(testproxy.ProxyFactoryPerformanceTest.class); // remove?
  1002. suite.addTestSuite(javassist.proxyfactory.ProxyFactoryTest.class);
  1003. suite.addTestSuite(javassist.proxyfactory.Tester.class);
  1004. suite.addTestSuite(javassist.HotswapTest.class);
  1005. suite.addTestSuite(test.javassist.proxy.ProxySerializationTest.class);
  1006. suite.addTestSuite(test.javassist.convert.ArrayAccessReplaceTest.class);
  1007. suite.addTestSuite(test.javassist.proxy.JASSIST113RegressionTest.class);
  1008. suite.addTestSuite(test.javassist.proxy.JBPAPP9257Test.class);
  1009. suite.addTestSuite(test.javassist.proxy.ProxyCacheGCTest.class); // remvoe?
  1010. suite.addTestSuite(test.javassist.proxy.ProxyFactoryCompatibilityTest.class);
  1011. suite.addTestSuite(test.javassist.proxy.ProxySerializationTest.class);
  1012. suite.addTestSuite(test.javassist.proxy.ProxySimpleTest.class);
  1013. suite.addTestSuite(test.javassist.bytecode.analysis.AnalyzerTest.class);
  1014. suite.addTestSuite(test.javassist.convert.ArrayAccessReplaceTest.class);
  1015. suite.addTestSuite(test.javassist.bytecode.analysis.DomTreeTest.class);
  1016. return suite;
  1017. }
  1018. }