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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package javassist;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.TypeVariable;
  4. import javassist.bytecode.ClassFile;
  5. import javassist.bytecode.InnerClassesAttribute;
  6. public class JvstTest5 extends JvstTestRoot {
  7. public JvstTest5(String name) {
  8. super(name);
  9. }
  10. public void testDollarClassInStaticMethod() throws Exception {
  11. CtClass cc = sloader.makeClass("test5.DollarClass");
  12. CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
  13. cc.addMethod(m);
  14. m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
  15. cc.addMethod(m);
  16. cc.writeFile();
  17. Object obj = make(cc.getName());
  18. assertEquals(cc.getName().length(), invoke(obj, "run"));
  19. assertEquals(cc.getName().length(), invoke(obj, "run2"));
  20. }
  21. public void testSuperDefaultMethodCall() throws Exception {
  22. CtClass cc = sloader.get("test5.DefaultMethod");
  23. CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc);
  24. cc.addMethod(m);
  25. m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc);
  26. cc.addMethod(m);
  27. m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc);
  28. cc.addMethod(m);
  29. cc.writeFile();
  30. Object obj = make(cc.getName());
  31. assertEquals(1, invoke(obj, "run"));
  32. assertEquals(10, invoke(obj, "run2"));
  33. assertEquals(10, invoke(obj, "run3"));
  34. }
  35. public void testTypeAnno() throws Exception {
  36. CtClass cc = sloader.get("test5.TypeAnno");
  37. cc.getClassFile().compact();
  38. cc.writeFile();
  39. Object obj = make(cc.getName());
  40. TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
  41. Annotation[] annos = t.getAnnotations();
  42. assertEquals("@test5.TypeAnnoA()", annos[0].toString());
  43. }
  44. public void testJIRA241() throws Exception {
  45. CtClass cc = sloader.get("test5.JIRA241");
  46. CtMethod testMethod = cc.getDeclaredMethod("test");
  47. testMethod.insertAfter("System.out.println(\"inserted!\");");
  48. cc.writeFile();
  49. Object obj = make(cc.getName());
  50. assertEquals(10, invoke(obj, "run"));
  51. }
  52. public void testJIRA246() throws Exception {
  53. CtClass ctClass = sloader.makeClass("test5.JIRA246Test");
  54. ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName()));
  55. String methodBody = "public void test() { defaultMethod(); }";
  56. CtMethod ctMethod = CtMethod.make(methodBody, ctClass);
  57. ctClass.addMethod(ctMethod);
  58. }
  59. public void testJIRA246b() throws Exception {
  60. CtClass ctClass = sloader.get(test5.JIRA246.A.class.getName());
  61. String src = "public void id() { get(); }";
  62. CtMethod make = CtNewMethod.make(src, ctClass);
  63. }
  64. public void testJIRA242() throws Exception {
  65. Boolean ss = new Boolean(2 > 3);
  66. ClassPool cp = ClassPool.getDefault();
  67. CtClass cc = cp.get("test5.JIRA242$Hello");
  68. CtMethod m = cc.getDeclaredMethod("say");
  69. m.insertBefore("{ System.out.println(\"Say Hello...\"); }");
  70. StringBuilder sb = new StringBuilder();
  71. sb.append("BOOL_SERIES = createBooleanSeriesStep();");
  72. //Below code cause the issue
  73. sb.append("BOOL_SERIES.setValue(3>=3);"); //lets comment this and run it will work
  74. // Below code snippets will work
  75. // this cast into exact class and call the same function
  76. sb.append("((test5.JIRA242$BooleanDataSeries)BOOL_SERIES).setValue(3>=3);");
  77. // this code snippet will set exact boolean variable to the function.
  78. sb.append("boolean var = 3>=3;");
  79. sb.append("BOOL_SERIES.setValue(var);");
  80. m.insertBefore(sb.toString());
  81. cc.writeFile();
  82. Object obj = make(cc.getName());
  83. assertEquals(0, invoke(obj, "say"));
  84. }
  85. public void testJIRA249() throws Exception {
  86. CtClass cc = sloader.get("test5.BoolTest");
  87. CtMethod testMethod = cc.getDeclaredMethod("test");
  88. testMethod.insertBefore("i = foo(true & true);");
  89. cc.writeFile();
  90. Object obj = make(cc.getName());
  91. assertEquals(1, invoke(obj, "run"));
  92. }
  93. public void testInnerClassAttributeRemove() throws Exception {
  94. CtClass cc = sloader.get("test5.InnerClassRemove");
  95. ClassFile cf = cc.getClassFile();
  96. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
  97. String second = ica.innerClass(1);
  98. String secondName = ica.innerName(1);
  99. String third = ica.innerClass(2);
  100. String thirdName = ica.innerName(2);
  101. assertEquals(3, ica.remove(3));
  102. assertEquals(2, ica.remove(0));
  103. assertEquals(second, ica.innerClass(0));
  104. assertEquals(secondName, ica.innerName(0));
  105. assertEquals(third, ica.innerClass(1));
  106. assertEquals(thirdName, ica.innerName(1));
  107. assertEquals(1, ica.remove(1));
  108. assertEquals(second, ica.innerClass(0));
  109. assertEquals(secondName, ica.innerName(0));
  110. cc.writeFile();
  111. Object obj = make(cc.getName());
  112. assertEquals(1, invoke(obj, "run"));
  113. }
  114. public void testJIRA248() throws Exception {
  115. CtClass cc = sloader.get("test5.JIRA248");
  116. String methodBody = "public int run() { return foo() + super.foo() + super.bar(); }";
  117. CtMethod ctMethod = CtMethod.make(methodBody, cc);
  118. cc.addMethod(ctMethod);
  119. cc.writeFile();
  120. Object obj = make(cc.getName());
  121. assertEquals(271, invoke(obj, "run"));
  122. }
  123. }