Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Jassist150.java 4.5KB

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