`

findbugs的一些错误说明

 
阅读更多

1. equals比较不同的对象类型
Call to equals() comparing different types
This method calls equals(Object) on two references of different class types with no common subclasses. Therefore, the objects being compared are unlikely to be members of the same class at runtime (unless some application classes were not analyzed, or dynamic class loading can occur at runtime). According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.
说的是equals要比较相同的对象类型

2,可能产生空指针异常
Possible null pointer dereference
A reference value dereferenced here might be null at runtime.  This may lead to a NullPointerException when the code is executed.

3.从未使用的本地变量
Dead store to local variable
This instruction assigns a value to a local variable, but the value is not read by 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 local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.

4.应该是一个静态内部类
Should be a static inner class

This class is an inner class, but does not use its embedded reference to the object which created it.  This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static.

5.方法名称第一个字母小写
Method names should start with an lower case letter

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


6.用包装类的valueOf代替NEW
解释:因为用new Integer(int) 这样的方式会产生一个新的对象
而当编译时用valueOf则会被缓存,并且速度更快。
Method invokes inefficient Number constructor; use static valueOf instead

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.

 

 

7.Dead store to local variable 本地变量存储了闲置不用的对象
举例:
List accountCoList = new ArrayList();
我们为accountCoList新建了一个对象,但是程序的后面并没有使用这个这个新建对象。
建议改为:
List accountCoList = null;

 

8.Write to static field from instance method 向static字段中写入值
举例:
 private static DBRBO dbrBO;
 public final void refresh() {
        danskeBankBO = null;
        dbrBO = null;
        fileAndPathBO = null;
    }
建议改为:
去掉static。


9. Load of known null value 大体意思是加载了null的对象。
举例
        if (null == boList) {

            for (int i = 0; i < boList.size(); i++) {
                entityList.add(productBOToEntity(boList.get(i)));
            }
        }

 

 15.Inefficient use of keySet iterator instead of entrySet iterator
这个意思是说请用遍历Entry的方式替换keySet,因为前者比后者效率高。
很多人都这样遍历Map,没错,但是效率很低,先一个一个的把key遍历,然后在根据key去查找value,这不是多此一举么,为什么不遍历entry(桶)然后直接从entry得到value呢?它们的执行效率大概为1.5:1(有人实际测试过)。

 

16.Method call passes null for nonnull parameter

    对参数为null的情况没做处理

    例

 

public void method1() {
   String ip = null;
  try {
   ip = InetAddress.getLocalHost().getHostAddress();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
  long ipCount = countIpAddress(ip); // 可能会传入空引用
                               //...
}

 long countIpAddress(String ip) {
  long ipNum = 0;
  String[] ipArray = ip.split("\\.");
}

 

修改后:

public void method1() {
   String ip = null;
  try {
   ip = InetAddress.getLocalHost().getHostAddress();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
  long ipCount = countIpAddress(ip); // 可能会传入空引用
                               //...
}

 long countIpAddress(String ip) {
  long ipNum = 0;
  if (ip == null) {
   return 0;          //或者抛出异常
  }
  String[] ipArray = ip.split("\\.");
                               //...
}

 

注意:函数入口需要交验入参的合法性。

 

分享到:
评论

相关推荐

    eclipse插件FindBugs错误分析说明

    主要是eclipse插件FindBugs中错误的解释说明,帮助定位错误原因

    findbugs1.3.8错误类型说明及中文对照

    findbugs1.3.8错误类型说明及中文对照

    FindBugs错误分析说明.pdf

    FindBugs错误分析说明包含了常见错误,有针对性的讲解,且有相应实例对照。对java编程者非常有帮助。

    java代码分析工具findbugs安装使用说明及常见错误

    findbugs是一款基于eclipse的java静态代码分析工具,其中包含findbugs安装包,安装使用说明及常见错误的分析和解释

    sonar-findbugs-plugin

    在质量配置文件中,从FindBugs,fb-contrib或FindSecBugs规则存储库中激活一些规则,然后对项目进行分析。 配置 可以使用声纳Web界面(请参见“常规/ Java”部分)或项目属性来配置此插件。 置信度级别(sonar....

    sonar-findbugs:SonarQube的SpotBugs插件

    SonarQube Spotbugs插件说明/功能该插件需要,并使用 , 和提供编码规则。用法在质量配置文件中,从Spotbugs,fb-contrib或Find Security Bugs规则存储库中激活一些规则,然后对项目进行分析。配置可以使用声纳Web...

    findbug分析

    FindBugs错误分析说明,所有的解释和解决方式只是可能和建议,大多数时候需要根据实际情况而定。Findbugs也有少量的误报,需要大家自己甄别。

    静态工具使用说明.rar

    静态工具Findbugs、PMD、CheckStyle等检查代码工具几个灵活的 Eclipse 插件,可以提供帮你找到代码中隐藏的一些错误,很不错!

    bug-report-compare:实验数据获取:代码入侵扫描与误报标记

    错误报告比较 简介 本项目用于获取开源项目不同版本的代码,扫描扩展生成报告并根据版本间的差异进行正误报的标记​​。 功能 通过Maven仓库的链接获取源代码和Jar包 通过Git仓库的标签获取源代码 使用findbugs生成...

    projekt_eszkozok:精英项目工具gitrepo,EU4

    project_toolsEU4战斗模拟器当之无愧地流行和成功的Europa Universalis 4... FindBugs:在Maven检查阶段运行的错误检查。 -在编写文档时,您仍然可以找到错误,但它们并未全部修复- 特拉维斯(Travis)链接: : Branch

Global site tag (gtag.js) - Google Analytics