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

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