我有一个代码分析工具,它在下面的方法中标记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
}
}
答案 0 :(得分:13)
我不确定,但我认为您收到错误消息,因为您从未使用指定的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);
答案 1 :(得分:0)
您收到错误消息,因为您从未使用分配的widgetsToUpdate。