findbugs工具常见问题(转)

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)

示例代码:

ZipEntry entry = new ZipEntry(PATH);

zos.putNextEntry(entry);

zos.closeEntry();

原因:

代码中在调用putNextEntry()之后紧接着调用了closeEntry()函数,致使该jar文件内容为空,因为打jar包的写内容是在putNextEntry()和closeEntry()两个函数调用之间来进行的。(有时候也许会有意的构建一个空目录,因此不一定就是bug)

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 {

// 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 = 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()方法在效率和实现方式上不统一)

你可能感兴趣的:(findbugs工具常见问题(转))