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.

Jassist150.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import javassist.CannotCompileException;
  2. import javassist.ClassPool;
  3. import javassist.CtClass;
  4. import javassist.CtMethod;
  5. import javassist.NotFoundException;
  6. import javassist.expr.ExprEditor;
  7. import javassist.expr.MethodCall;
  8. @SuppressWarnings("unused")
  9. public class Jassist150 {
  10. public static final String BASE_PATH = "./";
  11. public static final String JAVASSIST_JAR = BASE_PATH + "javassist.jar";
  12. public static final String CLASSES_FOLDER = BASE_PATH + "build/classes";
  13. public static final String TEST_CLASSES_FOLDER = BASE_PATH
  14. + "build/test-classes";
  15. public static class Inner1 {
  16. public static int get() {
  17. return 0;
  18. }
  19. }
  20. public static void implTestClassTailCache() throws NotFoundException,
  21. CannotCompileException {
  22. ClassPool pool = new ClassPool(true);
  23. for (int paths = 0; paths < 50; paths++) {
  24. pool.appendClassPath(JAVASSIST_JAR);
  25. pool.appendClassPath(CLASSES_FOLDER);
  26. pool.appendClassPath(TEST_CLASSES_FOLDER);
  27. }
  28. CtClass cc = pool.get("Jassist150$Inner1");
  29. CtMethod ccGet = cc.getDeclaredMethod("get");
  30. String code1 = "{ int n1 = Integer.valueOf(1); "
  31. + " int n2 = Integer.valueOf(2); "
  32. + " int n3 = Integer.valueOf(3); "
  33. + " int n4 = Integer.valueOf(4); "
  34. + " int n5 = Integer.valueOf(5); "
  35. + " return n1+n2+n3+n4+n5; }";
  36. String code2 = "{ int n1 = java.lang.Integer.valueOf(1); "
  37. + " int n2 = java.lang.Integer.valueOf(2); "
  38. + " int n3 = java.lang.Integer.valueOf(3); "
  39. + " int n4 = java.lang.Integer.valueOf(4); "
  40. + " int n5 = java.lang.Integer.valueOf(5); "
  41. + " return n1+n2+n3+n4+n5; }";
  42. String code3 = "{ int n1 = java.lang.Integer#valueOf(1); "
  43. + " int n2 = java.lang.Integer#valueOf(2); "
  44. + " int n3 = java.lang.Integer#valueOf(3); "
  45. + " int n4 = java.lang.Integer#valueOf(4); "
  46. + " int n5 = java.lang.Integer#valueOf(5); "
  47. + " return n1+n2+n3+n4+n5; }";
  48. loop(cc, ccGet, code1);
  49. }
  50. public static void loop(CtClass cc, CtMethod ccGet, String code)
  51. throws CannotCompileException {
  52. long startTime = System.currentTimeMillis();
  53. for (int replace = 0; replace < 1000; replace++) {
  54. ccGet.setBody(code);
  55. }
  56. long endTime = System.currentTimeMillis();
  57. System.out.println("Test: Time (ms) " + (endTime - startTime));
  58. }
  59. public static void implTestClassTailCache2() throws NotFoundException,
  60. CannotCompileException {
  61. ClassPool pool = new ClassPool(true);
  62. for (int paths = 0; paths < 50; paths++) {
  63. pool.appendClassPath(JAVASSIST_JAR);
  64. pool.appendClassPath(CLASSES_FOLDER);
  65. pool.appendClassPath(TEST_CLASSES_FOLDER);
  66. }
  67. CtClass cc = pool.get("Jassist150$Inner1");
  68. CtMethod ccGet = cc.getDeclaredMethod("get");
  69. String code3 = "{ int n1 = java.lang.Integer#valueOf(1); "
  70. + " int n2 = java.lang.Integer#valueOf(2); "
  71. + " int n3 = java.lang.Integer#valueOf(3); "
  72. + " int n4 = java.lang.Integer#valueOf(4); "
  73. + " int n5 = java.lang.Integer#valueOf(5); "
  74. + " return n1+n2+n3+n4+n5; }";
  75. ccGet.setBody(code3);
  76. }
  77. public void testJIRA152() throws Exception {
  78. CtClass cc = ClassPool.getDefault().get("test4.JIRA152");
  79. CtMethod mth = cc.getDeclaredMethod("buildColumnOverride");
  80. mth.instrument(new ExprEditor() {
  81. public void edit(MethodCall c) throws CannotCompileException {
  82. c.replace("try{ $_ = $proceed($$); } catch (Throwable t) { throw t; }");
  83. }
  84. });
  85. mth.getMethodInfo().rebuildStackMap(ClassPool.getDefault());
  86. cc.writeFile();
  87. }
  88. public static void main(String[] args) throws Exception {
  89. new Jassist150().testJIRA152();
  90. }
  91. public static void main2(String[] args) {
  92. for (int loop = 0; loop < 5; loop++) {
  93. try {
  94. implTestClassTailCache();
  95. for (int i = 0; i < 100; i++)
  96. implTestClassTailCache2();
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. System.out.println("size: " + javassist.compiler.MemberResolver.getInvalidMapSize());
  102. }
  103. }