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

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