2013-12-10 26 views
4

我有一個代碼分析工具,萎靡不振的LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>();線在下面的方法,在方法來解決,將滿足分析工具的邏輯什麼想法?代碼分析失敗:死存儲到本地變量

死存儲到局部變量:

該指令將一個值分配給本地變量,但該值不被讀取或以任何後續指令使用。通常,這表示出現錯誤,因爲計算的值從不使用。請注意,Sun的javac編譯器通常爲最終的局部變量生成死鎖存儲。由於FindBugs是一個基於字節碼的工具,因此沒有簡單的方法來消除這些誤報。

public void add(Map<String, String> input) {  
    TreeSet<String> widgetsToAdd = new TreeSet<String>(); 
    TreeSet<String> widgetsToUpdate = new TreeSet<String>(); 
    LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>(); 

    for (Map.Entry<String, String> entry : input.entrySet()) { 
     //logic to add to widgetsToAdd based on content of the input Map 
    } 

    widgetsToCreate = processInput(widgetsToAdd); 
    for (Iterator<String> wIterator = widgetsToCreate.iterator(); wIterator.hasNext();) { 
     //process each widgetsToCreate 
    } 
} 

回答

12

蔭不知道,但我覺得你得到的錯誤消息,因爲你永遠不使用分配new LinkedHashSet<String>();

// LinkedHashSet assigned to widgetsToCreate 
LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>(); 

// widgetsToCreate is not used 
for (Map.Entry<String, String> entry : input.entrySet()) { 
    //logic to add to widgetsToAdd based on content of the input Map 
} 

// new value assigned to widgetsToCreate, the LinkedHashSet assigned before wasn't used 
widgetsToCreate = processInput(widgetsToAdd); 

所以,你可以寫:

... 
for (Map.Entry<String, String> entry : input.entrySet()) { 
    //logic to add to widgetsToAdd based on content of the input Map 
} 
LinkedHashSet<String> widgetsToCreate = processInput(widgetsToAdd);