{"id":10006956,"date":"2022-12-14T17:03:20","date_gmt":"2022-12-14T09:03:20","guid":{"rendered":"https:\/\/ityet.com\/?p=10006956"},"modified":"2022-12-14T17:03:20","modified_gmt":"2022-12-14T09:03:20","slug":"%e6%b5%85%e8%b0%88%e5%ad%97%e8%8a%82%e7%a0%81%e5%a2%9e%e5%bc%ba%e6%8a%80%e6%9c%af%e7%b3%bb%e5%88%972-asm%e4%b8%8ecglib","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006956","title":{"rendered":"\u6d45\u8c08\u5b57\u8282\u7801\u589e\u5f3a\u6280\u672f\u7cfb\u52172-Asm\u4e0eCglib"},"content":{"rendered":"<h5><strong>\u4f5c\u8005\uff1a\u8463\u5b50\u9f99<\/strong><\/h5>\n<h2>\u524d\u8a00<\/h2>\n<p>\u8bb0\u5f97\u90a3\u662f2022\u5e74\u79cb\u5929\u7684\u7b2c\u4e00\u573a\u96e8\uff0c\u6bd42021\u5e74\u6765\u7684\u7a0d\u665a\u4e00\u4e9b\uff0c\u5728\u90a3\u4e2a\u79cb\u96e8\u6726\u80e7\u7684\u4e0b\u5348\uff0c\u6b63\u5728\u5de5\u4f4d\u4e0a\u594b\u7b14\u75be\u4e66\u7684\u6211\u7a81\u7136\u542c\u5230\u4e86\u524d\u9762\u6ce2\u54e5\u5bf9\u7740\u624b\u673a\u542c\u7b52\u8bf4\u51fa\u6765\u7684&#8221;\u6e29\u67d4&#8221;\u7684\u8bdd\u8bed\uff1a\u8bf4\u8bf4\u4f60\u4e86\u89e3\u7684spring-aop\u3002\u8bdd\u97f3\u521a\u843d\uff0caop\u8fd9\u4e09\u4e2a\u5b57\u4fbf\u72b9\u5982\u4e00\u628a\u5229\u5251\u4e00\u6837\u72e0\u72e0\u7684\u624e\u5230\u4e86\u6211\u7684\u5fc3\u4e0a\uff0c\u8ba9\u6211\u7684\u8111\u6d77\u4e2d\u987f\u65f6\u6d6e\u73b0\u4e86\u5f53\u5e74\u521a\u521a\u6bd5\u4e1a\u88ab\u9762\u8bd5\u5b98&#8221;\u8e42\u8e8f&#8221;\u7684\u51c4\u60e8\u753b\u9762\u3002\u5386\u7ecf\u591a\u5e74\uff0c\u76f4\u81f3\u73b0\u5728\uff0c\u867d\u7136\u65e5\u5e38\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u4f7f\u7528aop\u505a\u4e00\u4e9b\u4e1a\u52a1\u529f\u80fd\u7684\u5f00\u53d1\uff0c\u4f46\u662f\u5982\u679c\u8ba9\u6211\u89e3\u91ca&#8221;\u9762\u5411\u5207\u9762&#8221;\u8fd9\u56db\u4e2a\u5b57\u7684\u610f\u601d\uff0c\u6211\u8fd8\u662f\u4f1a&#8221;\u5341\u8138\u61f5\u903c&#8221;\uff0c\u54c8\u54c8\u3002\u90a3\u4e48\u4eca\u5929\u7684\u6587\u7ae0\uff0c\u4f5c\u4e3a\u5b57\u8282\u7801\u589e\u5f3a\u6280\u672f\u7cfb\u5217\u627f\u4e0a\u542f\u4e0b\u7684\u7b2c\u4e8c\u7bc7\uff0c\u5c31\u8ba9\u6211\u4eec\u4ee5aop\u4e3a\u9a6c\uff0c\u53bb\u8ffd\u9010\u5b57\u8282\u7801\u7684\u661f\u5149\u3002<\/p>\n<h2><strong>\u4e00\u3001SpringAop\u4e0eCglib<\/strong><\/h2>\n<h4><strong>1.1\u3001aop\u91cd\u8981\u6982\u5ff5<\/strong><\/h4>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062557\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h4><strong>1.2\u3001\u5b9e\u73b0\u539f\u7406\u89e3\u6790<\/strong><\/h4>\n<p>Spring AOP\u7684\u5b9e\u73b0\u539f\u7406\u662f\u57fa\u4e8e\u52a8\u6001\u7ec7\u5165\u7684\u52a8\u6001\u4ee3\u7406\u6280\u672f\uff0c\u800c\u52a8\u6001\u4ee3\u7406\u6280\u672f\u53c8\u5206\u4e3aJava JDK\u52a8\u6001\u4ee3\u7406\u548cCGLIB\u52a8\u6001\u4ee3\u7406\u3002\u5177\u4f53\u4f7f\u7528\u54ea\u4e00\u79cd\u9700\u8981\u6839\u636e AopProxyFactory \u63a5\u53e3\u7684 createProxy \u65b9\u6cd5\u4e2d\u7684 AdvisedSupport \u4e2d\u7684\u53c2\u6570\u8fdb\u884c\u786e\u5b9a\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5982\u679c\u76ee\u6807\u7c7b\u662f\u63a5\u53e3\uff0c\u5219\u4f7f\u7528 jdk \u52a8\u6001\u4ee3\u7406\u6280\u672f\uff0c\u5982\u679c\u662f\u975e\u63a5\u53e3\u7c7b\uff0c\u5219\u4f7f\u7528 cglib \u6765\u751f\u6210\u4ee3\u7406\u3002\u5177\u4f53\u7684\u6e90\u7801\u5c31\u4e0d\u7ed9\u5927\u5bb6\u4e00\u4e00\u8d34\u51fa\u6765\u4e86\uff0c\u5927\u5bb6\u53ef\u4ee5\u53bb\u7f51\u4e0a\u641c\u7d22\u4e00\u4e9b\u8d44\u6599\u8fdb\u884c\u6df1\u5165\u4e86\u89e3\u3002\u8fd9\u91cc\u53ea\u7ed9\u5927\u5bb6\u8d34\u4e00\u5f20\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u7684\u56fe\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062558\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h5><strong>1.2.1\u3001jdk\u52a8\u6001\u4ee3\u7406<\/strong><\/h5>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062559\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>spring-aop\u4e2djdk\u4ee3\u7406\u7684\u5b9e\u73b0\u548c\u6211\u4eec\u5e73\u65f6\u81ea\u5df1\u5b9e\u73b0\u52a8\u6001\u4ee3\u7406\u5f00\u53d1\u662f\u4e00\u81f4\u7684\uff0c\u6240\u4ee5\u6b64\u5904\u4e0d\u505a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u53ea\u662f\u7b80\u8981\u7ed9\u5927\u5bb6\u8bf4\u660e\u4e00\u4e0b\u3002\u4f7f\u7528\u505ajdk\u52a8\u6001\u4ee3\u7406\u65f6\uff0c\u4ee3\u7406\u7c7b\u8981\u5b9e\u73b0InvocationHandler\u63a5\u53e3\uff0c\u76ee\u6807\u7c7b\u8981\u5b9e\u73b0\u63a5\u53e3\uff0c\u56e0\u4e3aJDK\u63d0\u4f9b\u7684Proxy\u7c7b\u5c06\u901a\u8fc7\u76ee\u6807\u5bf9\u8c61\u7684\u7c7b\u52a0\u8f7d\u5668ClassLoader\u548cInterface\uff0c\u4ee5\u53ca\u53e5\u67c4(Callback)\u521b\u5efa\u4e0e\u76ee\u6807\u7c7b\u62e5\u6709\u76f8\u540c\u63a5\u53e3\u7684\u4ee3\u7406\u5bf9\u8c61proxy\uff0c\u8be5\u4ee3\u7406\u5bf9\u8c61\u5c06\u62e5\u6709\u76ee\u6807\u7c7b\u63a5\u53e3\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u540c\u65f6\u4ee3\u7406\u7c7b\u5fc5\u987b\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u4f3c\u56de\u8c03\u51fd\u6570\u7684InvocationHandler\u63a5\u53e3\u5e76\u91cd\u5199\u8be5\u63a5\u53e3\u4e2d\u7684invoke\u65b9\u6cd5\uff0c\u5f53\u8c03\u7528proxy\u7684\u6bcf\u4e2a\u65b9\u6cd5(\u5982\u6848\u4f8b\u4e2d\u7684proxy#execute())\u65f6\uff0cinvoke\u65b9\u6cd5\u5c06\u88ab\u8c03\u7528\uff0c\u5229\u7528\u8be5\u7279\u6027\uff0c\u53ef\u4ee5\u5728invoke\u65b9\u6cd5\u4e2d\u5bf9\u76ee\u6807\u5bf9\u8c61\u65b9\u6cd5\u6267\u884c\u7684\u524d\u540e\u52a8\u6001\u6dfb\u52a0\u5176\u4ed6\u5916\u56f4\u4e1a\u52a1\u64cd\u4f5c\uff0c\u6b64\u65f6\u65e0\u9700\u89e6\u53ca\u76ee\u6807\u5bf9\u8c61\u7684\u4efb\u4f55\u4ee3\u7801\uff0c\u4e5f\u5c31\u5b9e\u73b0\u4e86\u5916\u56f4\u4e1a\u52a1\u7684\u64cd\u4f5c\u4e0e\u76ee\u6807\u5bf9\u8c61\u5b8c\u5168\u89e3\u8026\u5408\u7684\u76ee\u7684\u3002\u5f53\u7136\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\u9700\u8981\u62e5\u6709\u63a5\u53e3\uff0c\u8fd9\u4e5f\u5c31\u6709\u4e86\u540e\u6765\u7684CGLIB\u52a8\u6001\u4ee3\u7406\u4e86\u3002<\/p>\n<h5><strong>1.2.2\u3001cglib\u52a8\u6001\u4ee3\u7406<\/strong><\/h5>\n<p>spring-aop\u4e2dcglib\u4ee3\u7406\u7684\u5b9e\u73b0\u7ed9\u5927\u5bb6\u8d34\u4e00\u4e0b\u6e90\u7801\u7684\u8def\u5f84org.springframework.aop.framework.CglibAopProxy\u3001org.springframework.aop.framework.CglibAopProxy#getProxy(java.lang.ClassLoader)\uff0c\u611f\u5174\u8da3\u7684\u8bdd\u5927\u5bb6\u53ef\u4ee5\u81ea\u5df1\u53bb\u770b\u4e00\u4e0b\u6e90\u7801\uff0c\u63a5\u4e0b\u6765\u6211\u4f1a\u4e3e\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff0c\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528cglib\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u3002\uff08\u6ce8\uff1acglibgithub\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=uW2xi0f0sYevH1U1pEvQzA%3D%3D.MMh1silmqfG87x5qeALfjJs%2FmBQ0LsppJaR%2BfpuKRuE%3D\" rel=\"nofollow\">https:\/\/github.com\/cglib\/cglib<\/a>\uff09<\/p>\n<h6><strong>1.2.2.1\u3001\u5f15\u5165jar\u5305<\/strong><\/h6>\n<pre><code> &lt;dependency&gt; \n     &lt;groupId&gt;cglib&lt;\/groupId&gt; \n     &lt;artifactId&gt;cglib&lt;\/artifactId&gt; \n     &lt;version&gt;3.2.5&lt;\/version&gt; \n&lt;\/dependency&gt;<\/code><\/pre>\n<h6><strong>1.2.2.2\u3001\u793a\u4f8b\u4ee3\u7801<\/strong><\/h6>\n<\/p>\n<pre><code>\/**\n * \u521b\u5efa\u76ee\u6807\u5bf9\u8c61\n *\/\npublic class Target {\n    public void execute(){\n        System.out.println(&quot;\u6267\u884cTarget\u7684execute\u65b9\u6cd5...&quot;);\n    }\n}\n\n\/**\n * \u521b\u5efacglib\u4ee3\u7406\u7c7b\n *\/\npublic class CGLibProxy implements MethodInterceptor {\n\n    \/**\n     * \u88ab\u4ee3\u7406\u7684\u76ee\u6807\u7c7b\n     *\/\n    private Target target;\n\n    public CGLibProxy(Target target) {\n        super();\n        this.target = target;\n    }\n\n    \/**\n     * \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\n     * @return\n     *\/\n    public Target createProxy(){\n        \/\/ \u4f7f\u7528CGLIB\u751f\u6210\u4ee3\u7406:\n        \/\/ 1.\u58f0\u660e\u589e\u5f3a\u7c7b\u5b9e\u4f8b,\u7528\u4e8e\u751f\u4ea7\u4ee3\u7406\u7c7b\n        Enhancer enhancer = new Enhancer();\n        \/\/ 2.\u8bbe\u7f6e\u88ab\u4ee3\u7406\u7c7b\u5b57\u8282\u7801\uff0cCGLIB\u6839\u636e\u5b57\u8282\u7801\u751f\u6210\u88ab\u4ee3\u7406\u7c7b\u7684\u5b50\u7c7b\n        enhancer.setSuperclass(target.getClass());\n        \/\/ 3.\/\/\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff0c\u5373\u4e00\u4e2a\u65b9\u6cd5\u62e6\u622a\n        enhancer.setCallback(this);\n        \/\/ 4.\u521b\u5efa\u4ee3\u7406:\n        return (Target) enhancer.create();\n    }\n\n    @Override\n    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {\n        \/\/ \u6307\u5b9a\u8981\u6267\u884c\u88ab\u4ee3\u7406\u7684\u65b9\u6cd5\n        if(&quot;execute&quot;.equals(method.getName())) {\n            \/\/\u8c03\u7528\u524d\u6267\u884c\u65b9\u6cd5\n            System.out.println(&quot;\u8c03\u7528\u524d\u6267\u884c\u65b9\u6cd5&quot;);\n            \/\/\u8c03\u7528\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5(\u6267\u884cA\u5bf9\u8c61\u5373\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684execute\u65b9\u6cd5)\n            Object result = methodProxy.invokeSuper(proxy, args);\n            \/\/\u8bb0\u5f55\u65e5\u5fd7\u6570\u636e(\u52a8\u6001\u6dfb\u52a0\u5176\u4ed6\u8981\u6267\u884c\u4e1a\u52a1)\n            System.out.println(&quot;\u8c03\u7528\u524d\u6267\u884c\u65b9\u6cd5&quot;);\n            return result;\n        }\n        \/\/\u5982\u679c\u4e0d\u9700\u8981\u589e\u5f3a\u76f4\u63a5\u6267\u884c\u539f\u65b9\u6cd5\n        return methodProxy.invokeSuper(proxy, args);\n    }\n}\n\n\/**\n * \u521b\u5efa\u6d4b\u8bd5\u7c7b\n *\/\npublic class CglibTest {\n\n    public static void main(String[] args) {\n        \/\/ \u5c06cglib\u751f\u6210\u7684\u4ee3\u7406\u7c7b\u5199\u5165\u5230\u78c1\u76d8\n        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,&quot;D:\/test\/test&quot;);\n        CGLibProxy cgLibProxy = new CGLibProxy(new Target());\n        Target proxy = cgLibProxy.createProxy();\n        proxy.execute();\n    }\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062560\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u4ece\u4ee3\u7801\u770b\u88ab\u4ee3\u7406\u7684\u7c7b\u65e0\u9700\u63a5\u53e3\u5373\u53ef\u5b9e\u73b0\u52a8\u6001\u4ee3\u7406\uff0c\u800cCGLibProxy\u4ee3\u7406\u7c7b\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u62e6\u622a\u5668\u63a5\u53e3<strong>MethodInterceptor<\/strong>\u5e76\u91cd\u5199<strong>intercept<\/strong>\u65b9\u6cd5\uff0c\u7c7b\u4f3cJDK\u52a8\u6001\u4ee3\u7406\u7684InvocationHandler\u63a5\u53e3\uff0c\u4e5f\u662f\u7406\u89e3\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u540c\u7406\u6bcf\u6b21\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u6cd5\u65f6\uff0cintercept\u65b9\u6cd5\u90fd\u4f1a\u88ab\u8c03\u7528\uff0c\u5229\u7528\u8be5\u65b9\u6cd5\u4fbf\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5bf9\u65b9\u6cd5\u6267\u884c\u524d\u540e\u8fdb\u884c\u52a8\u6001\u589e\u5f3a\u3002\u5173\u4e8e\u4ee3\u7406\u5bf9\u8c61\u521b\u5efa\u5219\u901a\u8fc7<strong>Enhancer<\/strong>\u7c7b\u6765\u8bbe\u7f6e\u7684\uff0cEnhancer\u662f\u4e00\u4e2a\u7528\u4e8e\u4ea7\u751f\u4ee3\u7406\u5bf9\u8c61\u7684\u7c7b\uff0c\u4f5c\u7528\u7c7b\u4f3cJDK\u7684Proxy\u7c7b\uff0c\u56e0\u4e3aCGLib\u5e95\u5c42\u662f\u901a\u8fc7\u7ee7\u627f\u5b9e\u73b0\u7684\u52a8\u6001\u4ee3\u7406\uff0c\u56e0\u6b64\u9700\u8981\u4f20\u9012\u76ee\u6807\u5bf9\u8c61\u7684Class\uff0c\u540c\u65f6\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5bf9\u8c03\u7528\u65b9\u6cd5\u8fdb\u884c\u62e6\u622a\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6700\u540e\u901a\u8fc7create()\u521b\u5efa\u76ee\u6807\u5bf9\u8c61\u7684\u4ee3\u7406\u5bf9\u8c61\u3002<\/p>\n<h6><strong>1.2.2.3\u3001\u539f\u7406\u89e3\u6790<\/strong><\/h6>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062561\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u4e0a\u56fe\u662f\u6211\u4eec\u5728CglibTest \u7684main\u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u4e86\u5c06\u4ee3\u7406\u7c7b\u5199\u5165\u5230\u78c1\u76d8\u4e4b\u540e\u751f\u6210\u7684\u6587\u4ef6\uff0c\u4e00\u5171\u6709\u4e09\u4e2a\u3002\u6211\u4eec\u8fd9\u91cc\u91cd\u70b9\u770b\u4e00\u4e0b\u7b2c\u4e8c\u4e2a\u6587\u4ef6\uff0c\u4e0b\u9762\u662f\u8be5class\u6587\u4ef6\u7684\u6e90\u7801\uff0c\u7ed9\u5927\u5bb6\u8d34\u4e00\u4e0b\uff0c\u4e00\u4e9b\u91cd\u8981\u7684\u5730\u65b9\u6211\u4f1a\u52a0\u4e0a\u6ce8\u91ca\uff1a<\/p>\n<pre><code>\/\/\n\/\/ Source code recreated from a .class file by IntelliJ IDEA\n\/\/ (powered by FernFlower decompiler)\n\/\/\n\npackage com.test.excel.cglib;\n\nimport java.lang.reflect.Method;\nimport net.sf.cglib.core.ReflectUtils;\nimport net.sf.cglib.core.Signature;\nimport net.sf.cglib.proxy.Callback;\nimport net.sf.cglib.proxy.Factory;\nimport net.sf.cglib.proxy.MethodInterceptor;\nimport net.sf.cglib.proxy.MethodProxy;\n\npublic class Target$$EnhancerByCGLIB$$e7b1b1b0 extends Target implements Factory {\n    private boolean CGLIB$BOUND;\n    public static Object CGLIB$FACTORY_DATA;\n    private static final ThreadLocal CGLIB$THREAD_CALLBACKS;\n    private static final Callback[] CGLIB$STATIC_CALLBACKS;\n    \/\/ \u62e6\u622a\u5668\n    private MethodInterceptor CGLIB$CALLBACK_0;\n    private static Object CGLIB$CALLBACK_FILTER;\n    \/\/ \u88ab\u4ee3\u7406\u65b9\u6cd5\n    private static final Method CGLIB$execute$0$Method;\n    \/\/ \u4ee3\u7406\u65b9\u6cd5\n    private static final MethodProxy CGLIB$execute$0$Proxy;\n    private static final Object[] CGLIB$emptyArgs;\n    private static final Method CGLIB$equals$1$Method;\n    private static final MethodProxy CGLIB$equals$1$Proxy;\n    private static final Method CGLIB$toString$2$Method;\n    private static final MethodProxy CGLIB$toString$2$Proxy;\n    private static final Method CGLIB$hashCode$3$Method;\n    private static final MethodProxy CGLIB$hashCode$3$Proxy;\n    private static final Method CGLIB$clone$4$Method;\n    private static final MethodProxy CGLIB$clone$4$Proxy;\n\n    static void CGLIB$STATICHOOK1() {\n        CGLIB$THREAD_CALLBACKS = new ThreadLocal();\n        CGLIB$emptyArgs = new Object[0];\n        \/\/ \u4ee3\u7406\u7c7b\n        Class var0 = Class.forName(&quot;com.test.excel.cglib.Target$$EnhancerByCGLIB$$e7b1b1b0&quot;);\n        \/\/ \u88ab\u4ee3\u7406\u7c7b\n        Class var1;\n        CGLIB$execute$0$Method = ReflectUtils.findMethods(new String[]{&quot;execute&quot;, &quot;()V&quot;}, (var1 = Class.forName(&quot;com.test.excel.cglib.Target&quot;)).getDeclaredMethods())[0];\n        CGLIB$execute$0$Proxy = MethodProxy.create(var1, var0, &quot;()V&quot;, &quot;execute&quot;, &quot;CGLIB$execute$0&quot;);\n        Method[] var10000 = ReflectUtils.findMethods(new String[]{&quot;equals&quot;, &quot;(Ljava\/lang\/Object;)Z&quot;, &quot;toString&quot;, &quot;()Ljava\/lang\/String;&quot;, &quot;hashCode&quot;, &quot;()I&quot;, &quot;clone&quot;, &quot;()Ljava\/lang\/Object;&quot;}, (var1 = Class.forName(&quot;java.lang.Object&quot;)).getDeclaredMethods());\n        CGLIB$equals$1$Method = var10000[0];\n        CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, &quot;(Ljava\/lang\/Object;)Z&quot;, &quot;equals&quot;, &quot;CGLIB$equals$1&quot;);\n        CGLIB$toString$2$Method = var10000[1];\n        CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, &quot;()Ljava\/lang\/String;&quot;, &quot;toString&quot;, &quot;CGLIB$toString$2&quot;);\n        CGLIB$hashCode$3$Method = var10000[2];\n        CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, &quot;()I&quot;, &quot;hashCode&quot;, &quot;CGLIB$hashCode$3&quot;);\n        CGLIB$clone$4$Method = var10000[3];\n        CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, &quot;()Ljava\/lang\/Object;&quot;, &quot;clone&quot;, &quot;CGLIB$clone$4&quot;);\n    }\n\n    final void CGLIB$execute$0() {\n        super.execute();\n    }\n    \/\/ \u88ab\u4ee3\u7406\u7684\u65b9\u6cd5\n\n    public final void execute() {\n        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;\n        if (var10000 == null) {\n            CGLIB$BIND_CALLBACKS(this);\n            var10000 = this.CGLIB$CALLBACK_0;\n        }\n\n        if (var10000 != null) {\n            \/\/ \u8c03\u7528\u62e6\u622a\u5668\n            var10000.intercept(this, CGLIB$execute$0$Method, CGLIB$emptyArgs, CGLIB$execute$0$Proxy);\n        } else {\n            super.execute();\n        }\n    }\n\n    final boolean CGLIB$equals$1(Object var1) {\n        return super.equals(var1);\n    }\n\n    public final boolean equals(Object var1) {\n        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;\n        if (var10000 == null) {\n            CGLIB$BIND_CALLBACKS(this);\n            var10000 = this.CGLIB$CALLBACK_0;\n        }\n\n        if (var10000 != null) {\n            Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);\n            return var2 == null ? false : (Boolean)var2;\n        } else {\n            return super.equals(var1);\n        }\n    }\n\n    final String CGLIB$toString$2() {\n        return super.toString();\n    }\n\n    public final String toString() {\n        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;\n        if (var10000 == null) {\n            CGLIB$BIND_CALLBACKS(this);\n            var10000 = this.CGLIB$CALLBACK_0;\n        }\n\n        return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();\n    }\n\n    final int CGLIB$hashCode$3() {\n        return super.hashCode();\n    }\n\n    public final int hashCode() {\n        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;\n        if (var10000 == null) {\n            CGLIB$BIND_CALLBACKS(this);\n            var10000 = this.CGLIB$CALLBACK_0;\n        }\n\n        if (var10000 != null) {\n            Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);\n            return var1 == null ? 0 : ((Number)var1).intValue();\n        } else {\n            return super.hashCode();\n        }\n    }\n\n    final Object CGLIB$clone$4() throws CloneNotSupportedException {\n        return super.clone();\n    }\n\n    protected final Object clone() throws CloneNotSupportedException {\n        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;\n        if (var10000 == null) {\n            CGLIB$BIND_CALLBACKS(this);\n            var10000 = this.CGLIB$CALLBACK_0;\n        }\n\n        return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();\n    }\n\n    public static MethodProxy CGLIB$findMethodProxy(Signature var0) {\n        String var10000 = var0.toString();\n        switch(var10000.hashCode()) {\n        case -508378822:\n            if (var10000.equals(&quot;clone()Ljava\/lang\/Object;&quot;)) {\n                return CGLIB$clone$4$Proxy;\n            }\n            break;\n        case 539325408:\n            if (var10000.equals(&quot;execute()V&quot;)) {\n                return CGLIB$execute$0$Proxy;\n            }\n            break;\n        case 1826985398:\n            if (var10000.equals(&quot;equals(Ljava\/lang\/Object;)Z&quot;)) {\n                return CGLIB$equals$1$Proxy;\n            }\n            break;\n        case 1913648695:\n            if (var10000.equals(&quot;toString()Ljava\/lang\/String;&quot;)) {\n                return CGLIB$toString$2$Proxy;\n            }\n            break;\n        case 1984935277:\n            if (var10000.equals(&quot;hashCode()I&quot;)) {\n                return CGLIB$hashCode$3$Proxy;\n            }\n        }\n\n        return null;\n    }\n\n    public Target$$EnhancerByCGLIB$$e7b1b1b0() {\n        CGLIB$BIND_CALLBACKS(this);\n    }\n\n    public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {\n        CGLIB$THREAD_CALLBACKS.set(var0);\n    }\n\n    public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {\n        CGLIB$STATIC_CALLBACKS = var0;\n    }\n\n    private static final void CGLIB$BIND_CALLBACKS(Object var0) {\n        Target$$EnhancerByCGLIB$$e7b1b1b0 var1 = (Target$$EnhancerByCGLIB$$e7b1b1b0)var0;\n        if (!var1.CGLIB$BOUND) {\n            var1.CGLIB$BOUND = true;\n            Object var10000 = CGLIB$THREAD_CALLBACKS.get();\n            if (var10000 == null) {\n                var10000 = CGLIB$STATIC_CALLBACKS;\n                if (var10000 == null) {\n                    return;\n                }\n            }\n\n            var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];\n        }\n\n    }\n\n    public Object newInstance(Callback[] var1) {\n        CGLIB$SET_THREAD_CALLBACKS(var1);\n        Target$$EnhancerByCGLIB$$e7b1b1b0 var10000 = new Target$$EnhancerByCGLIB$$e7b1b1b0();\n        CGLIB$SET_THREAD_CALLBACKS((Callback[])null);\n        return var10000;\n    }\n\n    public Object newInstance(Callback var1) {\n        CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});\n        Target$$EnhancerByCGLIB$$e7b1b1b0 var10000 = new Target$$EnhancerByCGLIB$$e7b1b1b0();\n        CGLIB$SET_THREAD_CALLBACKS((Callback[])null);\n        return var10000;\n    }\n\n    public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {\n        CGLIB$SET_THREAD_CALLBACKS(var3);\n        Target$$EnhancerByCGLIB$$e7b1b1b0 var10000 = new Target$$EnhancerByCGLIB$$e7b1b1b0;\n        switch(var1.length) {\n        case 0:\n            var10000.&lt;init&gt;();\n            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);\n            return var10000;\n        default:\n            throw new IllegalArgumentException(&quot;Constructor not found&quot;);\n        }\n    }\n\n    public Callback getCallback(int var1) {\n        CGLIB$BIND_CALLBACKS(this);\n        MethodInterceptor var10000;\n        switch(var1) {\n        case 0:\n            var10000 = this.CGLIB$CALLBACK_0;\n            break;\n        default:\n            var10000 = null;\n        }\n\n        return var10000;\n    }\n\n    public void setCallback(int var1, Callback var2) {\n        switch(var1) {\n        case 0:\n            this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;\n        default:\n        }\n    }\n\n    public Callback[] getCallbacks() {\n        CGLIB$BIND_CALLBACKS(this);\n        return new Callback[]{this.CGLIB$CALLBACK_0};\n    }\n\n    public void setCallbacks(Callback[] var1) {\n        this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];\n    }\n\n    static {\n        CGLIB$STATICHOOK1();\n    }\n}<\/code><\/pre>\n<p>\u4ece\u4ee3\u7406\u5bf9\u8c61\u53cd\u7f16\u8bd1\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\uff0c\u4ee3\u7406\u5bf9\u8c61\u7ee7\u627f\u4e8eTarget\uff0c\u62e6\u622a\u5668\u8c03\u7528intercept()\u65b9\u6cd5\uff0cintercept()\u65b9\u6cd5\u7531\u81ea\u5b9a\u4e49CGLibProxy\u5b9e\u73b0\uff0c\u6240\u4ee5\uff0c\u6700\u540e\u8c03\u7528CGLibProxy\u4e2d\u7684intercept()\u65b9\u6cd5\uff0c\u4ece\u800c\u5b8c\u6210\u4e86\u7531\u4ee3\u7406\u5bf9\u8c61\u8bbf\u95ee\u5230\u76ee\u6807\u5bf9\u8c61\u7684\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u3002<\/p>\n<h5><strong>1.2.3\u3001jdk\u4ee3\u7406\u548ccglib\u4ee3\u7406\u7684\u603b\u7ed3<\/strong><\/h5>\n<p><strong>JDK\u52a8\u6001\u4ee3\u7406\uff1a<\/strong><\/p>\n<p><strong>.<\/strong> \u9700\u8981\u76ee\u6807\u7c7b\u5b9e\u73b0\u63a5\u53e3<\/p>\n<p><strong>.<\/strong> \u751f\u6210\u7684\u4ee3\u7406\u7c7b\u662f\u4e0e\u76ee\u6807\u7c7b\u5e73\u7ea7\uff0c\u5b9e\u73b0\u4e86\u5171\u540c\u7684\u63a5\u53e3<\/p>\n<p><strong>.<\/strong> \u4f7f\u7528\u53cd\u5c04\u7684\u65b9\u5f0f\u8fdb\u884c\u6700\u7ec8\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u6027\u80fd\u8f83\u4f4e<\/p>\n<p><strong>CGLIB\u52a8\u6001\u4ee3\u7406\uff1a<\/strong><\/p>\n<p><strong>.<\/strong> \u4e0d\u8981\u6c42\u76ee\u6807\u7c7b\u5b9e\u73b0\u63a5\u53e3<\/p>\n<p><strong>.<\/strong> \u751f\u6210\u7684\u4ee3\u7406\u7c7b\u662f\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b<\/p>\n<p><strong>.<\/strong> final\u65b9\u6cd5\u4e0d\u4f1a\u51fa\u73b0\u5728\u4ee3\u7406\u7c7b\u4e2d<\/p>\n<p><strong>.<\/strong> \u4f7f\u7528<strong>\u7a7a\u95f4\u6362\u65f6\u95f4<\/strong>\u7684\u601d\u60f3\u5bf9\u6700\u7ec8\u7684\u65b9\u6cd5\u8c03\u7528\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u63d0\u5347\u4e86\u8fd0\u884c\u65f6\u6027\u80fd\u3002<\/p>\n<p>\u770b\u5230\u8fd9\u91cc\uff0c\u5927\u4f6c\u4eec\u6b64\u65f6\u662f\u4e0d\u662f\u5fc3\u91cc\u4f1a\u4ea7\u751f\u4e2a\u7591\u95ee\uff1a\u4f60\u7684\u6807\u9898\u4e0d\u662fasm\u4e0ecglib\u5417\uff0c\u524d\u9762\u8bf4\u4e86\u8fd9\u4e48\u591acglib\u4ee3\u7406\u548cjdk\u4ee3\u7406\uff0c\u548casm\u6709\u5173\u7cfb\u5417\uff1f\u54c8\u54c8\uff0c\u5176\u5b9e\u867d\u7136\u6211\u4eec\u6ca1\u6709\u518d\u524d\u9762\u7684\u5185\u5bb9\u4e2d\u4ecb\u7ecdasm\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4f46\u662f\u5176\u5b9e\u5b57\u91cc\u884c\u95f4\u90fd\u900f\u6f0f\u7740asm\uff0c\u6574\u4f53\u603b\u7ed3\u4e00\u53e5\u5c31\u662fCGLIB\u5305\u7684\u5e95\u5c42\u662f\u901a\u8fc7\u4f7f\u7528\u4e00\u4e2a\u5c0f\u800c\u5feb\u7684\u5b57\u8282\u7801\u5904\u7406\u6846\u67b6ASM\uff0c\u6765\u8f6c\u6362\u5b57\u8282\u7801\u5e76\u751f\u6210\u65b0\u7684\u7c7b\u3002<\/p>\n<h3><strong>\u4e8c\u3001\u6df1\u5165ASM<\/strong><\/h3>\n<h4><strong>2.1\u3001\u7b80\u4ecb<\/strong><\/h4>\n<p>ASM \u662f\u4e00\u4e2a Java <a href=\"https:\/\/link.segmentfault.com\/?enc=A5IuRrfim11%2BuD34D%2BCQqg%3D%3D.w%2BntIkc1e4t32ruvYzFA%2Bo659TwUFhvauWggQGSwSzwTUtGz2hs%2BCvx5tkQ4C0ecECOAH0XEaXmSMA0Sl78EUBm%2BtWyVJ4GkIxXL0BqNPWWsdUI5nVF0LROVAVpLl2IK\" rel=\"nofollow\">\u5b57\u8282\u7801<\/a>\u64cd\u63a7\u6846\u67b6\u3002\u5b83\u80fd\u88ab\u7528\u6765\u52a8\u6001\u751f\u6210\u7c7b\u6216\u8005\u589e\u5f3a\u65e2\u6709\u7c7b\u7684\u529f\u80fd\u3002ASM \u53ef\u4ee5\u76f4\u63a5\u4ea7\u751f\u4e8c\u8fdb\u5236 class \u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u5728\u7c7b\u88ab\u52a0\u8f7d\u5165 Java \u865a\u62df\u673a\u4e4b\u524d\u52a8\u6001\u6539\u53d8\u7c7b\u884c\u4e3a\u3002Java class \u88ab\u5b58\u50a8\u5728\u4e25\u683c\u683c\u5f0f\u5b9a\u4e49\u7684 .class \u6587\u4ef6\u91cc\uff0c\u8fd9\u4e9b\u7c7b\u6587\u4ef6\u62e5\u6709\u8db3\u591f\u7684\u5143\u6570\u636e\u6765\u89e3\u6790\u7c7b\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1a\u7c7b\u540d\u79f0\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u4ee5\u53ca Java \u5b57\u8282\u7801\uff08\u6307\u4ee4\uff09\u3002ASM \u4ece\u7c7b\u6587\u4ef6\u4e2d\u8bfb\u5165\u4fe1\u606f\u540e\uff0c\u80fd\u591f\u6539\u53d8\u7c7b\u884c\u4e3a\uff0c\u5206\u6790\u7c7b\u4fe1\u606f\uff0c\u751a\u81f3\u80fd\u591f\u6839\u636e\u7528\u6237\u8981\u6c42\u751f\u6210\u65b0\u7c7b\u3002<\/p>\n<p>\u4e0e BCEL \u548c SERL \u4e0d\u540c\uff0cASM \u63d0\u4f9b\u4e86\u66f4\u4e3a\u73b0\u4ee3\u7684\u7f16\u7a0b\u6a21\u578b\u3002\u5bf9\u4e8e ASM \u6765\u8bf4\uff0cJava class \u88ab\u63cf\u8ff0\u4e3a\u4e00\u68f5\u6811\uff1b\u4f7f\u7528 \u201cVisitor\u201d \u6a21\u5f0f\u904d\u5386\u6574\u4e2a\u4e8c\u8fdb\u5236\u7ed3\u6784\uff1b\u4e8b\u4ef6\u9a71\u52a8\u7684\u5904\u7406\u65b9\u5f0f\u4f7f\u5f97\u7528\u6237\u53ea\u9700\u8981\u5173\u6ce8\u4e8e\u5bf9\u5176\u7f16\u7a0b\u6709\u610f\u4e49\u7684\u90e8\u5206\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3 Java \u7c7b\u6587\u4ef6\u683c\u5f0f\u7684\u6240\u6709\u7ec6\u8282\uff1aASM \u6846\u67b6\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u7684&#8221;response taker&#8221;\u5904\u7406\u8fd9\u4e00\u5207\u3002\uff08\u5b98\u65b9\u6587\u6863\uff1a<strong><em><a href=\"https:\/\/link.segmentfault.com\/?enc=wxaJFoVDTeAYB5OGNz8cwg%3D%3D.2FD8vMD%2FGG6QAcfp9y6SiMnvgkxu3cimkFgNc%2FxxCnlWf1JFhOP3stv5uZsyWK1%2F\" rel=\"nofollow\">https:\/\/asm.ow2.io\/asm4-guide&#8230;<\/a><\/em><\/strong> <strong><em>\uff0c<\/strong> <\/em><em>\u4e2d\u6587\u6587\u6863\uff1a<\/em> <a href=\"https:\/\/link.segmentfault.com\/?enc=PbPUJumgKfFbs1nj2TGTNg%3D%3D.2aNKUuXMOdEXZ3wfWBF%2FLXe1fJ8Cfp1l%2F2pEScY2UteUr5y%2BXqxBvjKT7dMQnxQ7FvC2%2FvJu4etrvAm6cexoPQ%3D%3D\" rel=\"nofollow\">https:\/\/cf.jd.com\/pages\/viewp&#8230;<\/a> <em>\uff09<\/em><\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062562\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<p>\u6d41\u7a0b\u56fe<\/p>\n<h4><strong>2.2\u3001\u6838\u5fc3api\u4ecb\u7ecd<\/strong><\/h4>\n<p>ASM\u6846\u67b6\u4e2d\u7684\u6838\u5fc3\u7c7b\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a<\/p>\n<p>\u2460 ClassReader:\u8be5\u7c7b\u7528\u6765\u89e3\u6790\u7f16\u8bd1\u8fc7\u7684class\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/p>\n<p>\u2461 ClassWriter:\u8be5\u7c7b\u7528\u6765\u91cd\u65b0\u6784\u5efa\u7f16\u8bd1\u540e\u7684\u7c7b\uff0c\u6bd4\u5982\u8bf4\u4fee\u6539\u7c7b\u540d\u3001\u5c5e\u6027\u4ee5\u53ca\u65b9\u6cd5\uff0c\u751a\u81f3\u53ef\u4ee5\u751f\u6210\u65b0\u7684\u7c7b\u7684\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/p>\n<p>\u2462 ClassAdapter:\u8be5\u7c7b\u4e5f\u5b9e\u73b0\u4e86ClassVisitor\u63a5\u53e3\uff0c\u5b83\u5c06\u5bf9\u5b83\u7684\u65b9\u6cd5\u8c03\u7528\u59d4\u6258\u7ed9\u53e6\u4e00\u4e2aClassVisitor\u5bf9\u8c61\u3002<\/p>\n<p>\u4e0b\u9762\u4f1a\u5217\u4e3e\u6bcf\u4e2a\u6838\u5fc3\u7c7b\u7684\u51e0\u4e2a\u6838\u5fc3api\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u540e\u9762\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u76f4\u63a5\u67e5\u770basm\u7684\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<h5><strong>2.2.1\u3001ClassReader<\/strong><\/h5>\n<h6><strong>\u6784\u9020\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528 <strong>\uff1a<\/strong> <strong>\u83b7\u53d6Class\u6587\u4ef6\uff0c\u8f93\u5165\u6e90\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u5b57\u8282\u6d41\uff0cIO\u6d41\u6216\u8005\u76f4\u63a5\u52a0\u8f7dClass\u3002<\/strong><\/p>\n<p>\u793a\u4f8b <strong>\uff1a<\/strong> <strong>public ClassReader(final InputStream inputStream)<\/strong><\/p>\n<h6><strong>accept\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u89e3\u6790<\/strong><a href=\"https:\/\/link.segmentfault.com\/?enc=L5lnrLiFs3qUkpIy8y2uuQ%3D%3D.qlY1aHXBYHHwTVhKpPKGwHh9LLq%2BrpsqO%2F%2FTE193%2F2NQxnze0DhojP%2FF62ENeOJnCcng%2F8CewsnFqP2PeftA7lPuxaVYbfjXpbAlt%2B0dVMillGAatNMwtpzxnKF6mlE9\" rel=\"nofollow\"><strong>\u5b57\u8282\u7801<\/strong><\/a><strong>\u4e2d\u5e38\u91cf\u6c60\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20<\/strong><\/p>\n<p>\u793a\u4f8b\uff1a<strong>public void accept(final ClassVisitor classVisitor, final int parsingOptions)<\/strong><\/p>\n<p>\u91cd\u8981\u53c2\u6570\u89e3\u6790\uff1a<strong>parsingOptions\uff08\u7528\u4e8e\u8df3\u8fc7\u8bfb\u53d6\u5b57\u8282\u7801\u65f6\u7684\u4e00\u4e9b\u4fe1\u606f\u9009\u9879\uff0c\u6709\u4ee5\u4e0b\u56db\u79cd\u9009\u62e9\u53ef\u4ee5\u9009\u62e9)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>SKIP_CODE<\/strong><\/th>\n<th>\u8868\u793a\u8df3\u8fc7\u4ee3\u7801\u626b\u63cf\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u53ea\u662f\u7c7b\u7684\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u3002<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>SKIP_DEBUG<\/strong><\/td>\n<td>\u8df3\u8fc7\u8c03\u8bd5\u4fe1\u606f\uff0cClassReader\u4e0d\u4f1a\u53bb\u8bbf\u95ee\u8c03\u8bd5\u4fe1\u606f\u3002 \u5982\u679c\u8bbe\u7f6e\u4e86\u8fd9\u4e2a\u6807\u5fd7\uff0c\u8fd9\u4e9b\u5c5e\u6027\u65e2\u4e0d\u4f1a\u88ab\u89e3\u6790\u4e5f\u4e0d\u4f1a\u88ab\u8bbf\u95ee(\u4f8b\u5982ClassVisitor.visitSource,MethodVisitor.visitLocalVariable, MethodVisitor.visitLineNumber MethodVisitor.visitParameter)\u3002 \u8fd9\u4e2a\u4f1a\u6bd4\u8f83\u5e38\u7528\uff0c\u5f53\u4f60\u4e0d\u9700\u8981\u4e0a\u9762\u8fd9\u4e9b\u65b9\u6cd5\u65f6\u5019\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong>SKIP_FRAMES<\/strong><\/td>\n<td>\u8df3\u8fc7\u5806\u6808\u6620\u5c04\u5e27.\u5982\u679c\u8bbe\u7f6e\u4e86\u8fd9\u4e2a\u6807\u5fd7\uff0c\u8fd9\u4e9b\u5c5e\u6027\u65e2\u4e0d\u4f1a\u88ab\u89e3\u6790\u4e5f\u4e0d\u4f1a\u88ab\u8bbf\u95ee\uff08\u4f8b\u5982\uff1aMethodVisitor.visitFrame\uff09.\u8fd9\u4e2a\u6807\u5fd7\u5f53ClassWriter.COMPUTE_FRAME\u9009\u9879\u88ab\u4f7f\u7528\u65f6\uff0c\u5b83\u4f1a\u907f\u514d\u8bbf\u95ee\u5c06\u88ab\u5ffd\u7565\u5e76\u4ece\u5934\u91cd\u65b0\u8ba1\u7b97\u7684\u5e27\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong>EXPAND_FRAMES<\/strong><\/td>\n<td>\u7528\u6765\u5c55\u5f00\u5806\u6808\u6620\u5c04\u5e27\u7684\u6807\u5fd7\uff0c\u4f1a\u964d\u4f4e\u6027\u80fd\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>2.2.2\u3001ClassWriter<\/strong><\/h5>\n<h6><strong>\u6784\u9020\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u7528\u6765\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027<\/strong><\/p>\n<p>\u793a\u4f8b\uff1a<strong>public ClassWriter(final int flags)<\/strong><\/p>\n<p>\u91cd\u8981\u53c2\u6570\u89e3\u6790\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>flag == 0<\/th>\n<th>\u81ea\u5df1\u8ba1\u7b97\u6808\u5e27\u548c\u5c40\u90e8\u53d8\u91cf\u4ee5\u53ca\u64cd\u4f5c\u6570\u5806\u6808\u7684\u5927\u5c0f \uff0c\u4e5f\u5c31\u662f\u4f60\u8981\u81ea\u5df1\u8c03\u7528visitmax\u548cvisitFrame\u65b9\u6cd5\u3002<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>flag == <strong>ClassWriter. COMPUTE_MAXS<\/strong><\/td>\n<td>\u5c40\u90e8\u53d8\u91cf\u548c\u64cd\u4f5c\u6570\u5806\u6808\u90e8\u5206\u7684\u5927\u5c0f\u4f1a\u4e3a\u4f60\u8ba1\u7b97\uff0c\u8fd8\u9700\u8981\u8c03\u7528visitFrame\u65b9\u6cd5\u8bbe\u7f6e\u6808\u5e27\u3002<\/td>\n<\/tr>\n<tr>\n<td>flag == <strong>ClassWriter.COMPUTE_FRAMES<\/strong><\/td>\n<td>\u6240\u6709\u7684\u5185\u5bb9\u90fd\u662f\u81ea\u52a8\u8ba1\u7b97\u7684\u3002 \u4f60\u4e0d\u5fc5\u8c03\u7528visitFrame\u548cvisitmax<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6><strong>visit\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u7528\u6765\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027<\/strong><\/p>\n<p>\u793a\u4f8b <strong>\uff1a<\/strong> public final void visit(int version, int access, String name, String signature, String superName, String[] interfaces)<\/p>\n<p>\u91cd\u8981\u53c2\u6570\u89e3\u6790\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>version<\/th>\n<th>Java\u7248\u672c\u53f7\uff0c\u4f8b\u5982 <strong>V1_8<\/strong> \u4ee3\u8868<strong>Java 8<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>access<\/td>\n<td>Class\u8bbf\u95ee\u6743\u9650\uff0c\u4e00\u822c\u9ed8\u8ba4\u90fd\u662f **ACC_PUBLIC<\/td>\n<td>ACC_SUPER**<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>Class\u6587\u4ef6\u540d\uff0c\u4f8b\u5982\uff1a<strong>asm\/User<\/strong>\uff0c\u5305\u540d\u52a0\u7c7b\u540d<\/td>\n<\/tr>\n<tr>\n<td>signature<\/td>\n<td>\u7c7b\u7684\u7b7e\u540d\uff0c\u9664\u975e\u4f60\u662f\u6cdb\u578b\u7c7b\u6216\u8005\u5b9e\u73b0\u6cdb\u578b\u63a5\u53e3\uff0c\u4e00\u822c\u9ed8\u8ba4null\u3002<\/td>\n<\/tr>\n<tr>\n<td>superName<\/td>\n<td>\u7ee7\u627f\u7684\u7c7b\uff0c\u5f88\u660e\u663e\u6240\u6709\u7c7b\u9ed8\u8ba4\u7ee7\u627fObject\u3002\u4f8b\u5982\uff1a<strong>java\/lang\/Object \uff0c\u5982\u679c\u662f\u7ee7\u627f\u81ea\u5df1\u5199\u7684\u7c7bAnimal\uff0c\u90a3\u5c31\u662f asm\/Animal<\/strong><\/td>\n<\/tr>\n<tr>\n<td>interfaces<\/td>\n<td>\u5b9e\u73b0\u7684\u63a5\u53e3\uff0c\u4f8b\u5982\u5b9e\u73b0\u81ea\u5df1\u5199\u7684\u63a5\u53e3<strong>IPrint\uff0c<\/strong> \u90a3\u5c31\u662f<strong>new String[]{&#8220;asm\/IPrint&#8221;}<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6><strong>visitMethod\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u7528\u6765\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u793a\u4f8b <strong>\uff1a<\/strong> <strong>public final MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions)<\/strong><\/p>\n<p>\u91cd\u8981\u53c2\u6570\u89e3\u6790\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>access<\/th>\n<th>\u65b9\u6cd5\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u4e5f\u5c31\u662fpublic\uff0cprivate\u7b49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>name<\/td>\n<td>\u65b9\u6cd5\u540d: \u5728Class\u4e2d\uff0c\u6709\u4e24\u4e2a\u7279\u6b8a\u65b9\u6cd5\u540d\u3002&lt;init&gt;\u548c&lt;clinit&gt;\uff0c\u5176\u4ed6\u7684\u65b9\u6cd5\u90fd\u662f\u6b63\u5e38\u5bf9\u5e94Java\u65b9\u6cd5\u540d\u3002 &lt;init&gt;\u4ee3\u8868\u7684\u662f\u7c7b\u7684\u5b9e\u4f8b\u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\uff0c\u4e5f\u5c31\u662fnew \u4e00\u4e2a\u7c7b\u65f6\uff0c\u80af\u5b9a\u4f1a\u8c03\u7528\u7684\u65b9\u6cd5\u3002 &lt;clinit&gt;\u4ee3\u8868\u7684\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4e0d\u540c\u4e8e&lt;init&gt;\uff0c\u5b83\u4e0d\u662f\u663e\u793a\u8c03\u7528\u7684\u3002\u56e0\u4e3aJava\u865a\u62df\u673a\u4f1a\u81ea\u52a8\u8c03\u7528&lt;clinit&gt;\uff0c\u5e76\u4e14\u4fdd\u8bc1\u5728\u5b50\u7c7b\u7684&lt;clinit&gt;\u524d\u8c03\u7528\u7236\u7c7b\u7684&lt;clinit&gt;\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cJava\u865a\u62df\u673a\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u88ab\u6267\u884c&lt;clinit&gt;\u65b9\u6cd5\u7684\u80af\u5b9a\u662fjava.lang.Object\u3002 \u6ce8\u610f\uff1a&lt;init&gt;\u548c&lt;clinit&gt;\u6267\u884c\u7684\u65f6\u673a\u4e0d\u4e00\u6837\uff0c&lt;clinit&gt;\u7684\u65f6\u673a\u65e9\u4e8e&lt;init&gt;\uff0c&lt;clinit&gt;\u662f\u5728\u7c7b\u88ab\u52a0\u8f7d\u9636\u6bb5\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\u8c03\u7528&lt;clinit&gt;\u65b9\u6cd5\uff0c\u800c&lt;init&gt;\u65b9\u6cd5\u7684\u8c03\u7528\u662f\u5728new\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u7684\u65f6\u5019\u3002<\/td>\n<\/tr>\n<tr>\n<td>descriptor<\/td>\n<td>\u65b9\u6cd5\u7684\u63cf\u8ff0\u7b26\uff0c\u5c31\u662f<a href=\"https:\/\/link.segmentfault.com\/?enc=k3Js5%2FeZO46IFp%2FSNhLi3Q%3D%3D.vKV9l2HreGfOeeY0JN7L230LZ7CXUuamoKySOmZUfTRC%2F%2FlQaEIBHd2okd1IWTyeMKcmISTKDFiIBz9cO0XyQaKZVhMV1iawcO08a6e7qUd4%2FlfNNIt8w2Ko9E9AuXp%2F\" rel=\"nofollow\">\u5b57\u8282\u7801<\/a>\u5bf9\u4ee3\u7801\u4e2d\u65b9\u6cd5\u7684\u5f62\u53c2\u548c\u8fd4\u56de\u503c\u7684\u4e00\u4e2a\u63cf\u8ff0\u3002\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u4e00\u4e00\u5bf9\u5e94\u7684\u6a21\u677f<\/td>\n<\/tr>\n<tr>\n<td>signature<\/td>\n<td>\u65b9\u6cd5\u7b7e\u540d\uff0c\u9664\u975e\u65b9\u6cd5\u7684\u53c2\u6570\u3001\u8fd4\u56de\u7c7b\u578b\u548c\u5f02\u5e38\u4f7f\u7528\u4e86\u6cdb\u578b\uff0c\u5426\u5219\u4e00\u822c\u4e3a null\u3002<\/td>\n<\/tr>\n<tr>\n<td>exceptions<\/td>\n<td>\u65b9\u6cd5\u4e0a\u7684\u5f02\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6><strong>visitField\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf<\/strong><\/p>\n<p>\u793a\u4f8b\uff1a<strong>public final FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value)<\/strong><\/p>\n<p>\u91cd\u8981\u53c2\u6570\u89e3\u6790\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>access<\/th>\n<th>\u53d8\u91cf\u7684\u8bbf\u95ee\u6743\u9650\uff0c\uff0c\u4e5f\u5c31\u662fpublic\uff0cprivate\u7b49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>name<\/td>\n<td>\u53d8\u91cf\u540d<\/td>\n<\/tr>\n<tr>\n<td>descriptor<\/td>\n<td>\u53d8\u91cf\u7684\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>signature<\/td>\n<td>\u53d8\u91cf\u7684\u7b7e\u540d\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u6cdb\u578b\u5219\u4e3anull<\/td>\n<\/tr>\n<tr>\n<td>value<\/td>\n<td>\u53d8\u91cf\u7684\u521d\u59cb\u503c\u3002\u8fd9\u4e2a\u5b57\u6bb5\u4ec5\u4f5c\u7528\u4e8e\u88abfinal\u4fee\u9970\u7684\u5b57\u6bb5\uff0c\u6216\u8005\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u3002\u5176\u4ed6\u9ed8\u8ba4\u4e3anull\uff0c\u53d8\u91cf\u7684\u8d4b\u503c\u662f\u901a\u8fc7<strong>MethodVisitor<\/strong> \u7684 <strong>visitFieldInsn\u65b9\u6cd5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6><strong>visitEnd\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u7528\u6765\u901a\u77e5Class\u5df2\u7ecf\u4f7f\u7528\u5b8c<\/strong><\/p>\n<p>\u793a\u4f8b\uff1a<strong>public final void visitEnd()<\/strong><\/p>\n<h6><strong>toByteArray\u65b9\u6cd5<\/strong><\/h6>\n<p>\u4f5c\u7528\uff1a<strong>\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u793a\u4f8b\uff1a<strong>public byte[] toByteArray()<\/strong><\/p>\n<h5><strong>2.2.3\u3001ClassVisiter<\/strong><\/h5>\n<p>\u770b\u540d\u5b57\u5c31\u80fd\u770b\u51fa\u6765\uff0c\u8fd9\u662f\u4e00\u4e2a\u5bf9Class\u6587\u4ef6\u8fdb\u884c\u89c2\u5bdf\uff08\u626b\u63cf\uff09\u7684\u5de5\u5177\u7c7b\u3002\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u80fd\u5bf9\u5176\u8fdb\u884c\u7ee7\u627f\u91cd\u5199\u3002\u5e76\u4e14ClassVisitor\u6240\u6709\u7684\u8c03\u7528\u90fd\u662f\u7531ClassReader\u6765\u8fdb\u884c\u56de\u8c03\uff0c\u5c31\u662f\u6211\u4eec\u524d\u9762accept\u65b9\u6cd5\u3002\u800c\u8fd9\u4e2a\u65b9\u6cd5\u91cc\uff0c\u6267\u884c\u4e86\u5bf9ClassVisitor\u6e90\u7801\u626b\u63cf\u7684\u56de\u8c03\u3002\u5982\u4e0b\u56fe\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062563\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h6><strong>\u6784\u9020\u65b9\u6cd5<\/strong><\/h6>\n<p>\u793a\u4f8b\uff1a<strong>protected ClassVisitor(final int api, final ClassVisitor classVisitor)<\/strong><\/p>\n<p>\u53c2\u6570\u89e3\u91ca\uff1a<\/p>\n<p>api\uff1a<strong>\u4ee3\u8868\u662fASM API\u7684\u7248\u672c \uff0c\u9ed8\u8ba4\u586b\u6700\u65b0\uff08ASM9\uff09\u5373\u53ef\u3002\u7f16\u5199\u4ee3\u7801\u548c\u4ee3\u7801\u8bfb\u53d6\u7684\u7248\u672c\u53f7\u6700\u597d\u4fdd\u6301\u4e00\u81f4\uff0c\u4e0d\u7136\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u517c\u5bb9\u6027\u7684\u9519\u8bef\u3002<\/strong><\/p>\n<p><strong>\u7531\u4e8e<\/strong><strong>ClassVisitor\u6240\u6709\u7684\u8c03\u7528\u90fd\u662f\u7531ClassReader\u6765\u8fdb\u884c\u56de\u8c03\uff0c\u6240\u4ee5\u5176\u4ed6api\u54b1\u4eec\u4e0d\u505a\u8fc7\u591a\u4ecb\u7ecd\u3002<\/strong><\/p>\n<h4><strong>2.3\u3001ASM\u5b9e\u8df5<\/strong><\/h4>\n<p>\u4e0a\u9762\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86asm\u7684api\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684api\u4f8b\u5982<strong>MethodVisitor\u3001FieldVisitor\u7b49<\/strong>\u5c31\u4e0d\u8fc7\u591a\u4ecb\u7ecd\u4e86\uff0c\u8fd8\u662f\u90a3\u53e5\u8bdd\uff0c\u60f3\u6df1\u5165\u4e86\u89e3\uff0c\u5c31\u770b\u4e00\u4e0b\u5b98\u65b9\u6587\u6863\u3002\u4f46\u662f\u5149\u8bf4\u4e0d\u7ec3\u5047\u628a\u5f0f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u5bf9asm\u505a\u4e00\u756a\u5b9e\u8df5\uff0c\u7531\u6b64\u6765\u4f53\u4f1a\u4ed6\u7684\u529f\u80fd\u4e4b\u5f3a\u5927\u3002<\/p>\n<h5>2.3.1\u3001\u5f15\u5165jar<\/h5>\n<pre><code>&lt;dependency&gt;    \n    &lt;groupId&gt;org.ow2.asm&lt;\/groupId&gt;    \n    &lt;artifactId&gt;asm&lt;\/artifactId&gt;    \n    &lt;version&gt;9.4&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h5><strong>2.3.2\u3001\u751f\u6210\u7c7b<\/strong><\/h5>\n<p>\u6211\u4eec\u81ea\u5b9a\u4e49\u751f\u6210\u4e00\u4e2aPeople\u7684\u7c7b<\/p>\n<pre><code>package com.test.excel.asm;\n\nimport org.objectweb.asm.ClassWriter;\nimport org.objectweb.asm.FieldVisitor;\nimport org.objectweb.asm.MethodVisitor;\n\nimport java.io.FileOutputStream;\n\nimport static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;\nimport static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_MAXS;\nimport static jdk.internal.org.objectweb.asm.Opcodes.*;\n\n\/**\n * \u6587\u4ef6\u63cf\u8ff0\n *\n * @author dzl\n * @date 2022-11-27 18:49\n *\/\npublic class AsmTest {\n\n    public static void main(String[] args) throws Exception {\n        testCreateAClass();\n    }\n    public static void testCreateAClass()throws Exception{\n        \/\/\u65b0\u5efa\u4e00\u4e2a\u7c7b\u751f\u6210\u5668\uff0cCOMPUTE_FRAMES\uff0cCOMPUTE_MAXS\u8fd92\u4e2a\u53c2\u6570\u80fd\u591f\u8ba9asm\u81ea\u52a8\u66f4\u65b0\u64cd\u4f5c\u6570\u6808\n        ClassWriter cw = new ClassWriter(COMPUTE_FRAMES|COMPUTE_MAXS);\n        \/\/\u751f\u6210\u4e00\u4e2apublic\u7684\u7c7b\uff0c\u7c7b\u8def\u5f84\u662fcom.study.Human\n        cw.visit(V1_8, ACC_PUBLIC,&quot;com\/test\/excel\/asm\/People&quot;,null,&quot;java\/lang\/Object&quot;,null);\n        \/\/\u751f\u6210\u9ed8\u8ba4\u7684\u6784\u9020\u65b9\u6cd5\uff1a public People()\n        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,&quot;&lt;init&gt;&quot;,&quot;()V&quot;,null,null);\n        mv.visitVarInsn(ALOAD,0);\n        mv.visitMethodInsn(INVOKESPECIAL,&quot;java\/lang\/Object&quot;,&quot;&lt;init&gt;&quot;,&quot;()V&quot;,false);\n        mv.visitInsn(RETURN);\n        mv.visitMaxs(0,0);\/\/\u66f4\u65b0\u64cd\u4f5c\u6570\u6808\n        mv.visitEnd();\/\/\u4e00\u5b9a\u8981\u6709visitEnd\n\n        \/\/\u751f\u6210\u6210\u5458\u53d8\u91cf\n        \/\/1.\u751f\u6210String\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf:private String name;\n        FieldVisitor fv= cw.visitField(ACC_PRIVATE,&quot;name&quot;,&quot;Ljava\/lang\/String;&quot;,null,null);\n        fv.visitEnd();\/\/\u4e0d\u8981\u5fd8\u8bb0end\n        \/\/2.\u751f\u6210Long\u7c7b\u578b\u6210\u5458\uff1aprivate long age\n        fv=cw.visitField(ACC_PRIVATE,&quot;age&quot;,&quot;J&quot;,null,null);\n        fv.visitEnd();\n\n        \/\/3.\u751f\u6210Int\u7c7b\u578b\u6210\u5458:protected int no\n        fv=cw.visitField(ACC_PROTECTED,&quot;no&quot;,&quot;I&quot;,null,null);\n        fv.visitEnd();\n\n        \/\/4.\u751f\u6210\u9759\u6001\u6210\u5458\u53d8\u91cf\uff1apublic static long score\n        fv=cw.visitField(ACC_PUBLIC + ACC_STATIC,&quot;score&quot;,&quot;J&quot;,null,null);\n\n        \/\/5.\u751f\u6210\u5e38\u91cf\uff1apublic static final String real_name = &quot;Sand\u54e5&quot;\n        fv=cw.visitField(ACC_PUBLIC+ACC_STATIC+ACC_FINAL,&quot;real_name&quot;,&quot;Ljava\/lang\/String;&quot;,null,&quot;Sand\u54e5&quot;);\n        fv.visitEnd();\n\n        \/\/6.\u751f\u6210\u6210\u5458\u65b9\u6cd5greet\n        mv=cw.visitMethod(ACC_PUBLIC,&quot;greet&quot;,&quot;(Ljava\/lang\/String;)I&quot;,null,null);\n        mv.visitCode();\n        mv.visitIntInsn(ALOAD,0);\n        mv.visitIntInsn(ALOAD,1);\n\n        \/\/6.1 \u8c03\u7528\u9759\u6001\u65b9\u6cd5 System.out.println(&quot;Hello&quot;);\n        mv.visitFieldInsn(GETSTATIC,&quot;java\/lang\/System&quot;,&quot;out&quot;,&quot;Ljava\/io\/PrintStream;&quot;);\n\/\/        mv.visitLdcInsn(&quot;Hello&quot;);\/\/\u52a0\u8f7d\u5b57\u7b26\u5e38\u91cf\n        mv.visitIntInsn(ALOAD,1);\/\/\u52a0\u8f7d\u5f62\u53c2\n        mv.visitMethodInsn(INVOKEVIRTUAL,&quot;java\/io\/PrintStream&quot;,&quot;println&quot;,&quot;(Ljava\/lang\/String;)V&quot;,false);\/\/\u6253\u5370\u5f62\u53c2\n        \/\/\u83b7\u53d6\u7c7b\u7684byte\u6570\u7ec4\n        byte[] classByteData=cw.toByteArray();\n        \/\/\u628a\u7c7b\u6570\u636e\u5199\u5165\u5230class\u6587\u4ef6,\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u628a\u8fd9\u4e2a\u7c7b\u6587\u4ef6\u6253\u5305\u4f9b\u5176\u4ed6\u7684\u4eba\u4f7f\u7528\n        FileOutputStream out = new FileOutputStream(AsmTest.class.getResource(&quot;\/com\/test\/excel\/asm\/&quot;).getPath() + &quot;People.class&quot;);\n        out.write(classByteData);\n        out.close();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062564\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h5><strong>2.3.3\u3001\u4fee\u6539\u5df2\u5b58\u5728\u7684\u7c7b<\/strong><\/h5>\n<p>\u5047\u5982\u6211\u4eec\u73b0\u5728\u6709\u5982\u4e0b\u7c7b\uff0c\u6211\u4eec\u7528asm\u505a\u5982\u4e0b\u51e0\u4e2a\u4fee\u6539\uff1a1.\u589e\u52a0\u4e86\u4e00\u4e2aphone\u5b57\u6bb5 2.\u5220\u9664testA\u65b9\u6cd5 3.\u5c06testC\u65b9\u6cd5\u6539\u6210protected 4.\u65b0\u589e\u4e00\u4e2agetPhone\u65b9\u6cd5\u3002<\/p>\n<pre><code>\/**\n * \u6587\u4ef6\u63cf\u8ff0\n *\n * @author dzl\n * @date 2022-11-27 19:14\n *\/\npublic class ModifyFunction {\n\n    private int a;\n\n    public void testA(){\n        System.out.println(&quot;I am A&quot;);\n    }\n\n    public void testB(){\n        System.err.println(&quot;===&gt;I am B&quot;);\n    }\n\n    public int testC(){\n        return a;\n    }\n}<\/code><\/pre>\n<p>\u5229\u7528asm\u4fee\u6539\u8be5\u7c7b<\/p>\n<pre><code>package com.test.excel.asm;\n\nimport org.objectweb.asm.*;\n\nimport java.io.FileOutputStream;\n\nimport static jdk.internal.org.objectweb.asm.Opcodes.*;\nimport static org.objectweb.asm.Opcodes.ASM9;\n\n\/**\n * \u6587\u4ef6\u63cf\u8ff0\n *\n * @author dzl\n * @date 2022-11-27 18:49\n *\/\npublic class AsmTest {\n\n    public static void main(String[] args) throws Exception {\n        testModifyCalss();\n    }\n    private static void testModifyCalss()throws Exception{\n        ClassReader cr = new ClassReader(&quot;com.test.excel.asm.ModifyFunction&quot;);\n        final ClassWriter cw=new ClassWriter(cr,0);\n\/\/        cr.accept(cw, 0);\/\/\u53ef\u4ee5\u76f4\u63a5\u63a5\u53d7\u4e00\u4e2awriter,\u5b9e\u73b0\u590d\u5236\n        cr.accept(new ClassVisitor(ASM9,cw) {\/\/\u63a5\u53d7\u4e00\u4e2a\u5e26classWriter\u7684visitor\uff0c\u5b9e\u73b0\u5b9a\u5236\u5316\u65b9\u6cd5\u62f7\u8d1d\u6216\u8005\u5c5e\u6027\u5220\u9664\u5b57\u6bb5\n            @Override\n            public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {\n                System.out.println(&quot;visit method:&quot;+name+&quot;====&gt; &quot;+descriptor);\n\n                if(&quot;testA&quot;.equals(name)){\/\/\u62f7\u8d1d\u7684\u8fc7\u7a0b\u4e2d\u5220\u9664\u4e00\u4e2a\u65b9\u6cd5\n                    return null;\n                }\n\n                if(&quot;testC&quot;.equals(name)){\/\/\u5c06testC public\u65b9\u6cd5\u53d8\u6210protect\n                    access=ACC_PROTECTED;\n                }\n                return super.visitMethod(access, name, descriptor, signature, exceptions);\n            }\n\n            @Override\n            public void visitEnd() {\n                \/\/\u7279\u522b\u6ce8\u610f\u7684\u662f\uff1a\u8981\u4e3a\u7c7b\u589e\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u653e\u5230visitEnd\u4e2d\uff0c\u907f\u514d\u7834\u574f\u4e4b\u524d\u5df2\u7ecf\u6392\u5217\u597d\u7684\u7c7b\u7ed3\u6784\uff0c\u5728\u7ed3\u5c3e\u6dfb\u52a0\u65b0\u7ed3\u6784\n                \/\/\u589e\u52a0\u4e00\u4e2a\u5b57\u6bb5\uff08\u6ce8\u610f\u4e0d\u80fd\u91cd\u590d\uff09,\u6ce8\u610f\u6700\u540e\u90fd\u8981visitEnd\n                FieldVisitor fv = cv.visitField(ACC_PUBLIC, &quot;phone&quot;, &quot;Ljava\/lang\/String;&quot;, null, null);\n                fv.visitEnd();\/\/\u4e0d\u80fd\u7f3a\u5c11visitEnd\n                \/\/\u589e\u52a0\u4e00\u4e2a\u65b9\u6cd5\n                MethodVisitor mv=cv.visitMethod(ACC_PUBLIC,&quot;getPhone&quot;,&quot;()Ljava\/lang\/String;&quot;,null,null);\n                mv.visitCode();\n                mv.visitVarInsn(ALOAD, 0);\n                mv.visitFieldInsn(GETFIELD,&quot;com\/test\/excel\/asm\/ModifyFunction&quot;,&quot;phone&quot;,&quot;Ljava\/lang\/String;&quot;);\n                mv.visitInsn(IRETURN);\n                mv.visitMaxs(1, 1);\n                mv.visitEnd();\/\/\u4e0d\u80fd\u7f3a\u5c11visitEnd\n                super.visitEnd();\/\/\u6ce8\u610f\u539f\u672c\u7684visiEnd\u4e0d\u80fd\u5c11\n            }\n        },0);\n        \/\/\u6307\u5b9a\u65b0\u751f\u6210\u7684class\u8def\u5f84\u7684\u751f\u6210\u4f4d\u7f6e,\u8fd9\u4e2a\u8def\u5f84\u4f60\u53ef\u4ee5\u968f\u4fbf\u6307\u5b9a\n        FileOutputStream out = new FileOutputStream(AsmTest.class.getResource(&quot;\/com\/test\/excel\/asm\/&quot;).getPath() + &quot;ModifyFunction.class&quot;);\n        out.write(cw.toByteArray());\n        out.close();\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043062565\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<\/p>\n<h5><strong>2.3.4\u3001\u5b9e\u73b0\u65b9\u6cd5\u6ce8\u5165<\/strong><\/h5>\n<p>\u5b9e\u73b0\u65b9\u6cd5\u6ce8\u5165\u7c7b\u4f3c\u4e8e\u6211\u4eec\u7684aop\u529f\u80fd\uff0c\u672c\u7bc7\u5c31\u4e0d\u505a\u8fc7\u591a\u4ecb\u7ecd\u4e86\uff0c\u611f\u5174\u8da3\u7684\u5927\u4f6c\u53ef\u4ee5\u53c2\u8003\u4e0a\u4e00\u7bc7\u6587\u7ae0\u7684demo\u3002<\/p>\n<h3><strong>\u4e09\u3001\u603b\u7ed3<\/strong><\/h3>\n<p>\u5199\u5230\u8fd9\u91cc\uff0c\u5fc3\u91cc\u90fd\u611f\u89c9\u5982\u91ca\u91cd\u8d1f\u4e86\u4e00\u4e0b\uff0c\u56e0\u4e3a\u6b63\u6587\u603b\u7b97\u662f\u5199\u5b8c\u4e86\u3002\u4f5c\u4e3a\u672c\u7cfb\u5217\u5448\u4e0a\u542f\u4e0b\u7684\u7b2c\u4e8c\u7bc7\uff0c\u67e5\u4e86\u5f88\u591a\u8d44\u6599\uff0c\u770b\u4e86\u5f88\u591a\u6587\u7ae0\uff0c\u518d\u5199\u5206\u4eab\u7684\u540c\u65f6\uff0c\u81ea\u8eab\u4e5f\u5b66\u5230\u4e86\u5f88\u591a\u4e1c\u897f\u3002\u540c\u65f6\uff0c\u5982\u679c\u6587\u7ae0\u4e2d\u6709\u4e0d\u8db3\u6216\u8005\u63cf\u8ff0\u4e0d\u51c6\u786e\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u53ca\u65f6\u6279\u8bc4\u6307\u6b63\uff0c\u4e07\u5206\u611f\u8c22\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043062555\">https:\/\/segmentfault.com\/a\/1190000043062555<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8463\u5b50\u9f99 \u524d\u8a00 \u8bb0\u5f97\u90a3\u662f2022\u5e74\u79cb\u5929\u7684\u7b2c\u4e00\u573a\u96e8\uff0c\u6bd42021\u5e74\u6765\u7684\u7a0d\u665a\u4e00\u4e9b\uff0c\u5728\u90a3\u4e2a\u79cb\u96e8\u6726\u80e7\u7684\u4e0b\u5348\uff0c\u6b63\u5728\u5de5\u4f4d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006956","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006956"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006956\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}