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

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