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 26KB

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