findbugs检测提示详解

2019-01-07 14:06

findbugs检测提示详解

1、Comparison of String objects using == or !=

例,override equals方法时容易犯错 if(this.topic != key.getTopic()) return false;

2、Dead store to newStatusRecord

定义局部变量后没有引用

3、Invocation of toString on values

直接调用数组的toString方法

public Query createQuery(String hql, Object values[],Session session){

logger.debug(values); logger.debug((new

StringBuilder()).append(\\}

正确的例子,调用Arrays.toString()和Arrays.deepToString()方法。 import java.util.Arrays; class A{ }

class B{ @Override

public String toString() { return \ } }

public class Test {

public static void main(String[] args) {

Object [] a = {new Integer(0),new Boolean(true),true,new A(),new B()};

Object[][]b ={{new A(),new B()},{new A(),new B()},{new A(),new B()}}; System.out.println(Arrays.deepToString(b)); } }

4、ignores exceptional return value of

java.io.File.mkdirs()

忽略了返回值,应当含有返回值

public void initFolder() { if (!exitDir.isDirectory()) { exitDir.mkdirs();

logger.info(\ } }

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.

5、不使用new String()定义空的字符串

String alarmCodeCond = new String(); 应当

String alarmCodeCond = \

6、invokes inefficient new Short(short) constructor; use Short.valueOf(short) instead JVM缓存数字常量

Short aShort = new Short(12); 应当

Short aShort = Short.valueOf(12);

7、方法命名习惯,首字母小写

The method name LaneHandShakeService(Short) doesn't start with a lower case letter

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

8、一个primtive的类型的值经过box后马上unbox

Primitive value is boxed then unboxed to perform primitive coercion

exitRecord.setEnOperatorId(new

Long(transactRecord.getEnoperatorID()).intValue()); 应当直接强制类型转换

exitRecord.setEnOperatorId((int)transactRecord.getEnoperatorID());

9、Call to equals() comparing different types 使用equals()方法比较不同的类,

反例

StringBuilder builder = new StringBuilder(\ String string = \ builder.equals(string);

10、Check for oddness that won't work for negative numbers

检查奇数的方法:

反例

if (i % 2 == 1) { //... }

The code uses x % 2 == 1 to check to see if a value is odd, but this won't work for negative numbers (e.g., (-5) % 2 == -1). If this code is intending to check for oddness, consider using x & 1 == 1, or x % 2 != 0.

11、Load of known null value,null值的不当使用

反例:

if (devIds == null && devIds.size() == 0) { //... }

if (null != tempList || tempList.size() != 0) { //... }

if (batchNo == null) {

throw new Exception(\ + \ }

12、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(\

}

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

//...

//... //...


findbugs检测提示详解.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:自动化专业概论大作业及评分标准

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: