原!findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE 和 OBL_UNSATISFIED_OBLIGATION


改findbogs碰到的兩個問題,一個是關於IO流,一個是關於空指針檢查異常。

1.NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE 

前面代碼略。。。

 File crFile = new File(crFilelocalPath);
        if (crFile.exists()&& crFile.isDirectory() && crFile.listFiles() != null
                && crFile.listFiles().length > 0) {

          //略。。。

        }

后面略。。。。

原因分析:

問題出在 crFile.listFiles() ,google到,說是第一個 crFile.listFiles()判斷不為null,但是第二個crFile.listFiles()還是可能會為null。(原文:If the first call of listFiles() is != null, that doesn't mean the second call is also != null.)

google原文見:https://sourceforge.net/p/findbugs/bugs/1468/   

所以改為

      
前面略。。

     File crFile = new File(crFilelocalPath);
        if (crFile.exists() && crFile.isDirectory()) {
            File[] files = crFile.listFiles(); if (files != null && files.length > 0) {
             //略。。。
            }
        }
后面略。。。

findbugs就不報錯了。。。

 

2.OBL_UNSATISFIED_OBLIGATION

是關於IO流的關閉

findbugs報錯的代碼

 public static boolean storeFile2LocalPath(String fileName, String localPath, File fileInput) {
        boolean success = false;
        FileInputStream inputStream = null;
        OutputStream os = null;
        try {
            inputStream = new FileInputStream(fileInput);
            //保存到臨時文件
            byte[] bs = new byte[1024];// 1K的數據緩沖
            int len;// 讀取到的數據長度
            // 輸出的文件流保存到本地文件
            File tempFile = new File(localPath);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            String filePath = tempFile.getPath() + File.separator + fileName;
            os = new FileOutputStream(filePath);
            log.info("storeFile2LocalPath() and filePath = " + filePath);
            // 開始讀取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完畢,關閉所有鏈接
            try {
                if(os != null) {
                    os.close();
                    os = null;
                }
                if(inputStream != null){
                    inputStream.close();
                    inputStream = null; }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }

報錯如下:

This method may fail to clean up (close, dispose of) a stream, 
database object, or other resource requiring an explicit cleanup 
operation.

In general, if a method opens a stream or other resource, the method 
should use a try/finally block to ensure that the stream or resource 
is cleaned up before the method returns.

This bug pattern is essentially the same as the **OS_OPEN_STREAM and 
ODR_OPEN_DATABASE_RESOURCE** bug patterns, but is based on a different 
(and hopefully better) static analysis technique. We are interested is 
getting feedback about the usefulness of this bug pattern. To send 
feedback, either: •send email to findbugs@cs.umd.edu •file a bug 
report: http://findbugs.sourceforge.net/reportingBugs.html

In particular, the false-positive suppression heuristics for this bug 
pattern have not been extensively tuned, so reports about false 
positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling 
Mistakes, for a description of the analysis technique.

 

原因分析:

連續關閉兩個流,在同一個finally快里,若第一個流close失敗,出現異常時,會導致第二個流沒有關閉。

 

所以改為如下方式,findbugs不報錯了。

//將后面的finally塊改為: 再嵌套一個finally塊

finally {
            // 完畢,關閉所有鏈接
            try {
                if (os != null) {
                    os.close();
                    os = null; }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                        inputStream = null; }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM