2013-09-21 13 views
5

Sto cercando di trovare dei bug nel nostro codice legacy usando findBugs. In uno dei metodi, findBugs sta dando l'errore OBL_UNSATISFIED_OBLIGATION. Ho verificato che tutti i flussi siano correttamente chiusi. Ecco il frammento di codice:FindBugs OBL_UNSATISFIED_OBLIGATION

FileWriter fw = null; 
FileReader fr = null; 
try { 
    if (!new File(filePath).exists()) { 
     requiredStrings = CommandUtils.invoke(filename); 
     fw = new FileWriter(filePath); 
     fw.write(requiredStrings); 
    } else {    
     StringBuilder sb = new StringBuilder(); 
     fr = new FileReader(filePath); 

     char[] buffer = new char[BLOCK_READ_SIZE]; 
     int bytesRead; 
     while (-1 != (bytesRead = fr.read(buffer, 0, BLOCK_READ_SIZE))) { 
      sb.append(buffer, 0, bytesRead); 
     } 
     requiredStrings = sb.toString(); 
    } 
} finally { 
    if (fw != null) { 
     fw.close(); 
    } 
    if (fr != null) { 
     fr.close(); 
    } 
} 
return requiredStrings; 

L'errore dice che Obbligo di ripulire resurces a non scaricata, il percorso continua a .... linea .... obblighi rimanenti {Reader x 1, Scrittore x-1 }

+4

Forse si lamenta che non si rilevano eccezioni da 'close()' in modo che 'fr' possa essere lasciato libero se getta fw.close()'. Inoltre, [_ "l'euristica di soppressione del falso positivo per questo modello di bug non è stata ampiamente ottimizzata, quindi i rapporti sui falsi positivi ci sono utili." _] (Http://findbugs.sourceforge.net/bugDescriptions.html#OBL_UNSATISFIED_OBLIGATION) –

+0

Vedere questo: [Java chiusura delle connessioni e findbugs] [1] [1]: http://stackoverflow.com/questions/4398386/java-closing-connections-and-findbugs – zhaoyuanjie

risposta

0

È necessario rilevare le eccezioni IO lanciate da FileReader e FileWriter al momento della chiusura. È possibile farlo in Java 7 e superiore con try with resources

try (FileWriter fw = new FileWriter(filePath); FileReader fr = new FileReader(filePath)) { 
    /*your code here*/ 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

O per il vecchio modo

FileWriter fw = null; 
    FileReader fr = null; 
    try { 
     /*your code here*/ 
     fw = new FileWriter(filePath); 
     /*your code here*/ 
     fr = new FileReader(filePath); 
     /*your code here*/ 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (fw != null) { 
       fw.close(); 
      } 
      if (fr != null) { 
       fr.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

In un metodo, si apre una InputStream per la lettura, se c'è una possibilità del metodo si chiude senza chiudere questo oggetto InputStream, FindBugs si lamenterebbe come non riuscire a ripulire java.io.InputStream sull'eccezione controllata. Ad esempio:

void readProperties() throws FooException{ 
    InputStream is= ... 
    PropertyFactory.getInstance().loadXXXFromPropertyStream(is); // it throws FooException 
    is.close(); // maybe never called for a FooException leaving inputstream is open. 
} 
Problemi correlati