Bugs found in HBase 2.0:

本文分析了HBase 2.0中的一些常见错误模式,包括使用不同类型的equals()比较,未正确实现compareTo()和equals(),不覆盖hashCode(),构造函数启动线程,死区变量赋值等。这些问题可能会影响代码的正确性、性能和线程安全性。
摘要由CSDN通过智能技术生成

Bugs found in HBase 2.0:

Pattern: Call to equals() comparing different types

id: EC_UNRELATED_TYPES, type: EC, category:CORRECTNESS

This method calls equals(Object) on two references of different class typeswith no common subclasses. Therefore, the objects being compared are unlikelyto be members of the same class at runtime (unless some application classeswere not analyzed, or dynamic class loading can occur at runtime). According tothe contract of equals(), objects of different classes should always compare asunequal; therefore, according to the contract defined byjava.lang.Object.equals(Object), the result of this comparison will always befalse at runtime.

 

Pattern: Class defines compareTo(...) and usesObject.equals()

id: EQ_COMPARETO_USE_OBJECT_EQUALS, type: Eq, category:BAD_PRACTICE

This class defines a compareTo(...) method but inherits its equals()method from java.lang.Object. Generally, the value of compareTo should returnzero if and only if equals returns true. If this is violated, weird andunpredictable failures will occur in classes such as PriorityQueue. In Java 5the PriorityQueue.remove method uses the compareTo method, while in Java 6 ituses the equals method.

From the JavaDoc for the compareTo method in the Comparable interface:

It is strongly recommended, but not strictly requiredthat (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class thatimplements the Comparable interface and violates this condition should clearlyindicate this fact. The recommended language is "Note: this class has anatural ordering that is inconsistent with equals."

 

 

Pattern: Class defines equals() and uses Object.hashCode()

id: HE_EQUALS_USE_HASHCODE, type: HE, category:BAD_PRACTICE

This class overrides equals(Object), but does not override hashCode(), andinherits the implementation of hashCode() from java.lang.Object (which returnsthe identity hash code, an arbitrary value assigned to the object by theVM).  Therefore, the class is very likely to violate the invariant thatequal objects must have equal hashcodes.

If you don't think instances of this class will ever be inserted into aHashMap/HashTable, the recommended hashCode implementation to use is:

public int hashCode() {

  assert false :"hashCode not designed";

  return 42; // anyarbitrary constant will do

  }

Pattern: clone method does not call super.clone()

id: CN_IDIOM_NO_SUPER_CALL, type: CN, category:BAD_PRACTICE

This non-final class defines a clone() method that does not callsuper.clone(). If this class ("A") is extended by a subclass("B"), and the subclass B calls super.clone(), then itis likely that B's clone() method will return an object of type A,which violates the standard contract for clone().

If all clone() methods call super.clone(), then they are guaranteed to useObject.clone(), which always returns an object of the correct type.

 

Pattern: Constructor invokes Thread.start()

id: SC_START_IN_CTOR, type: SC, category:MT_CORRECTNESS

The constructor starts a thread. This is likely to be wrong if the classis ever extended/subclassed, since the thread will be started before thesubclass constructor is started.

 

Pattern: Dead store to local variable

id: DLS_DEAD_LOCAL_STORE, type: DLS, category:STYLE

This instruction assigns a value to a local variable, but the value is notread or used in any subsequent instruction. Often, this indicates an error,because the value computed is never used.

Note that Sun's javac compiler often generates dead stores for final localvariables. Because FindBugs is a bytecode-based tool, there is no easy way toeliminate these false positives.

 

Pattern: equals method always returns false

id: EQ_ALWAYS_FALSE, type: Eq, category:CORRECTNESS

This class defines an equals method that always returns false. This meansthat an object is not equal to itself, and it is impossible to create usefulMaps or

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值