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.

JvstTest3.java 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. package javassist;
  2. import javassist.bytecode.*;
  3. import javassist.bytecode.annotation.*;
  4. import javassist.expr.*;
  5. import test3.*;
  6. @SuppressWarnings({"rawtypes","unchecked","unused"})
  7. public class JvstTest3 extends JvstTestRoot {
  8. public void testAnnotation() throws Exception {
  9. CtClass cc = sloader.get("test3.AnnoTest");
  10. Object[] all = cc.getAnnotations();
  11. Anno a = (Anno)all[0];
  12. assertEquals('0', a.c());
  13. assertEquals(true, a.bool());
  14. assertEquals(1, a.b());
  15. assertEquals(2, a.s());
  16. assertEquals(3, a.i());
  17. assertEquals(4L, a.j());
  18. assertEquals(5.0F, a.f());
  19. assertEquals(6.0, a.d());
  20. assertEquals("7", a.str());
  21. assertEquals(AnnoTest.class, a.clazz());
  22. assertEquals(3, a.anno2().str().length);
  23. }
  24. public void testAnnotation2() throws Exception {
  25. CtClass cc = sloader.get("test3.AnnoTest2");
  26. Object[] all = cc.getAnnotations();
  27. Anno a = (Anno)all[0];
  28. assertEquals('a', a.c());
  29. assertEquals(false, a.bool());
  30. assertEquals(11, a.b());
  31. assertEquals(12, a.s());
  32. assertEquals(13, a.i());
  33. assertEquals(14L, a.j());
  34. assertEquals(15.0F, a.f());
  35. assertEquals(16.0, a.d());
  36. assertEquals("17", a.str());
  37. assertEquals(String.class, a.clazz());
  38. assertEquals(11, a.anno2().i()[0]);
  39. }
  40. public void testAnnotation3() throws Exception {
  41. CtClass cc = sloader.get("test3.AnnoTest3");
  42. Object[] all = cc.getAnnotations();
  43. assertEquals(2, all.length);
  44. int i;
  45. if (all[0] instanceof Anno2)
  46. i = 0;
  47. else
  48. i = 1;
  49. Anno2 a = (Anno2)all[i];
  50. assertEquals(1, a.i()[0]);
  51. assertEquals(test3.ColorName.RED, a.color());
  52. assertEquals(test3.ColorName.BLUE, a.color2()[0]);
  53. }
  54. public void testAnnotation4() throws Exception {
  55. CtClass cc = sloader.get("test3.AnnoTest4");
  56. Object[] all = cc.getAnnotations();
  57. Anno3 a = null;
  58. for (int i = 0; i < all.length; i++)
  59. if (all[i] instanceof Anno3)
  60. a = (Anno3)all[i];
  61. assertTrue(a != null);
  62. assertEquals('0', a.c()[0]);
  63. assertEquals(true, a.bool()[0]);
  64. assertEquals(1, a.b()[0]);
  65. assertEquals(2, a.s()[0]);
  66. assertEquals(3, a.i()[0]);
  67. assertEquals(4L, a.j()[0]);
  68. assertEquals(5.0F, a.f()[0]);
  69. assertEquals(6.0, a.d()[0]);
  70. assertEquals("7", a.str()[0]);
  71. assertEquals(AnnoTest.class, a.clazz()[0]);
  72. assertEquals(11, a.anno2()[0].i()[0]);
  73. }
  74. public void testAnnotation5() throws Exception {
  75. CtClass cc = sloader.get("test3.AnnoTest5");
  76. Object[] all = cc.getField("bar").getAnnotations();
  77. Anno2 a2 = (Anno2)all[0];
  78. assertEquals(test3.ColorName.RED, a2.color());
  79. all = cc.getDeclaredMethod("foo").getAnnotations();
  80. Anno a = (Anno)all[0];
  81. assertEquals("7", a.str());
  82. }
  83. public void testAnnotation6() throws Exception {
  84. CtClass cc = sloader.get("test3.AnnoTest6");
  85. Object[] all = cc.getAnnotations();
  86. Anno6 a = (Anno6)all[0];
  87. assertEquals(0, a.str1().length);
  88. assertEquals(0, a.str2().length);
  89. }
  90. public void testChainedException() throws Exception {
  91. try {
  92. throwChainedException();
  93. }
  94. catch (CannotCompileException e) {
  95. e.printStackTrace(System.out);
  96. }
  97. try {
  98. throwChainedException2();
  99. }
  100. catch (CannotCompileException e) {
  101. e.printStackTrace(System.out);
  102. }
  103. try {
  104. throwChainedException3();
  105. }
  106. catch (Exception e) {
  107. e.printStackTrace(System.out);
  108. }
  109. }
  110. public void throwChainedException() throws Exception {
  111. throw new CannotCompileException("test");
  112. }
  113. public void throwChainedException2() throws Exception {
  114. Throwable e = new CannotCompileException("test");
  115. throw new CannotCompileException("test2", e);
  116. }
  117. public void throwChainedException3() throws Exception {
  118. Throwable e = new CannotCompileException("testA");
  119. Throwable e2 = new CannotCompileException("testB", e);
  120. throw new Exception(e2);
  121. }
  122. // JIRA Javassist-12
  123. public void testInnerClassMethod() throws Exception {
  124. CtClass cc = sloader.get("test3.InnerMethod");
  125. CtMethod m1 = cc.getDeclaredMethod("test");
  126. m1.setBody("{inner.test();}");
  127. CtMethod m2 = CtNewMethod.make(
  128. "public int bar() {"
  129. + " if (counter-- <= 0) return 3;"
  130. + " else return bar();"
  131. + "}",
  132. cc);
  133. cc.addMethod(m2);
  134. cc.writeFile();
  135. Object obj = make(cc.getName());
  136. assertEquals(1, invoke(obj, "foo"));
  137. assertEquals(3, invoke(obj, "bar"));
  138. }
  139. public void testCheckModifyAndPruned() throws Exception {
  140. CtClass cc = sloader.get("test3.CheckModify");
  141. cc.addField(new CtField(CtClass.intType, "j", cc));
  142. cc.stopPruning(false);
  143. cc.toBytecode();
  144. try {
  145. cc.getClassFile();
  146. fail();
  147. }
  148. catch (RuntimeException e) {
  149. // System.err.println(e.getMessage());
  150. assertTrue(e.getMessage().indexOf("prune") >= 0);
  151. }
  152. }
  153. public void testReplaceNew() throws Exception {
  154. CtClass cc = sloader.get("test3.ReplaceNew");
  155. CtMethod m1 = cc.getDeclaredMethod("run");
  156. m1.instrument(new ExprEditor() {
  157. public void edit(NewExpr n) throws CannotCompileException {
  158. n.replace("{ i++; $_ = null; }");
  159. }
  160. });
  161. CtMethod m2 = cc.getDeclaredMethod("run2");
  162. m2.instrument(new ExprEditor() {
  163. public void edit(NewExpr n) throws CannotCompileException {
  164. n.replace("{ j++; $_ = null; }");
  165. }
  166. });
  167. cc.writeFile();
  168. Object obj = make(cc.getName());
  169. assertEquals(5, invoke(obj, "run"));
  170. assertEquals(2, invoke(obj, "run2"));
  171. }
  172. public void testPublicInner() throws Exception {
  173. CtClass cc0 = sloader.get("test3.PublicInner2");
  174. int mod = cc0.getClassFile2().getAccessFlags();
  175. System.out.println("testPublicInner: " + mod);
  176. CtClass cc = sloader.get("test3.PublicInner");
  177. CtClass jp = cc.makeNestedClass("Inner", true);
  178. assertEquals(Modifier.PUBLIC | Modifier.STATIC, jp.getModifiers());
  179. assertEquals(Modifier.PUBLIC | Modifier.STATIC,
  180. getPublicInner(jp, "Inner"));
  181. assertEquals(Modifier.PUBLIC | Modifier.STATIC,
  182. getPublicInner(cc, "Inner"));
  183. jp.setModifiers(Modifier.STATIC);
  184. assertEquals(Modifier.STATIC, jp.getModifiers());
  185. assertEquals(Modifier.STATIC, getPublicInner(jp, "Inner"));
  186. assertEquals(Modifier.STATIC, getPublicInner(cc, "Inner"));
  187. jp.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
  188. assertEquals(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STATIC, jp.getModifiers());
  189. assertEquals(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STATIC,
  190. getPublicInner(jp, "Inner"));
  191. assertEquals(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STATIC,
  192. getPublicInner(cc, "Inner"));
  193. cc.writeFile();
  194. jp.writeFile();
  195. }
  196. private int getPublicInner(CtClass cc, String name) throws Exception {
  197. ClassFile cf = cc.getClassFile();
  198. InnerClassesAttribute ica
  199. = (InnerClassesAttribute)cf.getAttribute(
  200. InnerClassesAttribute.tag);
  201. assertEquals(name, ica.innerName(0));
  202. return ica.accessFlags(0);
  203. }
  204. public void testConstructorToMethod() throws Exception {
  205. CtClass cc = sloader.get("test3.Constructor");
  206. CtConstructor[] cons = cc.getConstructors();
  207. CtConstructor sinit = cc.getClassInitializer();
  208. for (int i = 0; i < cons.length; i++) {
  209. CtConstructor ccons = cons[i];
  210. String desc = ccons.getSignature();
  211. boolean result = false;
  212. if (desc.equals("()V"))
  213. result = false;
  214. else if (desc.equals("(I)V"))
  215. result = true;
  216. else if (desc.equals("(Ljava/lang/String;)V"))
  217. result = false;
  218. else if (desc.equals("(D)V"))
  219. result = true;
  220. else
  221. fail("unknonw constructor");
  222. assertEquals(result, ccons.callsSuper());
  223. }
  224. CtClass cc2 = sloader.get("test3.Constructor2");
  225. for (int i = 0; i < cons.length; i++)
  226. cc2.addMethod(cons[i].toMethod("m", cc2));
  227. cc2.addMethod(sinit.toMethod("sinit", cc2));
  228. cc2.addMethod(CtMethod.make(
  229. "public int run() { m(); m(5); m(\"s\"); m(0.0);" +
  230. " sinit(); return i + str.length(); }",
  231. cc2));
  232. cc2.writeFile();
  233. Object obj = make(cc2.getName());
  234. assertEquals(119, invoke(obj, "run"));
  235. }
  236. public void testUnique() throws Exception {
  237. CtClass cc = sloader.get("test3.Unique");
  238. CtClass cc2 = sloader.get("test3.Unique3");
  239. assertEquals("poi", cc.makeUniqueName("poi"));
  240. assertEquals("foo102", cc.makeUniqueName("foo"));
  241. assertEquals("bar102", cc2.makeUniqueName("bar"));
  242. assertEquals("foo100", cc2.makeUniqueName("foo"));
  243. }
  244. public void testGetMethods() throws Exception {
  245. CtClass cc = sloader.get("test3.GetMethods");
  246. assertEquals(3, cc.getConstructors().length);
  247. assertEquals(6, cc.getFields().length);
  248. assertEquals(6 + Object.class.getMethods().length + 2,
  249. cc.getMethods().length);
  250. }
  251. public void testVisiblity() throws Exception {
  252. CtClass cc = sloader.get("test3.Visible");
  253. CtClass cc2 = sloader.get("test3.Visible2");
  254. CtClass subcc = sloader.get("test3.sub.Visible");
  255. CtClass subcc2 = sloader.get("test3.sub.Visible2");
  256. CtClass top = sloader.get("VisibleTop");
  257. CtClass top2 = sloader.get("VisibleTop2");
  258. assertEquals(true, cc.getField("pub").visibleFrom(cc2));
  259. assertEquals(true, cc.getField("pub").visibleFrom(subcc));
  260. assertEquals(true, cc.getField("pri").visibleFrom(cc));
  261. assertEquals(false, cc.getField("pri").visibleFrom(cc2));
  262. assertEquals(true, cc.getField("pack").visibleFrom(cc));
  263. assertEquals(true, cc.getField("pack").visibleFrom(cc2));
  264. assertEquals(false, cc.getField("pack").visibleFrom(subcc));
  265. assertEquals(false, cc.getField("pack").visibleFrom(top));
  266. assertEquals(true, cc.getField("pro").visibleFrom(cc));
  267. assertEquals(true, cc.getField("pro").visibleFrom(cc2));
  268. assertEquals(true, cc.getField("pro").visibleFrom(subcc2));
  269. assertEquals(false, cc.getField("pro").visibleFrom(subcc));
  270. assertEquals(false, cc.getField("pro").visibleFrom(top));
  271. assertEquals(true, top.getField("pack").visibleFrom(top2));
  272. assertEquals(false, top.getField("pack").visibleFrom(cc));
  273. }
  274. public void testNewAnnotation() throws Exception {
  275. CtClass c = sloader.makeClass("test3.NewClass");
  276. ClassFile cf = c.getClassFile();
  277. ConstPool cp = cf.getConstPool();
  278. AnnotationsAttribute attr
  279. = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
  280. javassist.bytecode.annotation.Annotation a
  281. = new Annotation("test3.ChibaAnnotation", cp);
  282. a.addMemberValue("name", new StringMemberValue("Chiba", cp));
  283. a.addMemberValue("version", new StringMemberValue("Chiba", cp));
  284. a.addMemberValue("description", new StringMemberValue("Chiba", cp));
  285. a.addMemberValue("interfaceName", new StringMemberValue("Chiba", cp));
  286. attr.setAnnotation(a);
  287. System.out.println(attr);
  288. cf.addAttribute(attr);
  289. cf.setVersionToJava5();
  290. Object [] ans = c.getAnnotations() ;
  291. System.out.println("Num Annotation : " +ans.length);
  292. // c.debugWriteFile();
  293. Class newclass = c.toClass(DefineClassCapability.class);
  294. java.lang.annotation.Annotation[] anns = newclass.getAnnotations();
  295. System.out.println("Num NewClass Annotation : " +anns.length);
  296. assertEquals(ans.length, anns.length);
  297. }
  298. public void testRecursiveReplace() throws Exception {
  299. CtClass cc = sloader.get("test3.RecReplace");
  300. CtMethod m1 = cc.getDeclaredMethod("run");
  301. final ExprEditor e2 = new ExprEditor() {
  302. public void edit(MethodCall mc) throws CannotCompileException {
  303. if (mc.getMethodName().equals("bar")) {
  304. mc.replace("{ double k = 10.0; $_ = $proceed($1 + k); }");
  305. }
  306. }
  307. };
  308. m1.instrument(new ExprEditor() {
  309. public void edit(MethodCall mc) throws CannotCompileException {
  310. if (mc.getMethodName().equals("foo")) {
  311. mc.replace("{ int k = bar($$); $_ = k + $proceed(7.0); }",
  312. e2);
  313. }
  314. }
  315. });
  316. CtMethod m2 = cc.getDeclaredMethod("run2");
  317. m2.instrument(new ExprEditor() {
  318. public void edit(MethodCall mc) throws CannotCompileException {
  319. if (mc.getMethodName().equals("foo")) {
  320. mc.replace("{ int k = bar($$); $_ = k + $proceed(7.0); }");
  321. }
  322. }
  323. });
  324. cc.writeFile();
  325. Object obj = make(cc.getName());
  326. assertEquals(26, invoke(obj, "run"));
  327. assertEquals(16, invoke(obj, "run2"));
  328. }
  329. public void testRecursiveReplace2() throws Exception {
  330. final ExprEditor[] ref = new ExprEditor[1];
  331. ExprEditor e2 = new ExprEditor() {
  332. public void edit(FieldAccess fa) throws CannotCompileException {
  333. if (fa.getFieldName().equals("value2")
  334. && fa.isWriter()) {
  335. fa.replace("{ $_ = $proceed($1 + 2); }");
  336. }
  337. }
  338. };
  339. ExprEditor e1 = new ExprEditor() {
  340. public void edit(FieldAccess fa) throws CannotCompileException {
  341. if (fa.getFieldName().equals("value")
  342. && fa.isWriter()) {
  343. fa.replace("{ value2 = $1; value = value2; }",
  344. ref[0]);
  345. }
  346. }
  347. };
  348. CtClass cc = sloader.get("test3.RecReplace2");
  349. CtMethod m1 = cc.getDeclaredMethod("run");
  350. ref[0] = e2;
  351. m1.instrument(e1);
  352. CtMethod m2 = cc.getDeclaredMethod("run2");
  353. ref[0] = null;
  354. m2.instrument(e1);
  355. cc.writeFile();
  356. Object obj = make(cc.getName());
  357. assertEquals(28, invoke(obj, "run"));
  358. assertEquals(24, invoke(obj, "run2"));
  359. }
  360. public void testInnerModifier() throws Exception {
  361. CtClass cc = sloader.get("test3.InnerClass$Inner");
  362. assertEquals(Modifier.PUBLIC | Modifier.STATIC, cc.getModifiers());
  363. CtClass cc2 = sloader.get("test3.InnerClass$Inner2");
  364. assertEquals(Modifier.PUBLIC, cc2.getModifiers());
  365. }
  366. public void testMethodLookup() throws Exception {
  367. CtClass cc = sloader.get("test3.SubValue");
  368. CtMethod m1 = CtNewMethod.make(
  369. "public int run() {" +
  370. " test3.SuperValue sup = new test3.SuperValue();" +
  371. " test3.SubValue sub = new test3.SubValue();" +
  372. " return this.after(sup, sub, sub) == null ? 0 : 1;" +
  373. "}",
  374. cc);
  375. cc.addMethod(m1);
  376. cc.writeFile();
  377. Object obj = make(cc.getName());
  378. assertEquals(1, invoke(obj, "run"));
  379. }
  380. public void testFieldAccessType() throws Exception {
  381. CtClass cc = sloader.get("test3.FieldAccessType");
  382. CtMethod m1 = cc.getDeclaredMethod("access");
  383. final boolean[] result = new boolean[1];
  384. result[0] = true;
  385. ExprEditor e = new ExprEditor() {
  386. public void edit(FieldAccess fa) throws CannotCompileException {
  387. if (!fa.getSignature().equals("[I"))
  388. result[0] = false;
  389. }
  390. };
  391. m1.instrument(e);
  392. assertTrue(result[0]);
  393. }
  394. public void testGetNestedClasses() throws Exception {
  395. CtClass cc = sloader.get("test3.NestedClass");
  396. CtClass[] nested = cc.getNestedClasses();
  397. assertEquals(4, nested.length);
  398. testGetNestedClasses("test3.NestedClass$Inner", nested);
  399. testGetNestedClasses("test3.NestedClass$StaticNested", nested);
  400. testGetNestedClasses("test3.NestedClass$1Local", nested);
  401. testGetNestedClasses("test3.NestedClass$1", nested);
  402. }
  403. private void testGetNestedClasses(String name, CtClass[] classes) {
  404. for (int i = 0; i < classes.length; i++)
  405. if (classes[i].getName().equals(name))
  406. return;
  407. fail("no class: " + name);
  408. }
  409. public void testGetParmeterAnnotations() throws Exception {
  410. CtClass cc = sloader.get("test3.ParamAnno");
  411. Object[][] anno = cc.getDeclaredMethod("foo").getParameterAnnotations();
  412. assertEquals(4, anno.length);
  413. assertEquals(0, anno[0].length);
  414. assertEquals(0, anno[1].length);
  415. assertEquals(0, anno[2].length);
  416. assertEquals(0, anno[3].length);
  417. Object[][] anno2 = cc.getDeclaredMethod("bar").getParameterAnnotations();
  418. assertEquals(0, anno2.length);
  419. Class rc = Class.forName("test3.ParamAnno");
  420. java.lang.reflect.Method[] ms = rc.getDeclaredMethods();
  421. java.lang.reflect.Method m1, m2;
  422. if (ms[0].getName().equals("foo")) {
  423. m1 = ms[0];
  424. m2 = ms[1];
  425. }
  426. else {
  427. m1 = ms[1];
  428. m2 = ms[0];
  429. }
  430. java.lang.annotation.Annotation[][] ja;
  431. ja = m1.getParameterAnnotations();
  432. assertEquals(4, ja.length);
  433. assertEquals(0, ja[0].length);
  434. assertEquals(0, ja[1].length);
  435. assertEquals(0, ja[2].length);
  436. assertEquals(0, ja[3].length);
  437. assertEquals(0, m2.getParameterAnnotations().length);
  438. }
  439. public void testSetModifiers() throws Exception {
  440. CtClass cc = sloader.get("test3.SetModifiers");
  441. try {
  442. cc.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
  443. fail("static public class SetModifiers");
  444. }
  445. catch (RuntimeException e) {
  446. assertEquals("cannot change test3.SetModifiers into a static class", e.getMessage());
  447. }
  448. cc = sloader.get("test3.SetModifiers$A");
  449. cc.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
  450. assertTrue(Modifier.isStatic(cc.getModifiers()));
  451. assertTrue((cc.getClassFile2().getAccessFlags() & AccessFlag.STATIC) == 0);
  452. }
  453. public void testFieldCopy() throws Exception {
  454. CtClass cc = sloader.get("test3.FieldCopy");
  455. CtClass cc2 = sloader.get("test3.FieldCopy2");
  456. CtField f = cc.getDeclaredField("foo");
  457. cc2.addField(new CtField(f, cc2));
  458. CtField f2 = cc2.getDeclaredField("foo");
  459. Object[] anno = f2.getAnnotations();
  460. assertTrue(anno[0] instanceof test3.FieldCopy.Test);
  461. assertEquals(Modifier.PRIVATE | Modifier.STATIC,
  462. f2.getModifiers());
  463. }
  464. public void testMethodRedirect() throws Exception {
  465. CtClass cc = sloader.get("test3.MethodRedirect");
  466. CtClass cc2 = sloader.get("test3.MethodRedirectIntf");
  467. CtMethod foo = cc.getDeclaredMethod("foo");
  468. CtMethod poi = cc.getDeclaredMethod("poi");
  469. CtMethod bar = cc.getDeclaredMethod("bar");
  470. CtMethod afo = cc2.getDeclaredMethod("afo");
  471. CodeConverter conv = new CodeConverter();
  472. try {
  473. conv.redirectMethodCall(foo, bar);
  474. fail("foo");
  475. }
  476. catch (CannotCompileException e) {}
  477. try {
  478. conv.redirectMethodCall(poi, bar);
  479. fail("bar");
  480. }
  481. catch (CannotCompileException e) {}
  482. try {
  483. conv.redirectMethodCall(bar, afo);
  484. fail("afo");
  485. }
  486. catch (CannotCompileException e) {}
  487. bar.setName("bar2");
  488. conv.redirectMethodCall("bar", bar);
  489. cc.instrument(conv);
  490. // cc.writeFile();
  491. Object obj = make(cc.getName());
  492. assertEquals(2, invoke(obj, "test"));
  493. }
  494. public void testMethodRedirect2() throws Exception {
  495. CtClass cc = sloader.get("test3.MethodRedirect2");
  496. CtClass sup = sloader.get("test3.MethodRedirect2Sup");
  497. CtClass supsup = sloader.get("test3.MethodRedirect2SupSup");
  498. CtClass intf = sloader.get("test3.MethodRedirect2SupIntf");
  499. CtMethod bfo2 = supsup.getDeclaredMethod("bfo2");
  500. CtMethod afo2 = sup.getDeclaredMethod("afo2");
  501. CtMethod foo = intf.getDeclaredMethod("foo");
  502. CodeConverter conv = new CodeConverter();
  503. conv.redirectMethodCall("bfo", bfo2);
  504. conv.redirectMethodCall("afo", afo2);
  505. conv.redirectMethodCall("bar", foo);
  506. conv.redirectMethodCall("bar2", foo);
  507. cc.instrument(conv);
  508. cc.writeFile();
  509. Object obj = make(cc.getName());
  510. assertEquals(524, invoke(obj, "test"));
  511. }
  512. public void testMethodRedirectToStatic() throws Exception {
  513. CtClass targetClass = sloader.get("test3.MethodRedirectToStatic");
  514. CtClass staticClass = sloader.get("test3.MethodRedirectToStatic2");
  515. CtMethod targetMethod = targetClass.getDeclaredMethod("add");
  516. CtMethod staticMethod = staticClass.getDeclaredMethod("add2");
  517. CodeConverter conv = new CodeConverter();
  518. conv.redirectMethodCallToStatic(targetMethod, staticMethod);
  519. targetClass.instrument(conv);
  520. targetClass.writeFile();
  521. Object obj = make(targetClass.getName());
  522. assertEquals(30, invoke(obj, "test"));
  523. }
  524. public void testClassMap() throws Exception {
  525. ClassMap map = new ClassMap();
  526. map.put("aa", "AA");
  527. map.put("xx", "XX");
  528. assertEquals("AA", map.get("aa"));
  529. assertEquals(null, map.get("bb"));
  530. ClassMap map2 = new ClassMap(map);
  531. map2.put("aa", "A1");
  532. map2.put("cc", "CC");
  533. assertEquals("A1", map2.get("aa"));
  534. assertEquals("CC", map2.get("cc"));
  535. assertEquals("XX", map2.get("xx"));
  536. assertEquals(null, map2.get("bb"));
  537. }
  538. public void testEmptyConstructor() throws Exception {
  539. CtClass cc = sloader.get("test3.EmptyConstructor");
  540. CtConstructor[] cons = cc.getDeclaredConstructors();
  541. for (int i = 0; i < cons.length; i++)
  542. assertTrue("index: " + i, cons[i].isEmpty());
  543. cc = sloader.get("test3.EmptyConstructor2");
  544. cons = cc.getDeclaredConstructors();
  545. for (int i = 0; i < cons.length; i++)
  546. assertTrue("index: " + i, cons[i].isEmpty());
  547. cc = sloader.get("test3.EmptyConstructor3");
  548. cons = cc.getDeclaredConstructors();
  549. for (int i = 0; i < cons.length; i++)
  550. assertTrue("index: " + i, cons[i].isEmpty());
  551. cc = sloader.get("test3.EmptyConstructor4");
  552. cons = cc.getDeclaredConstructors();
  553. for (int i = 0; i < cons.length; i++)
  554. assertFalse("index: " + i, cons[i].isEmpty());
  555. }
  556. public void testTransformRead() throws Exception {
  557. CtClass cc = sloader.get("test3.TransformRead");
  558. CtClass parent = cc.getSuperclass();
  559. CtMethod m = cc.getDeclaredMethod("foo");
  560. CodeConverter conv = new CodeConverter();
  561. conv.replaceFieldRead(parent.getField("value"), cc, "getValue");
  562. conv.replaceFieldRead(parent.getField("value2"), cc, "getValue2");
  563. m.instrument(conv);
  564. cc.writeFile();
  565. Object obj = make(cc.getName());
  566. assertEquals(11100, invoke(obj, "foo"));
  567. }
  568. public void testDescriptor() throws Exception {
  569. assertEquals("int", Descriptor.toString("I"));
  570. assertEquals("long[][]", Descriptor.toString("[[J"));
  571. assertEquals("java.lang.Object", Descriptor.toString("Ljava/lang/Object;"));
  572. assertEquals("()", Descriptor.toString("()V"));
  573. assertEquals("(int)", Descriptor.toString("(I)V"));
  574. assertEquals("(int,int[])", Descriptor.toString("(I[I)V"));
  575. assertEquals("(java.lang.String,Foo[][])", Descriptor.toString("(Ljava/lang/String;[[LFoo;)V"));
  576. }
  577. public void testLongName() throws Exception {
  578. CtClass cc = sloader.get("test3.Name");
  579. assertEquals("test3.Name.<clinit>()", cc.getClassInitializer().getLongName());
  580. assertEquals("test3.Name()", cc.getConstructor("()V").getLongName());
  581. assertEquals("test3.Name(int)", cc.getConstructor("(I)V").getLongName());
  582. assertEquals("test3.Name(test3.Name)", cc.getConstructor("(Ltest3/Name;)V").getLongName());
  583. assertEquals("test3.Name(test3.Name,java.lang.String)",
  584. cc.getConstructor("(Ltest3/Name;Ljava/lang/String;)V").getLongName());
  585. assertEquals("test3.Name.foo()", cc.getDeclaredMethod("foo").getLongName());
  586. assertEquals("test3.Name.foo2(int)", cc.getDeclaredMethod("foo2").getLongName());
  587. assertEquals("test3.Name.foo3(java.lang.String)", cc.getDeclaredMethod("foo3").getLongName());
  588. assertEquals("test3.Name.foo4(java.lang.String[])", cc.getDeclaredMethod("foo4").getLongName());
  589. assertEquals("test3.Name.foo5(int,java.lang.String)", cc.getDeclaredMethod("foo5").getLongName());
  590. }
  591. public void testPackageName() throws Exception {
  592. CtClass cc = sloader.get("test3.PackName");
  593. CtMethod m1 = CtNewMethod.make(
  594. "public int run() {" +
  595. " return test3.PackName.get() + test3.sub.SubPackName.get(); }",
  596. cc);
  597. cc.addMethod(m1);
  598. cc.writeFile();
  599. Object obj = make(cc.getName());
  600. assertEquals(8, invoke(obj, "run"));
  601. }
  602. public void testErasure() throws Exception {
  603. CtClass cc = sloader.get("test3.Erasure");
  604. cc.addInterface(sloader.get("test3.ErasureGet"));
  605. CtMethod m1 = CtNewMethod.make(
  606. "public Object get() { return value; }",
  607. cc);
  608. cc.addMethod(m1);
  609. cc.writeFile();
  610. Object obj = make(cc.getName());
  611. assertEquals(4, invoke(obj, "run"));
  612. }
  613. /* Check the result of JvstTest#testFieldInit()
  614. * This tests CtClassType#saveClassFile().
  615. */
  616. public void testFieldInitAgain() throws Exception {
  617. System.gc();
  618. CtClass cc = sloader.get("test1.FieldInit");
  619. CtField f = cc.getDeclaredField("f1");
  620. assertEquals(CtClass.intType, f.getType());
  621. assertTrue("f.hashCode() doesn't change!", f.hashCode() != JvstTest.testFieldInitHash);
  622. }
  623. /* This tests CtClassType#saveClassFile().
  624. * A CtMethod is not garbage collected, its CtClass is never
  625. * compressed.
  626. */
  627. public void testCalleeBeforeAgain() throws Exception {
  628. CtClass cc = sloader.get("test1.CalleeBefore");
  629. assertEquals(JvstTest.testCalleeBeforeMethod,
  630. cc.getDeclaredMethod("m1"));
  631. assertEquals(JvstTest.testCalleeBeforeMethod2,
  632. cc.getDeclaredMethod("m2").getMethodInfo2().hashCode());
  633. }
  634. public void testSetSuper() throws Exception {
  635. CtClass cc = sloader.get("test3.Superclass");
  636. CtClass cc3 = sloader.get("test3.Superclass3");
  637. cc3.setModifiers(Modifier.setPublic(cc3.getModifiers()));
  638. cc.setSuperclass(sloader.get("test3.Superclass3"));
  639. cc.writeFile();
  640. cc3.writeFile();
  641. Object obj = make(cc.getName());
  642. assertEquals(21, invoke(obj, "test"));
  643. }
  644. public void testFrozen2() throws Exception {
  645. CtClass cc = sloader.get("test3.Frozen");
  646. cc.addField(new CtField(CtClass.intType, "k", cc));
  647. cc.toBytecode();
  648. cc.toBytecode();
  649. cc = sloader.makeClass("test3.Frozen2");
  650. cc.toBytecode();
  651. cc.toBytecode();
  652. }
  653. public void testCopyAnnotation() throws Exception {
  654. CtClass cc1 = sloader.get("test3.CopyAnnoBase");
  655. CtMethod m1 = cc1.getDeclaredMethod("getX");
  656. CtClass cc2 = sloader.get("test3.CopyAnno");
  657. CtMethod m2 = cc2.getDeclaredMethod("getX");
  658. copyAnnotations(m1, m2);
  659. cc2.getClassFile();
  660. Class clazz = cc2.toClass(DefineClassCapability.class);
  661. java.lang.reflect.Method m = clazz.getDeclaredMethod("getX", new Class[0]);
  662. assertEquals(1, m.getAnnotations().length);
  663. test3.VisibleAnno a = m.getAnnotation(test3.VisibleAnno.class);
  664. assertNotNull(a);
  665. }
  666. private void copyAnnotations(CtMethod src, CtMethod dest)
  667. throws NotFoundException
  668. {
  669. MethodInfo srcInfo = src.getMethodInfo2();
  670. MethodInfo destInfo = dest.getMethodInfo2();
  671. copyAnnotations(srcInfo, destInfo, AnnotationsAttribute.invisibleTag);
  672. copyAnnotations(srcInfo, destInfo, AnnotationsAttribute.visibleTag);
  673. }
  674. private void copyAnnotations(MethodInfo src, MethodInfo dest, String annotationTag) {
  675. AnnotationsAttribute attribute = (AnnotationsAttribute)src.getAttribute(annotationTag);
  676. if (attribute != null)
  677. dest.addAttribute(attribute.copy(dest.getConstPool(), new java.util.HashMap()));
  678. }
  679. public void testStaticFinalField() throws Exception {
  680. CtClass cc = sloader.makeClass("test3.StaticFinalField");
  681. CtField fj = new CtField(CtClass.longType, "j", cc);
  682. fj.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  683. cc.addField(fj, CtField.Initializer.constant(2L));
  684. CtField fi = new CtField(CtClass.intType, "i", cc);
  685. fi.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  686. cc.addField(fi, CtField.Initializer.constant(3));
  687. CtField fs = new CtField(CtClass.shortType, "s", cc);
  688. fs.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  689. cc.addField(fs, CtField.Initializer.constant(4));
  690. CtField fc = new CtField(CtClass.charType, "c", cc);
  691. fc.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  692. cc.addField(fc, CtField.Initializer.constant('5'));
  693. CtField fby = new CtField(CtClass.byteType, "by", cc);
  694. fby.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  695. cc.addField(fby, CtField.Initializer.constant(6));
  696. CtField fb = new CtField(CtClass.booleanType, "b", cc);
  697. fb.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  698. cc.addField(fb, CtField.Initializer.constant(true));
  699. CtField ff = new CtField(CtClass.floatType, "f", cc);
  700. ff.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  701. cc.addField(ff, CtField.Initializer.constant(7.0F));
  702. CtField fstr = new CtField(sloader.get("java.lang.String"), "str", cc);
  703. fstr.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  704. cc.addField(fstr, CtField.Initializer.constant("foo"));
  705. CtField fobj = new CtField(sloader.get("java.lang.Object"), "obj", cc);
  706. fobj.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
  707. cc.addField(fobj, CtField.Initializer.constant("bar"));
  708. cc.writeFile();
  709. Class clazz = cc.toClass(DefineClassCapability.class);
  710. assertEquals(2L, clazz.getField("j").getLong(null));
  711. assertEquals(3, clazz.getField("i").getInt(null));
  712. assertEquals(4, clazz.getField("s").getShort(null));
  713. assertEquals('5', clazz.getField("c").getChar(null));
  714. assertEquals(6, clazz.getField("by").getByte(null));
  715. assertEquals(true, clazz.getField("b").getBoolean(null));
  716. assertEquals(7.0F, clazz.getField("f").getFloat(null));
  717. assertEquals("foo", clazz.getField("str").get(null));
  718. assertEquals("bar", clazz.getField("obj").get(null));
  719. }
  720. /*
  721. public void testClassPath() throws Exception {
  722. ClassPool cp = new ClassPool(null);
  723. cp.appendClassPath("./test-classpath.JaR");
  724. assertNotNull(cp.get("test.Point"));
  725. cp = new ClassPool(null);
  726. cp.appendClassPath("./*");
  727. assertNotNull(cp.get("javassist.bytecode.Gap0Example"));
  728. }*/
  729. public void testVoidReturn() throws Exception {
  730. CtClass cc = sloader.get("test3.VoidReturn");
  731. CtMethod m1 = cc.getDeclaredMethod("foo");
  732. m1.insertAfter("System.out.println(\"return value: \" + $_);", true);
  733. cc.writeFile();
  734. make(cc.getName());
  735. }
  736. public void testInsertParam0() throws Exception {
  737. assertEquals("(I)V", Descriptor.insertParameter(CtClass.intType, "()V"));
  738. assertEquals("(ILjava/lang/Object;)V", Descriptor.insertParameter(CtClass.intType, "(Ljava/lang/Object;)V"));
  739. assertEquals("(IJ)V", Descriptor.appendParameter(CtClass.longType, "(I)V"));
  740. assertEquals("(Ljava/lang/String;I)V", Descriptor.insertParameter(sloader.get("java.lang.String"), "(I)V"));
  741. }
  742. public void testInsertParam() throws Exception {
  743. CtClass cc = sloader.get("test3.InsParam");
  744. CtMethod m1 = cc.getDeclaredMethod("foo");
  745. m1.insertParameter(CtClass.longType);
  746. m1.insertBefore("$2 += (int)$1;");
  747. CtMethod m2 = cc.getDeclaredMethod("bar");
  748. m2.addParameter(CtClass.doubleType);
  749. m2.insertBefore("$1 += (int)$2;");
  750. CtMethod m3 = cc.getDeclaredMethod("poi");
  751. m3.addParameter(sloader.get("java.lang.Object"));
  752. m3.insertBefore("$2 = (String)$3;");
  753. cc.writeFile();
  754. Object obj = make(cc.getName());
  755. assertEquals(11, invoke(obj, "foo", 10L, 1));
  756. assertEquals(11, invoke(obj, "bar", 1, 10.0));
  757. assertEquals(3, invoke(obj, "poi", 1, "x", "xx"));
  758. }
  759. private int invoke(Object target, String method, long arg1, int arg2)
  760. throws Exception
  761. {
  762. java.lang.reflect.Method m =
  763. target.getClass().getMethod(method, new Class[] { long.class, int.class });
  764. Object res = m.invoke(target, new Object[] { Long.valueOf(arg1), Integer.valueOf(arg2)});
  765. return ((Integer)res).intValue();
  766. }
  767. private int invoke(Object target, String method, int arg1, double arg2)
  768. throws Exception
  769. {
  770. java.lang.reflect.Method m =
  771. target.getClass().getMethod(method, new Class[] { int.class, double.class });
  772. Object res = m.invoke(target, new Object[] { Integer.valueOf(arg1), Double.valueOf(arg2)});
  773. return ((Integer)res).intValue();
  774. }
  775. private int invoke(Object target, String method, int arg1, String arg2, Object arg3)
  776. throws Exception
  777. {
  778. java.lang.reflect.Method m =
  779. target.getClass().getMethod(method, new Class[] { int.class, String.class, Object.class });
  780. Object res = m.invoke(target, new Object[] { Integer.valueOf(arg1), arg2, arg3});
  781. return ((Integer)res).intValue();
  782. }
  783. public void testInvokeinterface() throws Exception {
  784. // JIRA JASSIST-60
  785. CtClass cc = sloader.get("test3.InvokeIntf");
  786. CtMethod mth = cc.getDeclaredMethod("doit");
  787. ExprEditor e = new ExprEditor() {
  788. public void edit(MethodCall m) throws CannotCompileException {
  789. String block = "{$_=$proceed($$);}";
  790. m.replace(block);
  791. }
  792. };
  793. mth.instrument(e);
  794. cc.writeFile();
  795. Object obj = make(cc.getName());
  796. assertEquals(7, invoke(obj, "test"));
  797. }
  798. public void testInvokeArrayObj() throws Exception {
  799. // JIRA JASSIST-61
  800. CtClass cc = sloader.get("test3.InvokeArray");
  801. CtMethod mth = cc.getDeclaredMethod("doit");
  802. ExprEditor e = new ExprEditor() {
  803. public void edit(MethodCall m) throws CannotCompileException {
  804. String block = "{$_=$proceed($$);}";
  805. m.replace(block);
  806. }
  807. };
  808. mth.instrument(e);
  809. cc.writeFile();
  810. Object obj = make(cc.getName());
  811. assertEquals(3, invoke(obj, "test"));
  812. }
  813. public void testNewExprTry() throws Exception {
  814. CtClass cc = sloader.get("test3.NewExprTryCatch");
  815. CtMethod mth = cc.getDeclaredMethod("instrumentMe");
  816. ExprEditor e = new ExprEditor() {
  817. public void edit(NewExpr m) throws CannotCompileException {
  818. String block = "{$_=$proceed($$);}";
  819. m.replace(block);
  820. }
  821. };
  822. mth.instrument(e);
  823. // JIRA JASSIST-52
  824. CtMethod mth2 = cc.getDeclaredMethod("me2");
  825. ExprEditor e2 = new ExprEditor() {
  826. public void edit(MethodCall m) throws CannotCompileException {
  827. String block = "{try{$_=$proceed($$);} catch(Throwable t) {}}";
  828. m.replace(block);
  829. }
  830. };
  831. //mth2.instrument(e2);
  832. /*
  833. // JIRA JASSIST-53
  834. CtClass cc2 = sloader.get("test3.NewExprTryCatch2");
  835. CtBehavior mth3 = cc2.getDeclaredConstructors()[0];
  836. ExprEditor e3 = new ExprEditor() {
  837. public void edit(ConstructorCall m) throws CannotCompileException {
  838. String block = "{try {$_=$proceed($$);} catch (Throwable t) {}}";
  839. m.replace(block);
  840. }
  841. };
  842. mth3.instrument(e3);
  843. */
  844. cc.writeFile();
  845. // cc2.writeFile();
  846. Object obj = make(cc.getName());
  847. assertEquals(0, invoke(obj, "test"));
  848. // Object obj2 = make(cc2.getName());
  849. }
  850. public void testJIRA63() throws Exception {
  851. frameTypeTest(2);
  852. frameTypeTest(7);
  853. }
  854. private void frameTypeTest(final int initializerRepeatCount) throws Exception {
  855. // Get a class
  856. final CtClass cTst = sloader.get("test3.JIRA63");
  857. cTst.getClassFile().setMajorVersion(ClassFile.JAVA_6);
  858. try {
  859. // Create an initializer for the fields
  860. String initializer = "test3.JIRA63Helper";
  861. for(int i=0; i < initializerRepeatCount; i++)
  862. initializer += ".getAnObject(new Integer(1))";
  863. initializer += ";";
  864. // Add some fields
  865. final CtClass cObj = sloader.get("java.lang.Object");
  866. for(int i = 0; i < 7; i++)
  867. cTst.addField(new CtField(cObj, "a" + i, cTst), initializer);
  868. // Modify the constructors
  869. for(final CtConstructor m : cTst.getConstructors()) {
  870. m.insertAfter(initializer, true);
  871. m.insertBefore(initializer);
  872. }
  873. // Get the byte code.
  874. // cTst.toBytecode();
  875. cTst.writeFile();
  876. //make(cTst.getName());
  877. }
  878. finally {
  879. cTst.detach();
  880. }
  881. }
  882. public void testInsertBeforeType() throws Exception {
  883. CtClass cc = sloader.get("test3.InsertBeforeType");
  884. CtMethod m1 = cc.getDeclaredMethod("foo");
  885. m1.insertBefore("value = $type.getName();");
  886. cc.writeFile();
  887. Object obj = make(cc.getName());
  888. assertEquals(5, invoke(obj, "test"));
  889. }
  890. public void testTransformNewClass() throws Exception {
  891. CodeConverter conv = new CodeConverter();
  892. conv.replaceNew(sloader.get("test3.TransNewClassOld"),
  893. sloader.get("test3.TransNewClassNew"));
  894. CtClass cc = sloader.get("test3.TransNewClass");
  895. cc.instrument(conv);
  896. cc.writeFile();
  897. CtClass cc2 = sloader.get("test3.TransNewClass$TransNewClass2");
  898. cc2.instrument(conv);
  899. cc2.writeFile();
  900. Object obj = make(cc.getName());
  901. assertEquals(170, invoke(obj, "test"));
  902. Object obj2 = make(cc2.getName());
  903. assertEquals(50, invoke(obj2, "test"));
  904. }
  905. public void testInsertAfter() throws Exception {
  906. CtClass cc = sloader.get("test3.InsertAfter");
  907. CtMethod m1 = cc.getDeclaredMethod("foo");
  908. m1.insertAfter("k++;", true);
  909. CtConstructor cons = cc.getConstructor("()V");
  910. cons.insertAfter("k++;", true);
  911. cc.writeFile();
  912. Object obj = make(cc.getName());
  913. assertEquals(6, invoke(obj, "test"));
  914. }
  915. public void testInsertSwitch() throws Exception {
  916. CtClass cc = sloader.get("test3.Switch");
  917. CtMethod m1 = cc.getDeclaredMethod("foo");
  918. String sourceCode = "{"
  919. + "System.out.println(\"Bla!\");"
  920. + "}";
  921. String toInsert =
  922. " try " +
  923. " { " +
  924. sourceCode +
  925. " } " +
  926. " catch(Throwable e) " +
  927. " { " +
  928. " e.printStackTrace(); " +
  929. " } ";
  930. m1.insertBefore(toInsert);
  931. cc.writeFile();
  932. Object obj = make(cc.getName());
  933. assertEquals(0, invoke(obj, "test"));
  934. }
  935. public void testStringBuilder() throws Exception {
  936. CtClass cc = sloader.get("test3.StrBuild");
  937. CtMethod mth = cc.getDeclaredMethod("test");
  938. ExprEditor ed = new ExprEditor() {
  939. public void edit(MethodCall m) throws CannotCompileException {
  940. String block = "$_=$proceed($$);";
  941. m.replace(block);
  942. }
  943. };
  944. mth.instrument(ed);
  945. cc.writeFile();
  946. Object obj = make(cc.getName());
  947. assertEquals('t', invoke(obj, "test"));
  948. }
  949. public void testInheritCons() throws Exception {
  950. CtClass s = sloader.get("test3.InheritCons");
  951. CtClass cc = sloader.makeClass("test3.InheritCons2", s);
  952. cc.writeFile();
  953. Object obj = make(cc.getName());
  954. assertEquals("test3.InheritCons2", obj.getClass().getName());
  955. assertEquals(3, obj.getClass().getDeclaredConstructors().length);
  956. cc = sloader.makeClass("InheritCons3", s);
  957. cc.writeFile();
  958. obj = make(cc.getName());
  959. assertEquals("InheritCons3", obj.getClass().getName());
  960. assertEquals(2, obj.getClass().getDeclaredConstructors().length);
  961. }
  962. public void testAddInterfaceMethod() throws Exception {
  963. CtClass cc = sloader.makeInterface("test3.AddIntfMth");
  964. CtMethod m = CtMethod.make("void foo();", cc);
  965. cc.addMethod(m);
  966. assertTrue(Modifier.isPublic(m.getModifiers()));
  967. CtMethod m2 = CtMethod.make("public void foo2();", cc);
  968. cc.addMethod(m2);
  969. assertTrue(Modifier.isPublic(m2.getModifiers()));
  970. CtMethod m3 = CtMethod.make("public void foo3() {}", cc);
  971. try {
  972. cc.addMethod(m3);
  973. if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_8)
  974. fail();
  975. }
  976. catch (CannotCompileException e) {
  977. // System.out.println(e);
  978. }
  979. }
  980. // JIRA-67
  981. public void test67() throws Exception {
  982. ClassPool pool = new ClassPool(true);
  983. CtClass ctc = pool.makeClass("test3.Clazz67");
  984. StringBuilder sb = new StringBuilder("public void test() {");
  985. for (int i = 0; i < 1000; i++) {
  986. sb.append("for(int i=0; i<10; i++) {}"); // line 1
  987. // sb.append("for(int i=0; i<10; i++) {int j=i;}"); // line 2
  988. }
  989. sb.append("}");
  990. ctc.addMethod(CtNewMethod.make(sb.toString(), ctc));
  991. ctc.debugWriteFile();
  992. ctc.toClass(DefineClassCapability.class).getConstructor().newInstance();
  993. }
  994. // JIRA-83
  995. public void testEmptyCatch() throws Exception {
  996. CtClass cc = sloader.get("test3.EmptyCatch");
  997. CtMethod mth = cc.getDeclaredMethod("test");
  998. mth.instrument(new ExprEditor() {
  999. public void edit(Handler h) throws CannotCompileException {
  1000. try {
  1001. assertEquals(null, h.getType());
  1002. assertTrue(h.isFinally());
  1003. } catch (NotFoundException e) {}
  1004. }
  1005. });
  1006. }
  1007. }