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