CodeReview常见错误集合(FindBugs)

2017-09-27  本文已影响0人  maven_hz

FindBugs插件代码分析结果中的分类:

常见错误列表及建议

错误代码示例:

public class QCData {
    private Date labdate;
    public Date getLabdate() {
       return labdate;
    }

   public void setLabdate(Date labdate) {
          this.labdate = labdate;
    }
     
}

当我们运行测试类时会发现:

public class ExposeTest {
    public static void main(String[] args){
        QCData qcdata = new QCData();
        Date labdate = new Date();
        qcdata.setLabdate(labdate); 
        System.err.println(qcdata.getLabdate());
        labdate.setTime(11122222);//外部引用改变了labdate的值
        System.err.println(qcdata.getLabdate());//这里并没有再次调用实体类的setLabdate方法,但是labdate的值已经改变
    }
}
结果:
Mon Sep 25 16:52:51 CST 2017
Thu Jan 01 11:05:22 CST 1970

解决方式:

public class QCData {
    private Date labdate;
    public Date getLabdate() {
        if (labdate == null)
        {  
            return null;
        }
        return (Date)labdate.clone();
     }
  
    public void setLabdate(Date labdate) {
        if (labdate == null)
        {
            this.labdate = null;
        } else {
            this.labdate = (Date)labdate.clone();
        }
   }
}
//再次运行测试类会发现对外部引用的改变将不对实例内的值产生影响
public class CommonTips {
    public static String TIPS_OPERATE_SUCCESS = "成功!";
    public static String TIPS_OPERATE_FAIL = "失败!";
    /**
    *修改为 public static final String TIPS_OPERATE_SUCCESS = "成功!";
    */
}
for (String key : dataMap.keySet()) {  //迭代key
    if(key.equals("a")){
        dataMap.get(key);  //根据key获取value的时候。又是一次迭代
    }
    ...           
 }

解决方式:

for(Map.Entry<String,String> entry : dataMap.entrySet()){  //只需要一次迭代
    String key = entry.getKey(); 
    if(key.equals("a")){
        entry.getValue();
    }
}
String tableHtml = new String(Common.getTableHtml(columns, columnsName, voucherList));
/**
*请不要 new String();  无须在堆中新建一个String对象。
*/
public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
                InputStream inStream = new FileInputStream(oldPath);
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();//只关闭了InputStream。未对FileOutputStream进行关闭。
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错 ");
            e.printStackTrace();
        }
    }

解决方式:

public static void copyFile(String oldPath, String newPath) {
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
                inStream = new FileInputStream(oldPath);
                fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错 ");
            e.printStackTrace();
        }finally{  //在finally语句块中将资源关闭
          try {
                inStream.close();
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
    }
上一篇 下一篇

猜你喜欢

热点阅读