{"id":6909,"date":"2022-08-13T16:43:27","date_gmt":"2022-08-13T08:43:27","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6909"},"modified":"2022-08-13T16:43:27","modified_gmt":"2022-08-13T08:43:27","slug":"java%e4%bd%bf%e7%94%a8%e6%b3%a8%e8%a7%a3%e6%a0%a1%e9%aa%8c%e5%af%b9%e8%b1%a1%e5%b1%9e%e6%80%a7%e5%80%bc%e6%95%b0%e6%8d%ae%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6909","title":{"rendered":"java\u4f7f\u7528\u6ce8\u89e3\u6821\u9a8c\u5bf9\u8c61\u5c5e\u6027\u503c\u6570\u636e\u957f\u5ea6"},"content":{"rendered":"\n<p>\u5f00\u53d1\u8fc7\u7a0b\u7531\u4e8e\u7ecf\u5e38\u9700\u8981\u7ec4\u62a5\u6587\uff0c\u6309\u4e00\u5b9a\u7684\u534f\u8bae\u7ec4\u6210\u4ea4\u4e92\u6570\u636e\uff0c\u62a5\u6587\u7684\u6570\u636e\u7531\u591a\u4e2a\u53c2\u6570\u7ec4\u6210\uff0c\u4e14\u53c2\u6570\u7684\u957f\u5ea6\u57fa\u672c\u90fd\u6709\u56fa\u5b9a\u7684\u957f\u5ea6\uff0c\u7136\u540e\u6709\u4e9b\u534f\u8bae\u7684\u53c2\u6570\u4e2a\u6570\u53ef\u80fd\u8fbe\u5230\u51e0\u5341\u4e2a\u4e4b\u591a\uff0c\u4e3a\u4e86\u4fdd\u969c\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u540c\u65f6\u53c8\u4e0d\u505aif(data.length())\u8fd9\u6837\u5927\u91cf\u7684\u4f53\u529b\u6d3b\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u79cd\u4f7f\u7528\u6ce8\u89e3\u6765\u5b8c\u6210\u8fd9\u4e2a\u6821\u9a8c\u7684\u52a8\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u5b9a\u4e49\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Target({ ElementType.FIELD, ElementType.TYPE })\n@Inherited\n@Documented\n@Retention(RetentionPolicy.RUNTIME)\npublic @interface DataLen\n{\n    int value();\n}\n<\/pre>\n\n\n\n<p><strong>\u5b9a\u4e49\u6821\u9a8c\u53c2\u6570\u957f\u5ea6\u7684\u5e2e\u52a9\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.lang.annotation.Annotation;\nimport java.lang.reflect.Field;\n\n\/**\n * \u6821\u9a8c\u6570\u636e\u957f\u5ea6\u7684\u5e2e\u52a9\u7c7b\n * \n * @author lupf\n * \n *\/\npublic class DataLenCheckHelper\n{\n    \/**\n     * \u6821\u9a8c\u6570\u636e\u5c5e\u6027\u81f3\n     * \n     * @param obj\n     * @throws Exception\n     *\/\n    @SuppressWarnings(\"rawtypes\")\n    public static void checkAttributeValueLen(Object obj) throws Exception\n    {\n        if (null != obj)\n        {\n            \/\/ \u5f97\u5230class\n            Class cls = obj.getClass();\n            System.out.println(\"\u6821\u9a8c\u5bf9\u8c61\u4e2d\u53c2\u6570\u7684\u6570\u636e\u957f\u5ea6\u662f\u5426\u7b26\u5408\u8981\u6c42,\u6821\u9a8c\u5bf9\u8c61:\" + cls.getName());\n            \/\/ \u5f97\u5230\u6240\u6709\u5c5e\u6027\n            Field[] fields = cls.getDeclaredFields();\n            for (int i = 0; i &lt; fields.length; i++)\n            {\/\/ \u904d\u5386\n                try\n                {\n                    \/\/ \u5f97\u5230\u5c5e\u6027\n                    Field field = fields[i];\n                    Annotation[] anns = field.getAnnotations();\n                    DataLen dataLen = null;\n                    for (Annotation ann : anns)\n                    {\n                        if (ann instanceof DataLen)\n                            dataLen = (DataLen) ann;\n                    }\n\n                    \/\/ \u5224\u65ad\u8be5\u5c5e\u6027\u662f\u5426\u6709\u6821\u9a8c\u6570\u636e\u957f\u5ea6\u7684\u6ce8\u89e3\n                    if (null != dataLen)\n                    {\n                        \/\/ \u6253\u5f00\u79c1\u6709\u8bbf\u95ee\n                        field.setAccessible(true);\n                        \/\/ \u83b7\u53d6\u5c5e\u6027\n                        String name = field.getName();\n                        \/\/ \u83b7\u53d6\u5c5e\u6027\u503c\n                        Object value = field.get(obj);\n                        \/\/ \u6307\u5b9a\u7684\u957f\u5ea6\n                        int len = dataLen.value();\n                        \/\/ \u6570\u636e\u7684\u957f\u5ea6\n                        int vaLen = 0;\n                        String data = null;\n                        \/\/ \u4e00\u4e2a\u4e2a\u8d4b\u503c\n                        if (null != value &amp;&amp; value instanceof String)\n                        {\n                            data = (String) value;\n                            vaLen = data.length();\n                        }\n\n                        if (vaLen != len)\n                        {\n                            System.out.print(\"\u5bf9\u8c61:\" + cls.getName() + \"\u4e2d\u5b58\u5728\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u53c2\u6570,\u53c2\u6570\u540d:\" + name + \"\u53c2\u6570\u503c:\" + data + \"\u6307\u5b9a\u7684\u6570\u636e\u957f\u5ea6:\" + len + \"\u5b9e\u9645\u957f\u5ea6:\" + vaLen\n                                    + \"\u4e0d\u7b26\u5408\u6761\u4ef6\");\n                            throw new Exception();\n                        }\n                    }\n                }\n                catch (IllegalAccessException e)\n                {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<ul><li><strong>\u5b9a\u4e49\u6d4b\u8bd5\u7c7b<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class TestObj\n{\n@DataLen(5)\npublic String data1;\n\n@DataLen(10)\npublic String data2;\n\npublic TestObj(String data1, String data2)\n{\n    this.data1 = data1;\n    this.data2 = data2;\n}\n}<\/pre>\n\n\n\n<ul><li><strong>main\u65b9\u6cd5\u6d4b\u8bd5<\/strong><\/li><\/ul>\n\n\n\n<p>\u6b63\u5e38<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    public static void main(String[] args)\n    {\n        try\n        {\n            TestObj testObj = new TestObj(\"12345\", \"1122334455\");\n            DataLenCheckHelper.checkAttributeValueLen(testObj);\n        }\n        catch (Exception e)\n        {\n            \/\/ TODO Auto-generated catch block\n        }\n\n    }<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/image.png\" alt=\"\" class=\"wp-image-6910\" width=\"702\" height=\"76\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image.png 816w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-300x32.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-768x83.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-230x25.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-350x38.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-480x52.png 480w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f02\u5e38<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String[] args)\n    {\n        try\n        {\n            TestObj testObj = new TestObj(\"123456\", \"1122334455\");\n            DataLenCheckHelper.checkAttributeValueLen(testObj);\n        }\n        catch (Exception e)\n        {\n            \/\/ TODO Auto-generated catch block\n        }\n\n    }\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/image-1-1024x90.png\" alt=\"\" class=\"wp-image-6911\" width=\"665\" height=\"58\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-1024x90.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-300x26.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-768x68.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-830x73.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-230x20.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-350x31.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1-480x42.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/image-1.png 1360w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/figure><\/div>\n\n\n\n<p>\u5230\u6b64\u5c31\u53ef\u4ee5\u4e00\u52b3\u6c38\u9038\u4e86\uff0c\u4e4b\u540e\u6240\u6709\u7684\u6570\u636e\u5bf9\u8c61\u53ea\u8981\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u628a\u76f8\u5173\u7684\u53c2\u6570\u6307\u5b9a\u957f\u5ea6\uff0c\u90a3\u4e48\u5728\u4e4b\u540e\u7684\u5b9e\u4f8b\u5316\u4e4b\u540e\u5373\u53ef\u5bf9\u53c2\u6570\u7684\u957f\u5ea6\u8fdb\u884c\u6821\u9a8c\uff0c\u53ef\u4ee5\u51cf\u5c11\u5927\u91cf\u7684\u4f53\u529b\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u8fc7\u7a0b\u7531\u4e8e\u7ecf\u5e38\u9700\u8981\u7ec4\u62a5\u6587\uff0c\u6309\u4e00\u5b9a\u7684\u534f\u8bae\u7ec4\u6210\u4ea4\u4e92\u6570\u636e\uff0c\u62a5\u6587\u7684\u6570\u636e\u7531\u591a\u4e2a\u53c2\u6570\u7ec4\u6210\uff0c\u4e14\u53c2\u6570\u7684\u957f\u5ea6\u57fa\u672c\u90fd\u6709\u56fa\u5b9a\u7684\u957f\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6909"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6909"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6909\/revisions"}],"predecessor-version":[{"id":6912,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6909\/revisions\/6912"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}