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

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