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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package javassist;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.TypeVariable;
  4. public class JvstTest5 extends JvstTestRoot {
  5. public JvstTest5(String name) {
  6. super(name);
  7. }
  8. public void testDollarClassInStaticMethod() throws Exception {
  9. CtClass cc = sloader.makeClass("test5.DollarClass");
  10. CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
  11. cc.addMethod(m);
  12. m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
  13. cc.addMethod(m);
  14. cc.writeFile();
  15. Object obj = make(cc.getName());
  16. assertEquals(cc.getName().length(), invoke(obj, "run"));
  17. assertEquals(cc.getName().length(), invoke(obj, "run2"));
  18. }
  19. public void testSuperDefaultMethodCall() throws Exception {
  20. CtClass cc = sloader.get("test5.DefaultMethod");
  21. CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc);
  22. cc.addMethod(m);
  23. m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc);
  24. cc.addMethod(m);
  25. m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc);
  26. cc.addMethod(m);
  27. cc.writeFile();
  28. Object obj = make(cc.getName());
  29. assertEquals(1, invoke(obj, "run"));
  30. assertEquals(10, invoke(obj, "run2"));
  31. assertEquals(10, invoke(obj, "run3"));
  32. }
  33. public void testTypeAnno() throws Exception {
  34. CtClass cc = sloader.get("test5.TypeAnno");
  35. cc.getClassFile().compact();
  36. cc.writeFile();
  37. Object obj = make(cc.getName());
  38. TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
  39. Annotation[] annos = t.getAnnotations();
  40. assertEquals("@test5.TypeAnnoA()", annos[0].toString());
  41. }
  42. public void testJIRA241() throws Exception {
  43. CtClass cc = sloader.get("test5.JIRA241");
  44. CtMethod testMethod = cc.getDeclaredMethod("test");
  45. testMethod.insertAfter("System.out.println(\"inserted!\");");
  46. cc.writeFile();
  47. Object obj = make(cc.getName());
  48. assertEquals(10, invoke(obj, "run"));
  49. }
  50. public void testJIRA246() throws Exception {
  51. CtClass ctClass = sloader.makeClass("test5.JIRA246Test");
  52. ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName()));
  53. String methodBody = "public void test() { defaultMethod(); }";
  54. CtMethod ctMethod = CtMethod.make(methodBody, ctClass);
  55. ctClass.addMethod(ctMethod);
  56. }
  57. public void testJIRA246b() throws Exception {
  58. CtClass ctClass = sloader.get(test5.JIRA246.A.class.getName());
  59. String src = "public void id() { get(); }";
  60. CtMethod make = CtNewMethod.make(src, ctClass);
  61. }
  62. public void testJIRA242() throws Exception {
  63. Boolean ss = new Boolean(2 > 3);
  64. ClassPool cp = ClassPool.getDefault();
  65. CtClass cc = cp.get("test5.JIRA242$Hello");
  66. CtMethod m = cc.getDeclaredMethod("say");
  67. m.insertBefore("{ System.out.println(\"Say Hello...\"); }");
  68. StringBuilder sb = new StringBuilder();
  69. sb.append("BOOL_SERIES = createBooleanSeriesStep();");
  70. //Below code cause the issue
  71. sb.append("BOOL_SERIES.setValue(3>=3);"); //lets comment this and run it will work
  72. // Below code snippets will work
  73. // this cast into exact class and call the same function
  74. sb.append("((test5.JIRA242$BooleanDataSeries)BOOL_SERIES).setValue(3>=3);");
  75. // this code snippet will set exact boolean variable to the function.
  76. sb.append("boolean var = 3>=3;");
  77. sb.append("BOOL_SERIES.setValue(var);");
  78. m.insertBefore(sb.toString());
  79. cc.writeFile();
  80. Object obj = make(cc.getName());
  81. assertEquals(0, invoke(obj, "say"));
  82. }
  83. }