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.

JvstTest5.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package javassist;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.TypeVariable;
  4. import javassist.bytecode.AccessFlag;
  5. import javassist.bytecode.AnnotationsAttribute;
  6. import javassist.bytecode.AttributeInfo;
  7. import javassist.bytecode.ClassFile;
  8. import javassist.bytecode.ConstPool;
  9. import javassist.bytecode.InnerClassesAttribute;
  10. import javassist.bytecode.NestHostAttribute;
  11. import javassist.bytecode.NestMembersAttribute;
  12. import javassist.expr.ExprEditor;
  13. import javassist.expr.Handler;
  14. import javassist.expr.MethodCall;
  15. import javassist.expr.NewExpr;
  16. @SuppressWarnings({"rawtypes","unchecked","unused"})
  17. public class JvstTest5 extends JvstTestRoot {
  18. public JvstTest5(String name) {
  19. super(name);
  20. }
  21. public void testDollarClassInStaticMethod() throws Exception {
  22. CtClass cc = sloader.makeClass("test5.DollarClass");
  23. CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
  24. cc.addMethod(m);
  25. m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
  26. cc.addMethod(m);
  27. cc.writeFile();
  28. Object obj = make(cc.getName());
  29. assertEquals(cc.getName().length(), invoke(obj, "run"));
  30. assertEquals(cc.getName().length(), invoke(obj, "run2"));
  31. }
  32. public void testSuperDefaultMethodCall() throws Exception {
  33. CtClass cc = sloader.get("test5.DefaultMethod");
  34. CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc);
  35. cc.addMethod(m);
  36. m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc);
  37. cc.addMethod(m);
  38. m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc);
  39. cc.addMethod(m);
  40. cc.writeFile();
  41. Object obj = make(cc.getName());
  42. assertEquals(1, invoke(obj, "run"));
  43. assertEquals(10, invoke(obj, "run2"));
  44. assertEquals(10, invoke(obj, "run3"));
  45. }
  46. public void testTypeAnno() throws Exception {
  47. CtClass cc = sloader.get("test5.TypeAnno");
  48. cc.getClassFile().compact();
  49. cc.writeFile();
  50. Object obj = make(cc.getName());
  51. TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
  52. Annotation[] annos = t.getAnnotations();
  53. assertEquals("@test5.TypeAnnoA()", annos[0].toString());
  54. }
  55. public void testJIRA241() throws Exception {
  56. CtClass cc = sloader.get("test5.JIRA241");
  57. CtMethod testMethod = cc.getDeclaredMethod("test");
  58. testMethod.insertAfter("System.out.println(\"inserted!\");");
  59. cc.writeFile();
  60. Object obj = make(cc.getName());
  61. assertEquals(10, invoke(obj, "run"));
  62. }
  63. public void testJIRA246() throws Exception {
  64. CtClass ctClass = sloader.makeClass("test5.JIRA246Test");
  65. ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName()));
  66. String methodBody = "public void test() { defaultMethod(); }";
  67. CtMethod ctMethod = CtMethod.make(methodBody, ctClass);
  68. ctClass.addMethod(ctMethod);
  69. }
  70. public void testJIRA246b() throws Exception {
  71. CtClass ctClass = sloader.get(test5.JIRA246.A.class.getName());
  72. String src = "public void id() { get(); }";
  73. CtMethod make = CtNewMethod.make(src, ctClass);
  74. }
  75. public void testJIRA242() throws Exception {
  76. Boolean ss = Boolean.valueOf(2 > 3);
  77. ClassPool cp = ClassPool.getDefault();
  78. CtClass cc = cp.get("test5.JIRA242$Hello");
  79. CtMethod m = cc.getDeclaredMethod("say");
  80. m.insertBefore("{ System.out.println(\"Say Hello...\"); }");
  81. StringBuilder sb = new StringBuilder();
  82. sb.append("BOOL_SERIES = createBooleanSeriesStep();");
  83. //Below code cause the issue
  84. sb.append("BOOL_SERIES.setValue(3>=3);"); //lets comment this and run it will work
  85. // Below code snippets will work
  86. // this cast into exact class and call the same function
  87. sb.append("((test5.JIRA242$BooleanDataSeries)BOOL_SERIES).setValue(3>=3);");
  88. // this code snippet will set exact boolean variable to the function.
  89. sb.append("boolean var = 3>=3;");
  90. sb.append("BOOL_SERIES.setValue(var);");
  91. m.insertBefore(sb.toString());
  92. cc.writeFile();
  93. Object obj = make(cc.getName());
  94. assertEquals(0, invoke(obj, "say"));
  95. }
  96. public void testJIRA249() throws Exception {
  97. CtClass cc = sloader.get("test5.BoolTest");
  98. CtMethod testMethod = cc.getDeclaredMethod("test");
  99. testMethod.insertBefore("i = foo(true & true);");
  100. cc.writeFile();
  101. Object obj = make(cc.getName());
  102. assertEquals(1, invoke(obj, "run"));
  103. }
  104. public void testInnerClassAttributeRemove() throws Exception {
  105. CtClass cc = sloader.get("test5.InnerClassRemove");
  106. ClassFile cf = cc.getClassFile();
  107. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
  108. String second = ica.innerClass(1);
  109. String secondName = ica.innerName(1);
  110. String third = ica.innerClass(2);
  111. String thirdName = ica.innerName(2);
  112. assertEquals(3, ica.remove(3));
  113. assertEquals(2, ica.remove(0));
  114. assertEquals(second, ica.innerClass(0));
  115. assertEquals(secondName, ica.innerName(0));
  116. assertEquals(third, ica.innerClass(1));
  117. assertEquals(thirdName, ica.innerName(1));
  118. assertEquals(1, ica.remove(1));
  119. assertEquals(second, ica.innerClass(0));
  120. assertEquals(secondName, ica.innerName(0));
  121. cc.writeFile();
  122. Object obj = make(cc.getName());
  123. assertEquals(1, invoke(obj, "run"));
  124. }
  125. public void testJIRA248() throws Exception {
  126. CtClass cc = sloader.get("test5.JIRA248");
  127. String methodBody = "public int run() { return foo() + super.foo() + super.bar() + test5.JIRA248Intf2.super.baz(); }";
  128. CtMethod ctMethod = CtMethod.make(methodBody, cc);
  129. cc.addMethod(ctMethod);
  130. cc.writeFile();
  131. Object obj = make(cc.getName());
  132. assertEquals(40271, invoke(obj, "run"));
  133. }
  134. public void testInvalidCastWithDollar() throws Exception {
  135. String code = "{ new test5.JavassistInvalidCastTest().inspectReturn((Object) ($w) $_); } ";
  136. CtClass c = sloader.get("test5.InvalidCastDollar");
  137. for (CtMethod method : c.getDeclaredMethods())
  138. method.insertAfter(code);
  139. }
  140. public void testJIRA256() throws Exception {
  141. // CtClass ec = sloader.get("test5.Entity");
  142. CtClass cc = sloader.makeClass("test5.JIRA256");
  143. ClassFile ccFile = cc.getClassFile();
  144. ConstPool constpool = ccFile.getConstPool();
  145. AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
  146. javassist.bytecode.annotation.Annotation entityAnno
  147. = new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
  148. // = new javassist.bytecode.annotation.Annotation(constpool, ec);
  149. entityAnno.addMemberValue("value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
  150. attr.addAnnotation(entityAnno);
  151. ccFile.addAttribute(attr);
  152. cc.writeFile();
  153. Object o = make(cc.getName());
  154. assertTrue(o.getClass().getName().equals("test5.JIRA256"));
  155. java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
  156. assertEquals(1, annotations.length);
  157. }
  158. public void testJIRA250() throws Exception {
  159. CtClass cc = sloader.makeClass("test5.JIRA250", sloader.get("test5.JIRA250Super"));
  160. cc.addMethod(CtNewMethod.make(
  161. " public test5.JIRA250Bar getBar() {" +
  162. " return super.getBar();\n" +
  163. " }\n", cc));
  164. cc.addMethod(CtNewMethod.make("public int run() { getBar(); return 1; }", cc));
  165. cc.writeFile();
  166. Object obj = make(cc.getName());
  167. assertEquals(1, invoke(obj, "run"));
  168. }
  169. public void testProceedToDefaultMethod() throws Exception {
  170. CtClass cc = ClassPool.getDefault().get("test5.ProceedDefault");
  171. CtMethod mth = cc.getDeclaredMethod("bar");
  172. mth.instrument(new ExprEditor() {
  173. public void edit(MethodCall c) throws CannotCompileException {
  174. c.replace("$_ = $proceed($$) + 10000;");
  175. }
  176. });
  177. cc.writeFile();
  178. Object obj = make(cc.getName());
  179. assertEquals(21713, invoke(obj, "run"));
  180. }
  181. public void testBadClass() throws Exception {
  182. CtClass badClass = ClassPool.getDefault().makeClass("badClass");
  183. String src = String.join(System.getProperty("line.separator"),
  184. "public void eval () {",
  185. " if (true) {",
  186. " double t=0;",
  187. " } else {",
  188. " double t=0;",
  189. " }",
  190. " for (int i=0; i < 2; i++) {",
  191. " int a=0;",
  192. " int b=0;",
  193. " int c=0;",
  194. " int d=0;",
  195. " if (true) {",
  196. " int e = 0;",
  197. " }",
  198. " }",
  199. "}");
  200. System.out.println(src);
  201. badClass.addMethod(CtMethod.make(src, badClass));
  202. Class clazzz = badClass.toClass(Class.forName("DefineClassCapability"));
  203. Object obj = clazzz.getConstructor().newInstance(); // <-- falls here
  204. }
  205. public void test83StackmapWithArrayType() throws Exception {
  206. final CtClass ctClass = sloader.get("test5.StackmapWithArray83");
  207. final CtMethod method = ctClass.getDeclaredMethod("bytecodeVerifyError");
  208. method.addLocalVariable("test_localVariable", CtClass.intType);
  209. method.insertBefore("{ test_localVariable = 1; }");
  210. final CtMethod method2 = ctClass.getDeclaredMethod("bytecodeVerifyError2");
  211. method2.addLocalVariable("test_localVariable", CtClass.intType);
  212. method2.insertBefore("{ test_localVariable = 1; }");
  213. ctClass.writeFile();
  214. Object obj = make(ctClass.getName());
  215. assertEquals(1, invoke(obj, "run"));
  216. }
  217. public void testLoaderClassPath() throws Exception {
  218. ClassPool cp = new ClassPool();
  219. cp.appendClassPath(new LoaderClassPath(new Loader()));
  220. assertNotNull(cp.get(Object.class.getName()));
  221. assertNotNull(cp.get(this.getClass().getName()));
  222. }
  223. public void testAddDefaultMethod() throws Exception {
  224. CtClass cc = sloader.makeInterface("test5.AddDefaultMethod");
  225. cc.addMethod(CtNewMethod.make("static int foo() { return 1; }", cc));
  226. cc.addMethod(CtNewMethod.make("public static int foo1() { return 1; }", cc));
  227. cc.addMethod(CtNewMethod.make("public int foo2() { return 1; }", cc));
  228. cc.addMethod(CtNewMethod.make("int foo3() { return 1; }", cc));
  229. try {
  230. cc.addMethod(CtNewMethod.make("private int foo4() { return 1; }", cc));
  231. fail();
  232. } catch (CannotCompileException e) {}
  233. try {
  234. cc.addMethod(CtNewMethod.make("private static int foo5() { return 1; }", cc));
  235. fail();
  236. } catch (CannotCompileException e) {}
  237. }
  238. public void testRemoveAnnotatino() throws Exception {
  239. CtClass cc = sloader.get("test5.RemoveAnnotation");
  240. AnnotationsAttribute aa
  241. = (AnnotationsAttribute)cc.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
  242. assertTrue(aa.removeAnnotation("test5.RemoveAnno1"));
  243. AttributeInfo ai = cc.getClassFile().removeAttribute(AnnotationsAttribute.invisibleTag);
  244. assertEquals(ai.getName(), AnnotationsAttribute.invisibleTag);
  245. CtMethod foo = cc.getDeclaredMethod("foo");
  246. AnnotationsAttribute aa2 = (AnnotationsAttribute)foo.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
  247. assertTrue(aa2.removeAnnotation("test5.RemoveAnno1"));
  248. CtMethod bar = cc.getDeclaredMethod("bar");
  249. AnnotationsAttribute aa3 = (AnnotationsAttribute)bar.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
  250. assertFalse(aa3.removeAnnotation("test5.RemoveAnno1"));
  251. assertTrue(aa3.removeAnnotation("test5.RemoveAnno2"));
  252. AttributeInfo ai2 = bar.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  253. assertEquals(ai2.getName(), AnnotationsAttribute.invisibleTag);
  254. CtMethod run = cc.getDeclaredMethod("run");
  255. AttributeInfo ai3 = run.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  256. assertNull(ai3);
  257. CtField baz = cc.getDeclaredField("baz");
  258. AttributeInfo ai4 = baz.getFieldInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  259. assertEquals(ai4.getName(), AnnotationsAttribute.invisibleTag);
  260. cc.writeFile();
  261. Object obj = make(cc.getName());
  262. assertEquals(3, invoke(obj, "run"));
  263. }
  264. public void testInnerClassModifiers() throws Exception {
  265. CtClass cc = sloader.get("test5.InnerModifier$NonStatic");
  266. try {
  267. cc.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
  268. fail();
  269. }
  270. catch (RuntimeException e) {
  271. if (!e.getMessage().startsWith("cannot change "))
  272. fail();
  273. }
  274. cc.setModifiers(Modifier.PUBLIC);
  275. cc.writeFile();
  276. assertEquals(Modifier.PUBLIC, cc.getModifiers());
  277. InnerClassesAttribute ica = getInnerClassAttr(cc);
  278. int i = ica.find("test5.InnerModifier$NonStatic");
  279. assertTrue(i >= 0);
  280. assertEquals(Modifier.PUBLIC, ica.accessFlags(i));
  281. CtClass cc2 = sloader.get("test5.InnerModifier$Static");
  282. InnerClassesAttribute ica3 = getInnerClassAttr(cc2);
  283. int i3 = ica3.find("test5.InnerModifier$Static");
  284. assertTrue(i3 >= 0);
  285. assertEquals(AccessFlag.STATIC, ica3.accessFlags(i3));
  286. cc2.setModifiers(Modifier.PROTECTED | Modifier.STATIC);
  287. cc2.setModifiers(Modifier.PUBLIC);
  288. cc2.writeFile();
  289. assertEquals(Modifier.PUBLIC | Modifier.STATIC, cc2.getModifiers());
  290. InnerClassesAttribute ica2 = getInnerClassAttr(cc2);
  291. int i2 = ica2.find("test5.InnerModifier$Static");
  292. assertTrue(i2 >= 0);
  293. assertEquals(AccessFlag.PUBLIC | AccessFlag.STATIC, ica2.accessFlags(i2));
  294. CtClass cc3 = cc.getDeclaringClass();
  295. assertTrue(cc3.isModified());
  296. cc3.writeFile();
  297. InnerClassesAttribute ica4 = getInnerClassAttr(cc3);
  298. int i4 = ica4.find("test5.InnerModifier$Static");
  299. assertTrue(i4 >= 0);
  300. assertEquals(AccessFlag.PUBLIC | AccessFlag.STATIC, ica4.accessFlags(i4));
  301. int i5 = ica4.find("test5.InnerModifier$NonStatic");
  302. assertTrue(i5 >= 0);
  303. assertEquals(Modifier.PUBLIC, ica4.accessFlags(i5));
  304. }
  305. public void testInnerClassModifiers2() throws Exception {
  306. CtClass cc = sloader.get("test5.InnerModifier2$Protected");
  307. Class<?> ccc = Class.forName("test5.InnerModifier2$Protected");
  308. assertEquals(cc.getModifiers(), ccc.getModifiers());
  309. assertTrue(Modifier.isProtected(cc.getModifiers()));
  310. cc = sloader.get("test5.InnerModifier2$Public");
  311. ccc = Class.forName("test5.InnerModifier2$Public");
  312. assertEquals(cc.getModifiers(), ccc.getModifiers());
  313. assertTrue(Modifier.isPublic(cc.getModifiers()));
  314. cc = sloader.get("test5.InnerModifier2$Private");
  315. ccc = Class.forName("test5.InnerModifier2$Private");
  316. assertEquals(cc.getModifiers(), ccc.getModifiers());
  317. assertTrue(Modifier.isPrivate(cc.getModifiers()));
  318. cc = sloader.get("test5.InnerModifier2$Package");
  319. ccc = Class.forName("test5.InnerModifier2$Package");
  320. assertEquals(cc.getModifiers(), ccc.getModifiers());
  321. assertTrue(Modifier.isPackage(cc.getModifiers()));
  322. cc = sloader.get("test5.InnerModifier2$ProtectedStatic");
  323. ccc = Class.forName("test5.InnerModifier2$ProtectedStatic");
  324. assertEquals(cc.getModifiers(), ccc.getModifiers());
  325. assertTrue(Modifier.isProtected(cc.getModifiers()));
  326. assertTrue(Modifier.isStatic(cc.getModifiers()));
  327. cc = sloader.get("test5.InnerModifier2$PublicStatic");
  328. ccc = Class.forName("test5.InnerModifier2$PublicStatic");
  329. assertEquals(cc.getModifiers(), ccc.getModifiers());
  330. assertTrue(Modifier.isPublic(cc.getModifiers()));
  331. assertTrue(Modifier.isStatic(cc.getModifiers()));
  332. cc = sloader.get("test5.InnerModifier2$PrivateStatic");
  333. ccc = Class.forName("test5.InnerModifier2$PrivateStatic");
  334. assertEquals(cc.getModifiers(), ccc.getModifiers());
  335. assertTrue(Modifier.isPrivate(cc.getModifiers()));
  336. assertTrue(Modifier.isStatic(cc.getModifiers()));
  337. cc = sloader.get("test5.InnerModifier2$PackageStatic");
  338. ccc = Class.forName("test5.InnerModifier2$PackageStatic");
  339. assertEquals(cc.getModifiers(), ccc.getModifiers());
  340. assertTrue(Modifier.isPackage(cc.getModifiers()));
  341. assertTrue(Modifier.isStatic(cc.getModifiers()));
  342. }
  343. private InnerClassesAttribute getInnerClassAttr(CtClass cc) {
  344. return (InnerClassesAttribute)cc.getClassFile2().getAttribute(InnerClassesAttribute.tag);
  345. }
  346. public void testVarArgsModifier() throws Exception {
  347. CtClass cc = sloader.get("test5.VarArgsMethod");
  348. assertTrue(Modifier.isVarArgs(cc.getDeclaredMethod("foo").getModifiers()));
  349. assertFalse(Modifier.isVarArgs(cc.getDeclaredMethod("bar").getModifiers()));
  350. }
  351. public void testIssue155() throws Exception {
  352. CtClass cc = sloader.get("test5.Issue155");
  353. CtMethod testMethod = cc.getDeclaredMethod("foo");
  354. testMethod.instrument(
  355. new ExprEditor() {
  356. public void edit(Handler m)
  357. throws CannotCompileException {
  358. m.insertBefore("throw $1;");
  359. }
  360. });
  361. cc.writeFile();
  362. Object obj = make(cc.getName());
  363. assertEquals(1, invoke(obj, "test"));
  364. }
  365. public void testNestHostAttribute() throws Exception {
  366. CtClass cc = sloader.get("test5.NestHost$Foo");
  367. ClassFile cf = cc.getClassFile();
  368. NestHostAttribute attr = (NestHostAttribute)cf.getAttribute(NestHostAttribute.tag);
  369. assertEquals(test5.NestHost.class.getName(),
  370. cf.getConstPool().getClassInfo(attr.hostClassIndex()));
  371. }
  372. public void testNestMembersAttribute() throws Exception {
  373. CtClass cc = sloader.get("test5.NestHost");
  374. ClassFile cf = cc.getClassFile();
  375. NestMembersAttribute attr = (NestMembersAttribute)cf.getAttribute(NestMembersAttribute.tag);
  376. assertEquals(2, attr.numberOfClasses());
  377. String[] names = new String[2];
  378. for (int i = 0; i < 2; i++)
  379. names[i] = cf.getConstPool().getClassInfo(attr.memberClass(i));
  380. assertFalse(names[0].equals(names[1]));
  381. assertTrue(names[0].equals("test5.NestHost$Foo") || names[0].equals("test5.NestHost$Bar"));
  382. assertTrue(names[1].equals("test5.NestHost$Foo") || names[1].equals("test5.NestHost$Bar"));
  383. }
  384. public void testNestMembersAttributeCopy() throws Exception {
  385. CtClass cc = sloader.get("test5.NestHost2");
  386. cc.getClassFile().compact();
  387. cc.writeFile();
  388. make(cc.getName());
  389. }
  390. public void testNestHostAttributeCopy() throws Exception {
  391. CtClass cc = sloader.get("test5.NestHost2$Foo");
  392. cc.getClassFile().compact();
  393. cc.toClass(test5.DefineClassCapability.class);
  394. }
  395. public void testSwitchCaseWithStringConstant() throws Exception {
  396. CtClass cc = sloader.get("test5.SwitchCase");
  397. cc.addMethod(CtNewMethod.make(
  398. "public int run() {" +
  399. " String s = \"foobar\";\n" +
  400. " switch (s) {\n" +
  401. " case STR1: return 1;\n" +
  402. " case \"foobar\": return 2;\n" +
  403. " default: return 3; }\n" +
  404. "}\n", cc));
  405. cc.writeFile();
  406. Object obj = make(cc.getName());
  407. assertEquals(2, invoke(obj, "run"));
  408. }
  409. public void testNestPrivateConstructor() throws Exception {
  410. CtClass cc = sloader.get("test5.NestHost3$Builder");
  411. cc.instrument(new ExprEditor() {
  412. public void edit(NewExpr e) throws CannotCompileException {
  413. String code = "$_ = $proceed($$);";
  414. e.replace(code);
  415. }
  416. });
  417. cc.writeFile();
  418. try {
  419. Class<?> nestHost3Class = cloader.loadClass("test5.NestHost3");
  420. Object builder = nestHost3Class.getDeclaredMethod("builder").invoke(nestHost3Class);
  421. Class<?> nestHost3BuilderClass = cloader.loadClass("test5.NestHost3$Builder");
  422. nestHost3BuilderClass.getDeclaredMethod("build").invoke(builder);
  423. } catch (Exception ex) {
  424. ex.printStackTrace();
  425. fail("it should be able to access the private constructor of the nest host");
  426. }
  427. }
  428. public void testSwitchCaseWithStringConstant2() throws Exception {
  429. CtClass cc = sloader.makeClass("test5.SwitchCase2");
  430. cc.addMethod(CtNewMethod.make(
  431. "public int run() {" +
  432. " String s = \"foo\";\n" +
  433. " switch (s) {\n" +
  434. " case test5.SwitchCase.STR1: return 1;\n" +
  435. " case \"foobar\": return 2;\n" +
  436. " }\n" +
  437. " return 3;\n" +
  438. "}\n", cc));
  439. cc.writeFile();
  440. Object obj = make(cc.getName());
  441. assertEquals(1, invoke(obj, "run"));
  442. }
  443. // Issue #241
  444. public void testInsertBeforeAndDollarR() throws Exception {
  445. CtClass cc = sloader.get(test5.InsertBeforeDollarR.class.getName());
  446. CtMethod m = cc.getDeclaredMethod("foo");
  447. m.insertBefore("{ if ($1 == 1) return ($r)$2; }");
  448. try {
  449. m.insertBefore("{ $_ = \"bar\"; }");
  450. assertTrue(false);
  451. } catch (CannotCompileException e) {}
  452. cc.writeFile();
  453. Object obj = make(cc.getName());
  454. assertEquals(1, invoke(obj, "run"));
  455. }
  456. }