打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JavaBean:属性名大小写与getter方法命名问题

JavaBean:属性名大小写与getter方法命名问题

情况如下,Company类的对象有一个叫sAddress的属性,根据JavaBean Specification,getter和setter分别为getSAddress和setSAddress。但是编译时会报错:

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类中的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类中声明的方法进行遍历,找到与属性名匹配的方法,即属性的getter方法。比较分两部分,第一部分,针对primitive和自定义类类型的属性;第二部分,针对boolean类型的属性(由于boolean类型属性的getter方法的特殊性)。

跟踪发现,methodName的值为“getSAddress”,propertyName的值为“sAddress”,testOldMethod的值为“SAddress”,testStdMethod的值为“SAddress”。testStdMethod和testOldMethod相同,而它们都不匹配propertyName!

因此,getterMethod()中找不到与属性sAddress匹配的getter方法,getterMethod()返回null,导致异常。

问题出在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);            }

注释说了:一般情况下,把字符串第一个字母变为小写,如把“FooBah”变为“fooBah”。但在特殊情况下,即字符串前两个字母都是大写的时候,什么也不做,如,遇到“URL”,原样返回。

decapitalize()的bug是:如果一个字符串,前两个字母大写,但后面还有小写字母,它仍然返回原字符串!

Hibernate的开发者注意到decapitalize()的特点,所以才在判断语句中使用一个或运算(不然只需要判断方法名截掉“get”,再改第一个字母为小写后的字符串与属性名是否相等即可,这也是按照JavaBean Specification定义的标准做法)。但是,Hibernate没有解决这个bug,可能是他们没有碰到我遇到的情况。

类似sAddress(一般性地说,第一个字母小写,第二个字母大写)属性命名就是bug的诱因。

那么,解决方法有三种:

  1. 把属性名改成SAddress,这样就满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合Java命名规范;
  2. 把getSAddress()改成getsAddress(),这样也满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合JavaBean命名规范;
  3. 把属性名改成strAddress,并形成一种约定:命名属性时,第二个字符只能是小写字母。这个方法不需要做更多地修改,符合所有规范,最为稳妥。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
解析Hibernate Validator
Hibernate Projections(投影、统计、不重复结果)
hibernate的Criteria条件查询
Hibernate框架学习之注解映射实体类
很有用的反射工具类
JavaScript高级培训-自定义对象
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服