{"id":9085,"date":"2022-12-13T17:13:20","date_gmt":"2022-12-13T09:13:20","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9085"},"modified":"2022-12-13T17:13:20","modified_gmt":"2022-12-13T09:13:20","slug":"java%e5%8f%8d%e5%b0%84%e8%b0%83%e7%94%a8get-set%e6%96%b9%e6%b3%95%ef%bc%8c%e4%bd%a0%e8%bf%98%e5%9c%a8%e6%8b%bc%e6%8e%a5%e6%96%b9%e6%b3%95%e5%90%8d%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9085","title":{"rendered":"Java\u53cd\u5c04\u8c03\u7528get\/set\u65b9\u6cd5\uff0c\u4f60\u8fd8\u5728\u62fc\u63a5\u65b9\u6cd5\u540d\u5417\uff1f"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6700\u65b0\u5de5\u4f5c\u4e2d\uff0c\u9047\u5230\u4e86\u901a\u8fc7\u53cd\u5c04\u8c03\u7528get\/set\u65b9\u6cd5\u7684\u5730\u65b9\uff0c\u867d\u7136\u53cd\u5c04\u7684\u6027\u80fd\u4e0d\u662f\u5f88\u597d\uff0c\u4f46\u662f\u76f8\u6bd4\u8f83\u4e8e\u786c\u7f16\u7801\u7684\u4e0d\u6613\u6269\u5c55\uff0cgetDeclareFields\u53ef\u4ee5\u62ff\u5230\u6240\u6709\u7684\u6210\u5458\u53d8\u91cf\uff0c\u540e\u7eed\u6dfb\u52a0\u6216\u5220\u9664\u6210\u5458\u53d8\u91cf\u65f6\uff0c\u4e0d\u7528\u4fee\u6539\u4ee3\u7801\uff0c\u4e14\u5e94\u7528\u6b21\u6570\u53ea\u5728\u4fee\u6539\u6570\u636e\u65f6\u4f7f\u7528\uff0c\u6545\u727a\u7272\u4e00\u4e9b\u6027\u80fd\u63d0\u9ad8\u6269\u5c55\u6027<\/p>\n\n\n\n<p><strong>\u4f20\u7edf\u7684\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u89c1\u8fc7\u5f88\u591a\u4eba\u901a\u8fc7\u53cd\u5c04\u8c03\u7528get\/set\u65b9\u6cd5\u90fd\u662f\u901a\u8fc7\u83b7\u53d6\u5c5e\u6027\u7684name\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u622a\u53d6\u5c06\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u518d\u62fc\u4e0aget\/set\u6765\u505a<\/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=\"\">String fieldName = field.getName();\nString getMethodName = \"get\" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);<\/pre>\n\n\n\n<p>\u8fd8\u6709\u7a0d\u5fae\u597d\u4e00\u70b9\u7684\u540c\u5b66\uff0c\u901a\u8fc7fieldName\u8f6c\u6210\u5b57\u7b26\u6570\u7ec4\uff0c\u9996\u4e2a\u5b57\u7b26-32\u6765\u907f\u514d\u5b57\u7b26\u4e32\u622a\u53d6\u7684<\/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=\"\">String fieldName = field.getName();\nchar[] chars = fieldName.toCharArray();\nchars[0] = (char)(chars[0] - 32);\nString getMethodName = \"get\" + new String(chars);<\/pre>\n\n\n\n<p>\u8bda\u7136\uff0c\u6211\u89c9\u5f97\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u6709\u6ca1\u6709\u9047\u5230\u8fc7\uff0c\u751f\u6210\u7684get\/set\u65b9\u6cd5\u5e76\u4e0d\u662f\u5df2get\/set\u5f00\u5934\u7684\uff0c\u800c\u662f\u4ee5is\u5f00\u5934\u7684\uff0c\u6bd4\u5982boolean\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u53bb\u5224\u65ad\u5c5e\u6027\u7684\u7c7b\u578b\uff0c\u7136\u540e\u7528\u4e0d\u540c\u7684\u524d\u7f00\u6765\u62fc\u63a5get\/set\u65b9\u6cd5\u540d\u3002\u5176\u5b9e\uff0c\u5728jdk\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86\u8fd9\u6837\u7684\u5de5\u5177\u7c7b<\/p>\n\n\n\n<p><strong>Introspector\u548cPropertyDescriptor<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5173\u4e8e\u8fd9\u4e24\u4e2a\u7c7b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u8bf4\u4e86\uff0c\u7b80\u5355\u7684\u7406\u89e3\u5c31\u662f\u5bf9\u8c61\u4fe1\u606f\u7684\u63cf\u8ff0\uff0c\u91cc\u9762\u63d0\u4f9b\u4e86\u4e00\u4e9bAPI\u65b9\u4fbf\u6211\u4eec\u62ff\u5230\u5bf9\u8c61\u7684\u4fe1\u606f<\/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=\"\">BeanInfo beanInfo;\ntry {\n    beanInfo = Introspector.getBeanInfo(template.getClass());\n} catch (IntrospectionException e) {\n    log.info(\"xxxxxxxxxxxxxxxx\", e);\n    return null;\n}\n\nList&lt;PropertyDescriptor> descriptors = Arrays.stream(beanInfo.getPropertyDescriptors()).filter(p -> {\n    String name = p.getName();\n    \/\/\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u4fee\u6539\u7684\u5c5e\u6027\n    return !\"class\".equals(name) &amp;&amp; !\"id\".equals(name);\n}).collect(Collectors.toList());\n\nfor (PropertyDescriptor descriptor : descriptors) {\n    \/\/descriptor.getWriteMethod()\u65b9\u6cd5\u5bf9\u5e94set\u65b9\u6cd5\n    Method readMethod = descriptor.getReadMethod();\n    System.out.println(descriptor.getName());\n    try {\n        Object o = readMethod.invoke(template);\n        System.out.println(o);\n    } catch (IllegalAccessException | InvocationTargetException e) {\n        log.info(\"xxxxxxxxxxxxxxxx\", e);\n        return null;\n    }\n}<\/pre>\n\n\n\n<p>PropertyDescriptor\u7c7b\u63d0\u4f9b\u4e86getReadMethod\u548cgetWriteMethod\uff0c\u5176\u5b9e\u5c31\u662f\u5bf9\u4e8eget\/set\u65b9\u6cd5\uff0c\u81f3\u4e8e\u65b9\u6cd5\u540d\u79f0\u4e0d\u9700\u8981\u6211\u4eec\u6765\u5173\u4e8e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u65b9\u6cd5\u540d\u62fc\u9519\u7684\u60c5\u51b5\u4e86\u3002<\/p>\n\n\n\n<p>\u53e6\u5916PropertyDescriptor\u9664\u4e86\u53ef\u4ee5\u901a\u8fc7Introspector\u83b7\u53d6\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5df1new\u6765\u521b\u5efa\uff0c\u5176\u6784\u9020\u65b9\u6cd5\u8fd8\u662f\u6bd4\u8f83\u5168\u7684<\/p>\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\/12\/\u56fe\u7247-75.png\" alt=\"\" class=\"wp-image-9086\" width=\"404\" height=\"137\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75.png 812w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75-300x102.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75-768x261.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75-230x78.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75-350x119.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-75-480x163.png 480w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/figure><\/div>\n\n\n\n<p>\u901a\u5e38\u4f20\u9012\u4e00\u4e2a\u5c5e\u6027\u7684\u540d\u79f0\u548c\u7c7b\u5bf9\u8c61class\u5c31\u53ef\u4ee5\u4e86<\/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=\"\">List&lt;Field> fields = Arrays.stream(template.getClass().getDeclaredFields()).filter(f -> {\n    String name = f.getName();\n    \/\/\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u4fee\u6539\u7684\u5c5e\u6027\n    return !\"id\".equals(name) &amp;&amp; !\"serialVersionUID\".equals(name);\n}).collect(Collectors.toList());\n\nfor (Field field : fields) {\n    try {\n        PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), template.getClass());\n        Method readMethod = descriptor.getReadMethod();\n        Object o = readMethod.invoke(template);\n        System.out.println(o);\n    } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {\n        e.printStackTrace();\n    }\n}<\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u4e24\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u53ef\u4ee5\u770b\u5230\uff0cIntrospector\u4f1a\u989d\u5916\u6709\u4e00\u4e2aclass\u5c5e\u6027\uff0c\u4f46\u662f\u7c7b\u4f3cserialVersionUID\u4e0d\u4f1a\u7b97\u5728\u5185\uff1b\u800c\u81ea\u5b9a\u4e49PropertyDescriptor\u9700\u8981\u901a\u8fc7\u53cd\u5c04\u62ff\u5230\u6240\u6709\u7684\u5c5e\u6027\uff0c\u867d\u7136\u4e0d\u4f1a\u6709class\u5c5e\u6027\uff0c\u4f46\u662fserialVersionUID\u4f1a\u7b97\u5728\u5185\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u4ee5\u4e3a\u8fd9\u5c31\u662fIntrospector\u7684\u5168\u90e8\u529f\u80fd\uff0c\u90a3\u5c31\u5927\u9519\u7279\u9519\u4e86\u3002Introspector\u4e0d\u540c\u4e8e\u666e\u901a\u7684\u53cd\u5c04\uff0c\u53cd\u5c04\u4e00\u6b21\uff0c\u4e00\u6bb5\u65f6\u95f4\u5185\u53ef\u91cd\u590d\u4f7f\u7528\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u662f\u6c38\u4e45\u5462\uff0c\u770b\u4e0b\u6e90\u7801<\/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=\"\">\/**\n     * Introspect on a Java Bean and learn about all its properties, exposed\n     * methods, and events.\n     * &lt;p>\n     * If the BeanInfo class for a Java Bean has been previously Introspected\n     * then the BeanInfo class is retrieved from the BeanInfo cache.\n     *\n     * @param beanClass  The bean class to be analyzed.\n     * @return  A BeanInfo object describing the target bean.\n     * @exception IntrospectionException if an exception occurs during\n     *              introspection.\n     * @see #flushCaches\n     * @see #flushFromCaches\n     *\/\n    public static BeanInfo getBeanInfo(Class&lt;?> beanClass)\n        throws IntrospectionException\n    {\n        if (!ReflectUtil.isPackageAccessible(beanClass)) {\n            return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();\n        }\n        ThreadGroupContext context = ThreadGroupContext.getContext();\n        BeanInfo beanInfo;\n        synchronized (declaredMethodCache) {\n            beanInfo = context.getBeanInfo(beanClass);\n        }\n        if (beanInfo == null) {\n            beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();\n            synchronized (declaredMethodCache) {\n                context.putBeanInfo(beanClass, beanInfo);\n            }\n        }\n        return beanInfo;\n    }<\/pre>\n\n\n\n<p>\u6ce8\u610f\u4e2d\u95f4\u52a0\u7c97\u6807\u7ea2\u7684\u4ee3\u7801\uff0c\u8fd9\u91cc\u9664\u4e86\u540c\u6b65\u4e4b\u5916\uff0c\u8fd8\u505a\u4e86\u4e00\u4e2a\u672c\u5730\u7684\u7f13\u5b58<\/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=\"\">BeanInfo getBeanInfo(Class&lt;?> type) {\n    return (this.beanInfoCache != null)\n            ? this.beanInfoCache.get(type)\n            : null;\n}<\/pre>\n\n\n\n<p>\u8fd9\u4e2abeanInfoCache \u5176\u5b9e\u662f\u4e00\u4e2aWeakHashMap\uff0c\u6bcf\u6b21gc\u88ab\u56de\u6536\uff0c\u6240\u4ee5\u4e0a\u9762\u8bf4\u4e00\u6bb5\u65f6\u95f4\u5185\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u800c\u4e0d\u662f\u6c38\u4e45\uff0c\u4e5f\u662f\u4e3a\u4e86\u907f\u514dOOM\u5427<\/p>\n\n\n\n<p>\u00a0<strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5927\u6982\u5148\u8bf4\u8fd9\u4e48\u591a\u5427\uff0c\u867d\u7136\u7b97\u4e0d\u4e0a\u4ec0\u4e48\u9ad8\u7ea7\u6280\u672f\uff0c\u4f46\u662f\u80fd\u5c06\u5de5\u4f5c\u4e2d\u9047\u5230\u7684\u5c0f\u95ee\u9898\u89e3\u51b3\u4e5f\u662f\u6210\u957f\u554a\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6700\u65b0\u5de5\u4f5c\u4e2d\uff0c\u9047\u5230\u4e86\u901a\u8fc7\u53cd\u5c04\u8c03\u7528get\/set\u65b9\u6cd5\u7684\u5730\u65b9\uff0c\u867d\u7136\u53cd\u5c04\u7684\u6027\u80fd\u4e0d\u662f\u5f88\u597d\uff0c\u4f46\u662f\u76f8\u6bd4\u8f83\u4e8e\u786c\u7f16\u7801\u7684\u4e0d\u6613 [&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\/9085"}],"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=9085"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9085\/revisions"}],"predecessor-version":[{"id":9088,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9085\/revisions\/9088"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}