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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. public class JvstTest5 extends JvstTestRoot {
  9. public JvstTest5(String name) {
  10. super(name);
  11. }
  12. public void testDollarClassInStaticMethod() throws Exception {
  13. CtClass cc = sloader.makeClass("test5.DollarClass");
  14. CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
  15. cc.addMethod(m);
  16. m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
  17. cc.addMethod(m);
  18. cc.writeFile();
  19. Object obj = make(cc.getName());
  20. assertEquals(cc.getName().length(), invoke(obj, "run"));
  21. assertEquals(cc.getName().length(), invoke(obj, "run2"));
  22. }
  23. public void testSuperDefaultMethodCall() throws Exception {
  24. CtClass cc = sloader.get("test5.DefaultMethod");
  25. CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc);
  26. cc.addMethod(m);
  27. m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc);
  28. cc.addMethod(m);
  29. m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc);
  30. cc.addMethod(m);
  31. cc.writeFile();
  32. Object obj = make(cc.getName());
  33. assertEquals(1, invoke(obj, "run"));
  34. assertEquals(10, invoke(obj, "run2"));
  35. assertEquals(10, invoke(obj, "run3"));
  36. }
  37. public void testTypeAnno() throws Exception {
  38. CtClass cc = sloader.get("test5.TypeAnno");
  39. cc.getClassFile().compact();
  40. cc.writeFile();
  41. Object obj = make(cc.getName());
  42. TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
  43. Annotation[] annos = t.getAnnotations();
  44. assertEquals("@test5.TypeAnnoA()", annos[0].toString());
  45. }
  46. public void testJIRA241() throws Exception {
  47. CtClass cc = sloader.get("test5.JIRA241");
  48. CtMethod testMethod = cc.getDeclaredMethod("test");
  49. testMethod.insertAfter("System.out.println(\"inserted!\");");
  50. cc.writeFile();
  51. Object obj = make(cc.getName());
  52. assertEquals(10, invoke(obj, "run"));
  53. }
  54. public void testJIRA246() throws Exception {
  55. CtClass ctClass = sloader.makeClass("test5.JIRA246Test");
  56. ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName()));
  57. String methodBody = "public void test() { defaultMethod(); }";
  58. CtMethod ctMethod = CtMethod.make(methodBody, ctClass);
  59. ctClass.addMethod(ctMethod);
  60. }
  61. public void testJIRA246b() throws Exception {
  62. CtClass ctClass = sloader.get(test5.JIRA246.A.class.getName());
  63. String src = "public void id() { get(); }";
  64. CtMethod make = CtNewMethod.make(src, ctClass);
  65. }
  66. public void testJIRA242() throws Exception {
  67. Boolean ss = new Boolean(2 > 3);
  68. ClassPool cp = ClassPool.getDefault();
  69. CtClass cc = cp.get("test5.JIRA242$Hello");
  70. CtMethod m = cc.getDeclaredMethod("say");
  71. m.insertBefore("{ System.out.println(\"Say Hello...\"); }");
  72. StringBuilder sb = new StringBuilder();
  73. sb.append("BOOL_SERIES = createBooleanSeriesStep();");
  74. //Below code cause the issue
  75. sb.append("BOOL_SERIES.setValue(3>=3);"); //lets comment this and run it will work
  76. // Below code snippets will work
  77. // this cast into exact class and call the same function
  78. sb.append("((test5.JIRA242$BooleanDataSeries)BOOL_SERIES).setValue(3>=3);");
  79. // this code snippet will set exact boolean variable to the function.
  80. sb.append("boolean var = 3>=3;");
  81. sb.append("BOOL_SERIES.setValue(var);");
  82. m.insertBefore(sb.toString());
  83. cc.writeFile();
  84. Object obj = make(cc.getName());
  85. assertEquals(0, invoke(obj, "say"));
  86. }
  87. public void testJIRA249() throws Exception {
  88. CtClass cc = sloader.get("test5.BoolTest");
  89. CtMethod testMethod = cc.getDeclaredMethod("test");
  90. testMethod.insertBefore("i = foo(true & true);");
  91. cc.writeFile();
  92. Object obj = make(cc.getName());
  93. assertEquals(1, invoke(obj, "run"));
  94. }
  95. public void testInnerClassAttributeRemove() throws Exception {
  96. CtClass cc = sloader.get("test5.InnerClassRemove");
  97. ClassFile cf = cc.getClassFile();
  98. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
  99. String second = ica.innerClass(1);
  100. String secondName = ica.innerName(1);
  101. String third = ica.innerClass(2);
  102. String thirdName = ica.innerName(2);
  103. assertEquals(3, ica.remove(3));
  104. assertEquals(2, ica.remove(0));
  105. assertEquals(second, ica.innerClass(0));
  106. assertEquals(secondName, ica.innerName(0));
  107. assertEquals(third, ica.innerClass(1));
  108. assertEquals(thirdName, ica.innerName(1));
  109. assertEquals(1, ica.remove(1));
  110. assertEquals(second, ica.innerClass(0));
  111. assertEquals(secondName, ica.innerName(0));
  112. cc.writeFile();
  113. Object obj = make(cc.getName());
  114. assertEquals(1, invoke(obj, "run"));
  115. }
  116. public void testJIRA248() throws Exception {
  117. CtClass cc = sloader.get("test5.JIRA248");
  118. String methodBody = "public int run() { return foo() + super.foo() + super.bar() + test5.JIRA248Intf2.super.baz(); }";
  119. CtMethod ctMethod = CtMethod.make(methodBody, cc);
  120. cc.addMethod(ctMethod);
  121. cc.writeFile();
  122. Object obj = make(cc.getName());
  123. assertEquals(40271, invoke(obj, "run"));
  124. }
  125. public void testInvalidCastWithDollar() throws Exception {
  126. String code = "{ new JavassistInvalidCastTest().inspectReturn((Object) ($w) $_); } ";
  127. CtClass c = sloader.get("test5.InvalidCastDollar");
  128. for (CtMethod method : c.getDeclaredMethods())
  129. method.insertAfter(code);
  130. }
  131. public void testJIRA256() throws Exception {
  132. // CtClass ec = sloader.get("test5.Entity");
  133. CtClass cc = sloader.makeClass("test5.JIRA256");
  134. ClassFile ccFile = cc.getClassFile();
  135. ConstPool constpool = ccFile.getConstPool();
  136. AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
  137. javassist.bytecode.annotation.Annotation entityAnno
  138. = new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
  139. // = new javassist.bytecode.annotation.Annotation(constpool, ec);
  140. entityAnno.addMemberValue("value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
  141. attr.addAnnotation(entityAnno);
  142. ccFile.addAttribute(attr);
  143. cc.writeFile();
  144. Object o = make(cc.getName());
  145. assertTrue(o.getClass().getName().equals("test5.JIRA256"));
  146. java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
  147. assertEquals(1, annotations.length);
  148. }
  149. }