FindBug使用总结

AM : Creates an empty jar file entry(AM_CREATES_EMPTY_JAR_FILE_ENTRY)
在putNextEntry()和closeEntry()之间,没有对jar文件做其他操作。这样会给jar文件生成一个空的条目。
The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty JarFile entry. The contents of the entry should be written to the JarFile between the calls to putNextEntry() and closeEntry().


AM : Creates an empty zip file entry(AM_CREATES_EMPTY_ZIP_FILE_ENTRY)
在putNextEntry()和closeEntry()之间,没有对zip文件做其他操作。这样会给zip文件生成一个空的条目。
The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty ZipFile entry. The contents of the entry should be written to the ZipFile between the calls to putNextEntry() and closeEntry().


BC : Equals method should not assume anything about the type of its argument (BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS)
在equals方法中,没有对参数进行类型匹配判断。改法很简单,加上:
if (!(o instanceof [当前的class]) {
return false;
}
The equals(Object o) method shouldn't make any assumptions about the type of o. It should simply return false if o is not the same type as this.


BC : Random object created and used only once(DMI_RANDOM_USED_ONLY_ONCE)
一个java.util.Random对象只使用了一次,生成了一个随机数就废弃了。正常的做法,应该把这个对象保存起来,所有需要用到随机数的地方都调用这一个对象就行了。
This code creates a java.util.Random object, uses it to generate one random number, and then discards the Random object. This produces mediocre quality random numbers and is inefficient. If possible, rewrite the code so that the Random object is created once and saved, and each time a new random number is required invoke a method on the existing Random object to obtain it.


BIT : Check for sign of bitwise operation(BIT_SIGNED_CHECK)
一个判断语句中,使用了位操作,并且进行了>0的比较。例如:
((event.detail & SWT.SELECTED) > 0)
这个判断,本意应该是两个数字的做与操作后还有非0的位数。但是,一个不小心,与操作的结果是个负数,这就是一个bug了。最好用"!="替换">0"
This method compares an expression such as

((event.detail & SWT.SELECTED) > 0)
. Using bit arithmetic and then comparing with the greater than operator can lead to unexpected results (of course depending on the value of SWT.SELECTED). If SWT.SELECTED is a negative number, this is a candidate for a bug. Even when SWT.SELECTED is not negative, it seems good practice to use '!= 0' instead of '> 0'.
Boris Bokowski



CN : Class implements Cloneable but does not define or use clone method(CN_IDIOM)
一个类实现了Cloneable接口,但是没有声明或使用到clone方法。因为clone方法是Object类的方法,所以当前类不去声明这个方法,不会编译不通过。但是,clone是需要逐个字段去复制的,所以没有声明clone方法是不对的。
Class implements Cloneable but does not define or use the clone method.


CN : clone method does not call super.clone()(CN_IDIOM_NO_SUPER_CALL)
非final的类,定义了clone()方法,却在方法中没有调用super.clone()。
看上去,不应该调用super.clone()。如果A是B的父类,那么B调用super.clone(),则B的clone方法返回的是A的实例,看上去是错的。
但是,如果所有的clone()方法都调用了super.clone(),则最终调用的是Object.clone(),那就能返回正确的类型。
This non-final class defines a clone() method that does not call super.clone(). If this class ("A") is extended by a subclass ("B"), and the subclass B calls super.clone(), then it is 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 use Object.clone(), which always returns an object of the correct type.



CN : Class defines clone() but doesn't implement Cloneable (CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE)
类定义了clone()方法,但是没有声明实现Cloneable接口。这个不是个什么大问题,只是确认一下是不是漏了声明。
This class defines a clone() method but the class doesn't implement Cloneable. There are some situations in which this is OK (e.g., you want to control how subclasses can clone themselves), but just make sure that this is what you intended.


Co : Abstract class defines covariant compareTo() method(CO_ABSTRACT_SELF)
类定义了一个compareTo()方法,其参数不是Object类型。要正确的实现Comparable接口的compareTo()方法,最好的做法就是,compareTo()方法的参数就是Object类。
This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.


Co : Covariant compareTo() method defined(CO_SELF_NO_OBJECT)
同上
This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.


DE : Method might drop exception(DE_MIGHT_DROP)
这个方法可能放弃了异常。正常的做法,异常应该被处理或者通过某种方式被报告,或者再扔给外层。
This method might drop an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.


DE : Method might ignore exception(DE_MIGHT_IGNORE)
这个方法可能忽略了异常。正常的做法,异常应该被处理或者通过某种方式被报告,或者再扔给外层。
This method might ignore an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.


DMI : Don't use removeAll to clear a collection(DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION)
如果你想把集合内的所有的元素都删除掉,请用集合的clear方法,而不是c.removeAll( c )方法。调用c.removeAll( c )去清空集合,会清除的不干净,容易产生错误,可能会抛出ConcurrentModificationException异常。
If you want to remove all elements from a collection c, use c.clear, not c.removeAll(c). Calling c.removeAll(c) to clear a collection is less clear, susceptible to errors from typos, less efficient and for some collections, might throw a ConcurrentModificationException.


DP : Classloaders should only be created inside doPrivileged block (DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED)
这段代码写了一个需要安全管理器的classloader。如果代码需要被授权为安全权限,但是可能被不安全的代码去调用,那么classloader就需要放在doPrivileged块内。
This code creates a classloader, which requires a security manager. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the classloader creation needs to occur inside a doPrivileged block.


DP : Method invoked that should be only be invoked inside a doPrivileged block (DP_DO_INSIDE_DO_PRIVILEGED)

代码调用了一个需要安全权限检查的方法。如果代码需要被授权为安全权限,但是可能被不安全的代码去调用,那么classloader就需要放在doPrivileged块内。
This code invokes a method that requires a security permission check. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the invocation needs to occur inside a doPrivileged block.


Dm : Method invokes System.exit(...)(DM_EXIT)
调用了System.exit去关闭虚拟机进程。只能在适当的时候这么用。这种调用会让你的代码很难甚至不可能被其他代码调用。用扔出RuntimeException异常来代替会比较好。
Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.


Dm : Method invokes dangerous method runFinalizersOnExit(DM_RUN_FINALIZERS_ON_EXIT)
永远不要以任何理由调用System.runFinalizersOnExit 或者Runtime.runFinalizersOnExit,在java包里面,他们是非常危险的方法。
-- Java教父Joshua Bloch
Never call System.runFinalizersOnExit or Runtime.runFinalizersOnExit for any reason: they are among the most dangerous methods in the Java libraries. -- Joshua Bloch


ES : Comparison of String parameter using == or !=(ES_COMPARING_PARAMETER_STRING_WITH_EQ)
这段代码用 == 或者 != 来比较字符串。这种方式去比较字符串,并不是比较字符串的内容相同,而是比较是不是同一个对象。用equals方法来代替这种比较。
This code compares a java.lang.String parameter for reference equality using the == or != operators. Requiring callers to pass only String constants or interned strings to a method is unnecessarily fragile, and rarely leads to measurable performance gains. Consider using the equals(Object) method instead.


ES : Comparison of String objects using == or !=(ES_COMPARING_STRINGS_WITH_EQ)
同上
This code compares java.lang.String objects for reference equality using the == or != operators. Unless both strings are either constants in a source file, or have been interned using the String.intern() method, the same string value may be represented by two different String objects. Consider using the equals(Object) method instead.


Eq : Abstract class defines covariant equals() method(EQ_ABSTRACT_SELF)
这个类定义了equals()方法,但是参数却是Object的子类。正确覆盖equals()方法,参数必须是Object
This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.


Eq : Equals checks for noncompatible operand (EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS)
equals方法内,对参数的类型检查的时候,检查了除了本身之外的其他类型。如:
public boolean equals(Object o) {
if (o instanceof Foo)
return name.equals(((Foo)o).name);
else if (o instanceof String)
return name.equals(o);
else return false;
这种写法是不好的习惯,它会让代码难以理解和迁移。
This equals method is checking to see if the argument is some incompatible type (i.e., a class that is neither a supertype nor subtype of the class that defines the equals method). For example, the Foo class might have an equals method that looks like:


public boolean equals(Object o) {
if (o instanceof Foo)
return name.equals(((Foo)o).name);
else if (o instanceof String)
return name.equals(o);
else return false;
This is considered bad practice, as it makes it very hard to implement an equals method that is symmetric and transitive. Without those properties, very unexpected behavoirs are possible.



Eq : Class defines compareTo(...) and uses Object.equals()(EQ_COMPARETO_USE_OBJECT_EQUALS)
这个类定义了compareTo(…)方法,但是却直接继承Object的equals()方法。通常,compareTo方法在并且只有在equals方法返回ture的时候返回0。如果没有遵守这个原则,就会出现一些奇怪和不可预测的问题。在java5中,PriorityQueue.remove方法是用了compareTo方法,但是java6中它用的却是equals方法。
不用多说了。
This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.
From the JavaDoc for the compareTo method in the Comparable interface:

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



Eq : equals method fails for subtypes(EQ_GETCLASS_AND_CLASS_CONSTANT)
这个类写了自己的equals方法,但是这个方法在比较参数的对象类型的时候被定义的不可继承,如果有子类继承了这个类,那么就会出错。例如,Foo类的检查是:
if (Foo.class == o.getClass())。最好改成:
if (this.getClass() == o.getClass())
This class has an equals method that will be broken if it is inherited by subclasses. It compares a class literal with the class of the argument (e.g., in class Foo it might check if Foo.class == o.getClass()). It is better to check if this.getClass() == o.getClass().


Eq : Covariant equals() method defined(EQ_SELF_NO_OBJECT)
equals()方法的参数,最好就是Object。如果不是,会容易出问题。
This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.

你可能感兴趣的:(FindBug使用总结)