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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package javassist;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.TypeVariable;
  4. import javassist.bytecode.AnnotationsAttribute;
  5. import javassist.bytecode.AttributeInfo;
  6. import javassist.bytecode.ClassFile;
  7. import javassist.bytecode.ConstPool;
  8. import javassist.bytecode.InnerClassesAttribute;
  9. import javassist.expr.ExprEditor;
  10. import javassist.expr.MethodCall;
  11. public class JvstTest5 extends JvstTestRoot {
  12. public JvstTest5(String name) {
  13. super(name);
  14. }
  15. public void testDollarClassInStaticMethod() throws Exception {
  16. CtClass cc = sloader.makeClass("test5.DollarClass");
  17. CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
  18. cc.addMethod(m);
  19. m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
  20. cc.addMethod(m);
  21. cc.writeFile();
  22. Object obj = make(cc.getName());
  23. assertEquals(cc.getName().length(), invoke(obj, "run"));
  24. assertEquals(cc.getName().length(), invoke(obj, "run2"));
  25. }
  26. public void testSuperDefaultMethodCall() throws Exception {
  27. CtClass cc = sloader.get("test5.DefaultMethod");
  28. CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc);
  29. cc.addMethod(m);
  30. m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc);
  31. cc.addMethod(m);
  32. m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc);
  33. cc.addMethod(m);
  34. cc.writeFile();
  35. Object obj = make(cc.getName());
  36. assertEquals(1, invoke(obj, "run"));
  37. assertEquals(10, invoke(obj, "run2"));
  38. assertEquals(10, invoke(obj, "run3"));
  39. }
  40. public void testTypeAnno() throws Exception {
  41. CtClass cc = sloader.get("test5.TypeAnno");
  42. cc.getClassFile().compact();
  43. cc.writeFile();
  44. Object obj = make(cc.getName());
  45. TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
  46. Annotation[] annos = t.getAnnotations();
  47. assertEquals("@test5.TypeAnnoA()", annos[0].toString());
  48. }
  49. public void testJIRA241() throws Exception {
  50. CtClass cc = sloader.get("test5.JIRA241");
  51. CtMethod testMethod = cc.getDeclaredMethod("test");
  52. testMethod.insertAfter("System.out.println(\"inserted!\");");
  53. cc.writeFile();
  54. Object obj = make(cc.getName());
  55. assertEquals(10, invoke(obj, "run"));
  56. }
  57. public void testJIRA246() throws Exception {
  58. CtClass ctClass = sloader.makeClass("test5.JIRA246Test");
  59. ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName()));
  60. String methodBody = "public void test() { defaultMethod(); }";
  61. CtMethod ctMethod = CtMethod.make(methodBody, ctClass);
  62. ctClass.addMethod(ctMethod);
  63. }
  64. public void testJIRA246b() throws Exception {
  65. CtClass ctClass = sloader.get(test5.JIRA246.A.class.getName());
  66. String src = "public void id() { get(); }";
  67. CtMethod make = CtNewMethod.make(src, ctClass);
  68. }
  69. public void testJIRA242() throws Exception {
  70. Boolean ss = Boolean.valueOf(2 > 3);
  71. ClassPool cp = ClassPool.getDefault();
  72. CtClass cc = cp.get("test5.JIRA242$Hello");
  73. CtMethod m = cc.getDeclaredMethod("say");
  74. m.insertBefore("{ System.out.println(\"Say Hello...\"); }");
  75. StringBuilder sb = new StringBuilder();
  76. sb.append("BOOL_SERIES = createBooleanSeriesStep();");
  77. //Below code cause the issue
  78. sb.append("BOOL_SERIES.setValue(3>=3);"); //lets comment this and run it will work
  79. // Below code snippets will work
  80. // this cast into exact class and call the same function
  81. sb.append("((test5.JIRA242$BooleanDataSeries)BOOL_SERIES).setValue(3>=3);");
  82. // this code snippet will set exact boolean variable to the function.
  83. sb.append("boolean var = 3>=3;");
  84. sb.append("BOOL_SERIES.setValue(var);");
  85. m.insertBefore(sb.toString());
  86. cc.writeFile();
  87. Object obj = make(cc.getName());
  88. assertEquals(0, invoke(obj, "say"));
  89. }
  90. public void testJIRA249() throws Exception {
  91. CtClass cc = sloader.get("test5.BoolTest");
  92. CtMethod testMethod = cc.getDeclaredMethod("test");
  93. testMethod.insertBefore("i = foo(true & true);");
  94. cc.writeFile();
  95. Object obj = make(cc.getName());
  96. assertEquals(1, invoke(obj, "run"));
  97. }
  98. public void testInnerClassAttributeRemove() throws Exception {
  99. CtClass cc = sloader.get("test5.InnerClassRemove");
  100. ClassFile cf = cc.getClassFile();
  101. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
  102. String second = ica.innerClass(1);
  103. String secondName = ica.innerName(1);
  104. String third = ica.innerClass(2);
  105. String thirdName = ica.innerName(2);
  106. assertEquals(3, ica.remove(3));
  107. assertEquals(2, ica.remove(0));
  108. assertEquals(second, ica.innerClass(0));
  109. assertEquals(secondName, ica.innerName(0));
  110. assertEquals(third, ica.innerClass(1));
  111. assertEquals(thirdName, ica.innerName(1));
  112. assertEquals(1, ica.remove(1));
  113. assertEquals(second, ica.innerClass(0));
  114. assertEquals(secondName, ica.innerName(0));
  115. cc.writeFile();
  116. Object obj = make(cc.getName());
  117. assertEquals(1, invoke(obj, "run"));
  118. }
  119. public void testJIRA248() throws Exception {
  120. CtClass cc = sloader.get("test5.JIRA248");
  121. String methodBody = "public int run() { return foo() + super.foo() + super.bar() + test5.JIRA248Intf2.super.baz(); }";
  122. CtMethod ctMethod = CtMethod.make(methodBody, cc);
  123. cc.addMethod(ctMethod);
  124. cc.writeFile();
  125. Object obj = make(cc.getName());
  126. assertEquals(40271, invoke(obj, "run"));
  127. }
  128. public void testInvalidCastWithDollar() throws Exception {
  129. String code = "{ new test5.JavassistInvalidCastTest().inspectReturn((Object) ($w) $_); } ";
  130. CtClass c = sloader.get("test5.InvalidCastDollar");
  131. for (CtMethod method : c.getDeclaredMethods())
  132. method.insertAfter(code);
  133. }
  134. public void testJIRA256() throws Exception {
  135. // CtClass ec = sloader.get("test5.Entity");
  136. CtClass cc = sloader.makeClass("test5.JIRA256");
  137. ClassFile ccFile = cc.getClassFile();
  138. ConstPool constpool = ccFile.getConstPool();
  139. AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
  140. javassist.bytecode.annotation.Annotation entityAnno
  141. = new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
  142. // = new javassist.bytecode.annotation.Annotation(constpool, ec);
  143. entityAnno.addMemberValue("value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
  144. attr.addAnnotation(entityAnno);
  145. ccFile.addAttribute(attr);
  146. cc.writeFile();
  147. Object o = make(cc.getName());
  148. assertTrue(o.getClass().getName().equals("test5.JIRA256"));
  149. java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
  150. assertEquals(1, annotations.length);
  151. }
  152. public void testJIRA250() throws Exception {
  153. CtClass cc = sloader.makeClass("test5.JIRA250", sloader.get("test5.JIRA250Super"));
  154. cc.addMethod(CtNewMethod.make(
  155. " public test5.JIRA250Bar getBar() {" +
  156. " return super.getBar();\n" +
  157. " }\n", cc));
  158. cc.addMethod(CtNewMethod.make("public int run() { getBar(); return 1; }", cc));
  159. cc.writeFile();
  160. Object obj = make(cc.getName());
  161. assertEquals(1, invoke(obj, "run"));
  162. }
  163. public void testProceedToDefaultMethod() throws Exception {
  164. CtClass cc = ClassPool.getDefault().get("test5.ProceedDefault");
  165. CtMethod mth = cc.getDeclaredMethod("bar");
  166. mth.instrument(new ExprEditor() {
  167. public void edit(MethodCall c) throws CannotCompileException {
  168. c.replace("$_ = $proceed($$) + 10000;");
  169. }
  170. });
  171. cc.writeFile();
  172. Object obj = make(cc.getName());
  173. assertEquals(21713, invoke(obj, "run"));
  174. }
  175. public void testBadClass() throws Exception {
  176. CtClass badClass = ClassPool.getDefault().makeClass("badClass");
  177. String src = String.join(System.getProperty("line.separator"),
  178. "public void eval () {",
  179. " if (true) {",
  180. " double t=0;",
  181. " } else {",
  182. " double t=0;",
  183. " }",
  184. " for (int i=0; i < 2; i++) {",
  185. " int a=0;",
  186. " int b=0;",
  187. " int c=0;",
  188. " int d=0;",
  189. " if (true) {",
  190. " int e = 0;",
  191. " }",
  192. " }",
  193. "}");
  194. System.out.println(src);
  195. badClass.addMethod(CtMethod.make(src, badClass));
  196. Class clazzz = badClass.toClass();
  197. Object obj = clazzz.getConstructor().newInstance(); // <-- falls here
  198. }
  199. public void test83StackmapWithArrayType() throws Exception {
  200. final CtClass ctClass = sloader.get("test5.StackmapWithArray83");
  201. final CtMethod method = ctClass.getDeclaredMethod("bytecodeVerifyError");
  202. method.addLocalVariable("test_localVariable", CtClass.intType);
  203. method.insertBefore("{ test_localVariable = 1; }");
  204. final CtMethod method2 = ctClass.getDeclaredMethod("bytecodeVerifyError2");
  205. method2.addLocalVariable("test_localVariable", CtClass.intType);
  206. method2.insertBefore("{ test_localVariable = 1; }");
  207. ctClass.writeFile();
  208. Object obj = make(ctClass.getName());
  209. assertEquals(1, invoke(obj, "run"));
  210. }
  211. public void testLoaderClassPath() throws Exception {
  212. ClassPool cp = new ClassPool();
  213. cp.appendClassPath(new LoaderClassPath(new Loader()));
  214. assertNotNull(cp.get(Object.class.getName()));
  215. assertNotNull(cp.get(this.getClass().getName()));
  216. public void testAddDefaultMethod() throws Exception {
  217. CtClass cc = sloader.makeInterface("test5.AddDefaultMethod");
  218. cc.addMethod(CtNewMethod.make("static int foo() { return 1; }", cc));
  219. cc.addMethod(CtNewMethod.make("public static int foo1() { return 1; }", cc));
  220. cc.addMethod(CtNewMethod.make("public int foo2() { return 1; }", cc));
  221. cc.addMethod(CtNewMethod.make("int foo3() { return 1; }", cc));
  222. try {
  223. cc.addMethod(CtNewMethod.make("private int foo4() { return 1; }", cc));
  224. fail();
  225. } catch (CannotCompileException e) {}
  226. try {
  227. cc.addMethod(CtNewMethod.make("private static int foo5() { return 1; }", cc));
  228. fail();
  229. } catch (CannotCompileException e) {}
  230. }
  231. public void testRemoveAnnotatino() throws Exception {
  232. CtClass cc = sloader.get("test5.RemoveAnnotation");
  233. AnnotationsAttribute aa
  234. = (AnnotationsAttribute)cc.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
  235. assertTrue(aa.removeAnnotation("test5.RemoveAnno1"));
  236. AttributeInfo ai = cc.getClassFile().removeAttribute(AnnotationsAttribute.invisibleTag);
  237. assertEquals(ai.getName(), AnnotationsAttribute.invisibleTag);
  238. CtMethod foo = cc.getDeclaredMethod("foo");
  239. AnnotationsAttribute aa2 = (AnnotationsAttribute)foo.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
  240. assertTrue(aa2.removeAnnotation("test5.RemoveAnno1"));
  241. CtMethod bar = cc.getDeclaredMethod("bar");
  242. AnnotationsAttribute aa3 = (AnnotationsAttribute)bar.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
  243. assertFalse(aa3.removeAnnotation("test5.RemoveAnno1"));
  244. assertTrue(aa3.removeAnnotation("test5.RemoveAnno2"));
  245. AttributeInfo ai2 = bar.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  246. assertEquals(ai2.getName(), AnnotationsAttribute.invisibleTag);
  247. CtMethod run = cc.getDeclaredMethod("run");
  248. AttributeInfo ai3 = run.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  249. assertNull(ai3);
  250. CtField baz = cc.getDeclaredField("baz");
  251. AttributeInfo ai4 = baz.getFieldInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
  252. assertEquals(ai4.getName(), AnnotationsAttribute.invisibleTag);
  253. cc.writeFile();
  254. Object obj = make(cc.getName());
  255. assertEquals(3, invoke(obj, "run"));
  256. }
  257. }