{"id":1537,"date":"2017-07-19T22:41:16","date_gmt":"2017-07-19T14:41:16","guid":{"rendered":"http:\/\/coderbee.net\/?p=1537"},"modified":"2017-07-19T22:41:16","modified_gmt":"2017-07-19T14:41:16","slug":"javassist-%e5%ad%97%e8%8a%82%e7%a0%81%e6%93%8d%e4%bd%9c%e5%ba%93","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20170719\/1537","title":{"rendered":"Javassist \u5b57\u8282\u7801\u64cd\u4f5c\u5e93"},"content":{"rendered":"<p>\u7814\u7a76 Javassist \u7684\u8d77\u56e0\u662f\u7ef4\u62a4\u7684\u9879\u76ee\u662f\u4ece\u5916\u90e8\u91c7\u8d2d\u7684\u4e00\u4e2a\u7cfb\u7edf\uff0c\u6709\u51e0\u4e2a\u6838\u5fc3\u7684\u7c7b\u5728\u6784\u9020\u51fd\u6570\u91cc\u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u4e00\u4e9b\u914d\u7f6e\u4fe1\u606f\uff0c\u800c\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u57fa\u672c\u662f\u4e0d\u4f1a\u6539\u53d8\u7684\uff0c\u5e94\u5f53\u7f13\u5b58\u8d77\u6765\u3002\u8fd9\u4e9b\u7c7b\u6ca1\u6709\u6e90\u7801\uff0cclass \u6587\u4ef6\u8fd8\u662f\u6df7\u6dc6\u8fc7\u7684\u3002<\/p>\n<p>\u60f3\u6765\u60f3\u53bb\uff0c\u89c9\u5f97\u7528\u5b57\u8282\u7801\u64cd\u4f5c\u5de5\u5177\u6765\u6539\u5199\u662f\u6bd4\u8f83\u5408\u9002\u7684\uff0c\u628a\u6539\u5199\u540e\u751f\u6210\u7684 class \u6587\u4ef6\u66ff\u6362\u539f\u6765\u7684\uff0c\u8fd9\u6837\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u7684\u5730\u65b9\u4e5f\u4e0d\u7528\u505a\u4efb\u4f55\u4fee\u6539\u3002<\/p>\n<h1>1. Javassist \u7b80\u4ecb<\/h1>\n<p>Javassist \u662f\u4e00\u4e2a\u5b57\u8282\u7801\u64cd\u4f5c\u5e93\uff0c\u901a\u8fc7\u5b83\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6539\u5199\u7c7b\uff1a\u6dfb\u52a0\u65b0\u7684\u5b57\u6bb5\u3001\u65b9\u6cd5\u548c\u6784\u9020\u51fd\u6570\uff0c\u6539\u53d8\u7c7b\u3001\u7236\u7c7b\u548c\u63a5\u53e3\u7684\u65b9\u6cd5\u3002<\/p>\n<p>Javassist \u5b9a\u4e49\u4e86 CtField, CtMethod, CtConstructor, CtClass \u6765\u8868\u793a \u5b57\u6bb5\u3001\u65b9\u6cd5\u3001\u6784\u9020\u51fd\u6570\u3001\u7c7b\u3002<\/p>\n<p><!--more--><\/p>\n<h1>2. \u64cd\u4f5c\u793a\u4f8b<\/h1>\n<h2>2.1 \u64cd\u4f5c\u76ee\u6807\u4ee3\u7801<\/h2>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u8981\u64cd\u4f5c\u7684\u76ee\u6807\u7c7b\uff0c\u7f16\u8bd1\u751f\u6210 class \u6587\u4ef6\u3002<\/p>\n<pre><code class=\"java\">package net.coderbee.javassist;\n\npublic class Person {\n    private String name;\n    private int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n        System.out.println(\"name:\" + this.name + \", age:\" + this.age);\n    }\n\n    public String show() {\n        String msg = \"show name:\" + this.name + \", age:\" + this.age;\n        System.out.println(msg);\n        return msg;\n    }\n\n    public int longRun(long sleep) throws InterruptedException {\n        Thread.sleep(sleep);\n        return 0;\n    }\n\n}\n<\/code><\/pre>\n<h2>2.2 \u83b7\u53d6 CtClass \u5b9e\u4f8b<\/h2>\n<p>\u83b7\u53d6 CtClass \u5b9e\u4f8b\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff1a<code>ClassPool.getDefault().get(\"net.coderbee.javassist.Person\");<\/code> \u3002<\/p>\n<h2>2.3 \u64cd\u4f5c\u5b57\u6bb5<\/h2>\n<p>\u5982\u4e0b\u4ee3\u7801\u83b7\u53d6\u5b57\u6bb5\u540d\u4e3a <code>name<\/code> \u7684\u5b57\u6bb5\uff0c\u628a\u5b83\u6539\u4e3a <code>public<\/code> \u4e14\u662f <code>volatile<\/code> \u4fee\u9970\uff1a<\/p>\n<pre><code class=\"java\">CtField nameField = ctClass.getField(\"name\");\nnameField.setModifiers(Modifier.PUBLIC | Modifier.VOLATILE);\n<\/code><\/pre>\n<h2>2.4 \u64cd\u4f5c\u65b9\u6cd5<\/h2>\n<p>Javassist \u7528 <code>$<\/code> \u52a0\u6570\u5b57\u7684\u6807\u8bc6\u7b26\u8868\u793a \u65b9\u6cd5\u6216\u6784\u9020\u51fd\u6570\u7684 \u53c2\u6570\u3002 \u7528 <code>$0<\/code> \u8868\u793a\u5f53\u524d\u7c7b\u7684\u5b9e\u4f8b <code>this<\/code>\uff0c <code>$$<\/code>\u8868\u793a\u5f53\u524d\u65b9\u6cd5\u7684\u4e00\u5e8f\u5217\u53c2\u6570\uff0c <code>$1<\/code> \u8868\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c<code>$2<\/code> \u8868\u793a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5176\u4ed6\u53c2\u6570\u7c7b\u63a8\u3002<\/p>\n<h3>2.4.1 \u6dfb\u52a0\u6784\u9020\u51fd\u6570<\/h3>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u7ed9 <code>Person<\/code> \u7c7b\u6dfb\u52a0\u53e6\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"java\">CtClass stringClass = ClassPool.getDefault().get(\"java.lang.String\");\nCtClass intClass = ClassPool.getDefault().getCtClass(\"int\");\nCtConstructor constructor = new CtConstructor(new CtClass[] { intClass, stringClass }, ctClass);\nconstructor.setBody(\"{this.age = $0; this.name = $2; }\");\nctClass.addConstructor(constructor);\n<\/code><\/pre>\n<h3>2.4.2 \u7ed9\u65b9\u6cd5\u52a0\u4e0a\u8017\u65f6\u7edf\u8ba1<\/h3>\n<pre><code class=\"java\">private static void wrapMethod(CtClass ctClass, String methodName) throws NotFoundException, CannotCompileException {\n    CtMethod targetMethod = ctClass.getDeclaredMethod(methodName);\n    String returnType = targetMethod.getReturnType().getName();\n\n    String newMethodName = targetMethod.getName() + \"$impl\";\n    CtMethod cloneMethod = CtNewMethod.copy(targetMethod, newMethodName, ctClass, null);\n    cloneMethod.setModifiers(Modifier.PRIVATE);\n    ctClass.addMethod(cloneMethod);\n\n    String warpBody = buildWrapBody(returnType, newMethodName);\n    targetMethod.setBody(warpBody);\n}\n\nprivate static String buildWrapBody(String returnType, String newMethodName) {\n    boolean hasReturn = !\"void\".equals(returnType);\n\n    StringBuilder sb = new StringBuilder(256);\n    sb.append(\"\\t{\\r\\n\\t\\tlong start = System.currentTimeMillis();\\r\\n\");\n\n    if (hasReturn) {\n        sb.append(returnType).append(\"\\t\\tresult = \").append(newMethodName).append(\"($$);\");\n    } else {\n        sb.append(\"\\r\\n\\t\\t\").append(newMethodName).append(\"($$);\");\n    }\n    sb.append(\"\\r\\n\\t\\tlong end = System.currentTimeMillis();\");\n    sb.append(\"\\r\\n\\t\\tSystem.out.println(\\\"call show used millis:\\\" + (end-start));\");\n    if (hasReturn) {\n        sb.append(\"\\r\\n\\t\\treturn result;\");\n    }\n    sb.append(\"\\r\\n\\t}\");\n    return sb.toString();\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5904\u7406\u8fc7\u540e\u751f\u6210\u7684\u5b57\u8282\u7801\u6587\u4ef6\u53cd\u7f16\u8bd1\u540e\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"java\">package net.coderbee.javassist;\n\nimport java.io.PrintStream;\n\npublic class Person {\n    public volatile String name;\n    private int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n        System.out.println(\"name:\" + this.name + \", age:\" + this.age);\n    }\n\n    public String show() {\n        long l1 = System.currentTimeMillis();\n        String str = show$impl();\n        long l2 = System.currentTimeMillis();\n        System.out.println(\"call show used millis:\" + (l2 - l1));\n        return str;\n    }\n\n    public int longRun(long paramLong) throws InterruptedException {\n        long l1 = System.currentTimeMillis();\n        int i = longRun$impl(paramLong);\n        long l2 = System.currentTimeMillis();\n        System.out.println(\"call show used millis:\" + (l2 - l1));\n        return i;\n    }\n\n    private String show$impl() {\n        String msg = \"show name:\" + this.name + \", age:\" + this.age;\n        System.out.println(msg);\n        return msg;\n    }\n\n    private int longRun$impl(long sleep) throws InterruptedException {\n        Thread.sleep(sleep);\n        return 0;\n    }\n\n    public Person(int paramInt, String paramString) {\n        this(paramString, paramInt);\n        this.age = paramInt;\n        this.name = paramString;\n    }\n}\n<\/code><\/pre>\n<p>\u6709\u4e2a\u5c0f\u95ee\u9898\u662f\uff0cJavassist \u81ea\u52a8\u52a0\u4e86\u4e00\u4e2a\u6ca1\u6709\u4f7f\u7528\u7684\u5bfc\u5165\uff1a <code>import java.io.PrintStream;<\/code> \u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7814\u7a76 Javassist \u7684\u8d77\u56e0\u662f\u7ef4\u62a4\u7684\u9879\u76ee\u662f\u4ece\u5916\u90e8\u91c7\u8d2d\u7684\u4e00\u4e2a\u7cfb\u7edf\uff0c\u6709\u51e0\u4e2a\u6838\u5fc3\u7684 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20170719\/1537\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[231,277],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1537"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1537"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1537\/revisions"}],"predecessor-version":[{"id":1538,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1537\/revisions\/1538"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}