boss代码review记录

发布时间 : 星期六 文章boss代码review记录更新完毕开始阅读

1.21.1 错误示例

1.21.2 Bug描述

Bug: its.sso.sync.rmc.pojo.User defines equals and uses Object.hashCode() Patternid: HE_EQUALS_USE_HASHCODE, type: HE, category: BAD_PRACTICE This class overrides equals(Object), but does not override hashCode(), and inherits the implementation of hashCode() from java.lang.Object (which returns the identity hash code, an arbitrary value assigned to the object by the VM). Therefore, the class is very likely to violate the invariant that equal objects must have equal hashcodes. If you don't think instances of this class will ever be inserted into a HashMap/HashTable, the recommended hashCode implementation to use is: public int hashCode() { assert false : \ return 42; // any arbitrary constant will do } 1.21.3 解决方案

重载了equals方法,却没有重载hashCode方法,如果使用object自己的hashCode,我们可以从JDK源代码可以看到object的hashCode方法是native的,它的值由虚拟机分配(某种情况下代表了在虚拟机中的地址或者唯一标识),每个对象都不一样。所以这很可能违

反“Equals相等,hashcode一定相等;hashcode相等,equals不一定相等。”除非你保证不运用到HashMap/HashTable等运用散列表查找值的数据结构中。否则,发生任何事情都是有可能的。 关于何时改写hashcode,请参考:在重写了对象的equals方法后,还需要重写hashCode方法吗? 关于编写高质量的equals方法: 1.先使用==操作符检查是否是同一个对象,==都相等,那么逻辑相等肯定成立; 2.然后使用instanceof操作符检查“参数是否为正确的类型”; 3.把参数转换成正确的类型; 4.对于该类中的非基本类型变量,递归调用equals方法; 5.变量的比较顺序可能会影响到equals方法的性能,应该最先比较最有可能不一致的变量,或者是开销最低的变量。 当你编写完成equals方法之后,应该问自己三个问题:它是否是对称的、传递的、一致的? 解决方法: 除非你保证不运用到HashMap/HashTable等运用散列表查找值的数据结构中,请重写hashcode方法。 1.21.4 自动生成的

@Override publicint hashCode() { finalint prime = 31; int result = 1; result = prime * result + ((add1 == null) ? 0 : add1.hashCode()); result = prime * result + ((add5 == null) ? 0 : add5.hashCode()); result = prime * result + ((cellPhone == null) ? 0 : cellPhone.hashCode()); result = prime * result + ((department == null) ? 0 : department.hashCode()); result = prime * result + ((email == null) ? 0 : email.hashCode());

result = prime * result + ((fullName == null) ? 0 : fullName.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((login == null) ? 0 : login.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + (processed ? 1231 : 1237); result = prime * result + ((tel == null) ? 0 : tel.hashCode()); return result; } @Override publicboolean equals(Object obj) { if (this == obj) { returntrue; } if (obj == null) { returnfalse; } if (getClass() != obj.getClass()) { returnfalse; } User other = (User) obj; if (add1 == null) { if (other.add1 != null) { returnfalse; } } elseif (!add1.equals(other.add1)) { returnfalse; } if (add5 == null) { if (other.add5 != null) { returnfalse; } } elseif (!add5.equals(other.add5)) { returnfalse; } if (cellPhone == null) { if (other.cellPhone != null) { returnfalse; } } elseif (!cellPhone.equals(other.cellPhone)) { returnfalse; }

if (department == null) { if (other.department != null) { returnfalse; } } elseif (!department.equals(other.department)) { returnfalse; } if (email == null) { if (other.email != null) { returnfalse; } } elseif (!email.equals(other.email)) { returnfalse; } if (fullName == null) { if (other.fullName != null) { returnfalse; } } elseif (!fullName.equals(other.fullName)) { returnfalse; } if (id == null) { if (other.id != null) { returnfalse; } } elseif (!id.equals(other.id)) { returnfalse; } if (login == null) { if (other.login != null) { returnfalse; } } elseif (!login.equals(other.login)) { returnfalse; } if (password == null) { if (other.password != null) { returnfalse; } } elseif (!password.equals(other.password)) { returnfalse; } if (processed != other.processed) { returnfalse;

联系合同范文客服:xxxxx#qq.com(#替换为@)