欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
JavaBean:屬性名大小寫(xiě)與getter方法命名問(wèn)題

JavaBean:屬性名大小寫(xiě)與getter方法命名問(wèn)題

情況如下,Company類(lèi)的對象有一個(gè)叫sAddress的屬性,根據JavaBean Specification,getter和setter分別為getSAddress和setSAddress。但是編譯時(shí)會(huì )報錯:

Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for sAddress in class Company at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)

跟蹤到org.hibernate.property.BasicPropertyAccessor類(lèi)中的getterMethod(Class theClass, String propertyName)方法:

1            2            3            4            5            6            7            8            9            10            11            12            13            14            15            16            17            18            19            20            21            22            23            24            25            26            
private static Method getterMethod(Class theClass, String propertyName) {            Method[] methods = theClass.getDeclaredMethods();            for (int i = 0; i < methods.length; i++) {            // only carry on if the method has no parameters            if (methods[i].getParameterTypes().length == 0) {            String methodName = methods[i].getName();            // try "get"            if (methodName.startsWith("get")) {            String testStdMethod = Introspector.decapitalize(methodName.substring(3));            String testOldMethod = methodName.substring(3);            if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {            return methods[i];            }            }            // if not "get" then try "is"            if (methodName.startsWith("is")) {            String testStdMethod = Introspector.decapitalize(methodName.substring(2));            String testOldMethod = methodName.substring(2);            if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {            return methods[i];            }            }            }            }            return null;            }

getterMethod()對Company類(lèi)中聲明的方法進(jìn)行遍歷,找到與屬性名匹配的方法,即屬性的getter方法。比較分兩部分,第一部分,針對primitive和自定義類(lèi)類(lèi)型的屬性;第二部分,針對boolean類(lèi)型的屬性(由于boolean類(lèi)型屬性的getter方法的特殊性)。

跟蹤發(fā)現,methodName的值為“getSAddress”,propertyName的值為“sAddress”,testOldMethod的值為“SAddress”,testStdMethod的值為“SAddress”。testStdMethod和testOldMethod相同,而它們都不匹配propertyName!

因此,getterMethod()中找不到與屬性sAddress匹配的getter方法,getterMethod()返回null,導致異常。

問(wèn)題出在Introspector.decapitalize()方法。

decapitalize()源碼如下:

1            2            3            4            5            6            7            8            9            10            11            12            13            14            15            16            17            18            19            20            21            22            23            24            25            
/**            * Utility method to take a string and convert it to normal Java variable            * name capitalization.  This normally means converting the first            * character from upper case to lower case, but in the (unusual) special            * case when there is more than one character and both the first and            * second characters are upper case, we leave it alone.            *            * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays            * as "URL".            *            * @param  name The string to be decapitalized.            * @return  The decapitalized version of the string.            */            public static String decapitalize(String name) {            if (name == null || name.length() == 0) {            return name;            }            if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&            Character.isUpperCase(name.charAt(0))){            return name;            }            char chars[] = name.toCharArray();            chars[0] = Character.toLowerCase(chars[0]);            return new String(chars);            }

注釋說(shuō)了:一般情況下,把字符串第一個(gè)字母變?yōu)樾?xiě),如把“FooBah”變?yōu)?#8220;fooBah”。但在特殊情況下,即字符串前兩個(gè)字母都是大寫(xiě)的時(shí)候,什么也不做,如,遇到“URL”,原樣返回。

decapitalize()的bug是:如果一個(gè)字符串,前兩個(gè)字母大寫(xiě),但后面還有小寫(xiě)字母,它仍然返回原字符串!

Hibernate的開(kāi)發(fā)者注意到decapitalize()的特點(diǎn),所以才在判斷語(yǔ)句中使用一個(gè)或運算(不然只需要判斷方法名截掉“get”,再改第一個(gè)字母為小寫(xiě)后的字符串與屬性名是否相等即可,這也是按照JavaBean Specification定義的標準做法)。但是,Hibernate沒(méi)有解決這個(gè)bug,可能是他們沒(méi)有碰到我遇到的情況。

類(lèi)似sAddress(一般性地說(shuō),第一個(gè)字母小寫(xiě),第二個(gè)字母大寫(xiě))屬性命名就是bug的誘因。

那么,解決方法有三種:

  1. 把屬性名改成SAddress,這樣就滿(mǎn)足上面匹配判斷的第二個(gè)條件(方法名截掉“get”后,與屬性名匹配)。但是,這樣做不符合Java命名規范;
  2. 把getSAddress()改成getsAddress(),這樣也滿(mǎn)足上面匹配判斷的第二個(gè)條件(方法名截掉“get”后,與屬性名匹配)。但是,這樣做不符合JavaBean命名規范;
  3. 把屬性名改成strAddress,并形成一種約定:命名屬性時(shí),第二個(gè)字符只能是小寫(xiě)字母。這個(gè)方法不需要做更多地修改,符合所有規范,最為穩妥。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
解析Hibernate Validator
Hibernate Projections(投影、統計、不重復結果)
hibernate的Criteria條件查詢(xún)
Hibernate框架學(xué)習之注解映射實(shí)體類(lèi)
很有用的反射工具類(lèi)
JavaScript高級培訓-自定義對象
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久