1,AM: Creates an empty jar file entry (AM_CREATES_EMPTY_JAR_FILE_ENTRY)/AM: Creates an empty zip file entry (AM_CREATES_EMPTY_ZIP_FILE_ENTRY)java
示例代码:dom
ZipEntry entry = new ZipEntry(PATH);函数
zos.putNextEntry(entry);字体
zos.closeEntry();code
缘由:对象
代码中在调用putNextEntry()以后紧接着调用了closeEntry()函数,导致该jar文件内容为空,由于打jar包的写内容是在putNextEntry()和closeEntry()两个函数调用之间来进行的。(有时候也许会有意的构建一个空目录,所以不必定就是bug)blog
2,BC: Equals method should not assume anything about the type of its argument (BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS)继承
示例代码:接口
public class Foo {ip
// some code
public void equals(Object o) {
Foo other = (Foo) o;
// the real equals code
}
}
缘由:
当你在实现类的equals方法时,不该该对参数有任何的预先设定。如上代码所写,则设定了参数o确定是Foo类的一个对象.可是若是在函数调用时,参数o不是一个Foo类或其子类,就会致使代码会抛出一个ClassCastException。所以在实现equals方法,应该加一个判断,若是参数o不是一个Foo类对象,则返回false。
3,BC: Random object created and used only once (DMI_RANDOM_USED_ONLY_ONCE)
示例代码:
public int getRandom(int seed) {
return new Random(seed).nextInt();
}
缘由:
因为java.util.Random是一个伪随机函数,若是传入的seed值相同的话,返回的随机数者是相同的 。所以不必每次都new一个新的random出来计算随机数。若是你想真正地得到一个不可预知的随机数,建议使用java.security.SecureRandom,该类继承自Random,是一个强随机数生成器 。所以上述代码能够修改成:
public class Test extends Thread{
private SecureRandom ran;
Test(int seed){
ran = new SecureRandom();
}
public int getRandom(int seed) {
return ran.nextInt();
}
}
4,CN: Class implements Cloneable but does not define or use clone method (CN_IDIOM)
示例代码:
public class Foo implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
缘由:
类定义要实现了 Cloneable接口,却没有定义或使用 clone方法,即缺乏红色字体部分。
5,CN: clone method does not call super.clone() (CN_IDIOM_NO_SUPER_CALL)
示例代码:
public class Foo implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
缘由:
clone方法没有调用super.clone()方法,若是没有调用,则会致使对象父子层级关系不能正确创建,最终致使没法正确组装对象。
6,CN: Class defines clone() but doesn't implement Cloneable (CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE)
示例代码:
public class Foo{
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
缘由:
这个用法的意义在于你能够规范该类的子类的clone的实现,若是你的确想这样作的话,这不是一个bug,不然的话是一个bug
7,DE: Method might drop exception (DE_MIGHT_DROP)/DE: Method might ignore exception (DE_MIGHT_IGNORE)
示例代码:
try{}catch(Exception ex){}
缘由:
方法有可能抛异常或者忽略异常,须要对异常进行处理,即须要在catch体中对异常进行处理。
8,DMI: Don't use removeAll to clear a collection (DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION)
缘由:
建议不要使用 collection.removeAll(collection)方法来删除 collection中的全部元素,而使用collection.clear()。比较两者的代码实现就能够看出:
removeAll()源码:
public boolean removeAll(Collection<?> c) {
boolean modified = false;
Iterator<?> e = iterator();
while (e.hasNext()) {
if (c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
clear()源码:
public void clear() {
Iterator<E> e = iterator();
while (e.hasNext()) {
e.next();
e.remove();
}
}
前者是比较参数中的collection和要移除元素的collection中是否有交集,而后将交集元素删除;后者是直接将collenction中的元素删除。显而后者要比前者高效,并且对于某些特殊的collenction还容易抛出一些异常,如ConcurrentModificationException
9,ES: Comparison of String parameter using == or != (ES_COMPARING_PARAMETER_STRING_WITH_EQ)
缘由:当比较两个字符串内容是否相同时,仅当两个字符串在源文件中都是常量时或者是使用intern()来比较才能够用==来比较,不然最好使用对象比较方法equal。附string比较:
String str1 = "java";
String str2 = "java";
System.out.print(str1==str2);
结果:true(两者都为常量)
String str1 = new String("java");
String str2 = new String("java");
System.out.print(str1==str2);
结果:false(两者为对象)
String str1 = "java";
String str2 = "blog";
String s = str1+str2;
System.out.print(s=="javablog");
结果:false(s不为常量,为对象)
String s1 = "java";
String s2 = new String("java");
System.out.print(s1.intern()==s2.intern());
结果:true(可是intern()方法在效率和实现方式上不统一)