我最近在eclipse中设置了find bugs来查看它生成了什么报告。我已将所有设置都设置为尽可能灵敏。如果我创建了一个写入文件的小应用程序,并且没有关闭流,它会拾取它,这一切都是好的。
然而,使用一个已经编写的项目,其中我们没有几个bug,特别是在输出中,我们根本没有得到任何错误(就查找bug而言)
我想知道是否有人可以运行他们的版本,并报告我是否发现了错误设置的bug,或者实际上它是否找不到bug?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SpellUtil {
private final static String teenSpelling[] = {"Zero", "One", "Two", "Three",
"Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
private final static String centSpelling[] = {"Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private final static String suffixSpelling[] = {
"", // Dummy! no level 0 (added for nicer indexing in code)
"", // Nothing for level 1
" Thousand, ", " Million, ", " Billion, ", " Trillion, ", " Quadrillion, ",
" Quintillion, "};
public static String spell(int number) {
int rem, placeIndicator = 1;
boolean isNegative = false;
List<String> spelling = new ArrayList<String>();
if (number < 0) {
isNegative = true;
number = Math.abs(number);
}
while (number > 0) {
rem = number % 1000;
number = number / 1000;
spelling.add(suffixSpelling[placeIndicator]);
try {
spelling.add(spellBelow1000(rem));
} catch (SpellingException e) {
System.out.println(e.getMessage());
}
placeIndicator++;
}
StringBuilder sb = new StringBuilder();
if (isNegative) sb.append("Minus ");
for (int i = spelling.size() - 1; i >= 0; i--) {
sb.append(spelling.get(i));
}
return sb.toString();
}
private static String spellBelow1000(int number) throws SpellingException {
if (number < 0 || number >= 1000)
throw new SpellingException("Expecting a number between 0 and 999: " + number);
if (number < 20) {
// if number is a teen,
// find it in teen table and return its equivalent text (word).
return teenSpelling[number];
} else if (number < 100) {
// otherwise, if it is a cent,
// find the most (div) and least (rem) significant digits (MSD/LSD)
int div = (int) number / 10;
int rem = (int) number % 10;
if (rem == 0) {
// if LSD is zero, return the cent key word directly (like
// fifty).
return centSpelling[div-2];
} else {
// otherwise, return the text as cent-teen (like fifty-one)
return centSpelling[div-2] + "-" + teenSpelling[rem];
}
} else {
// otherwise, it is a mil;
// find it's MSD and remaining cent.
int div = number / 100;
int rem = (int) number % 100; // TODO will findbugs detect unnecessary (int)?
// Prepare the mil prefix:
String milText = teenSpelling[div] + " Hundred";
// decide whether to append the cent tail or not.
if (rem == 0) {
// if it does have a non-zero cent, that's it.
// return the mil prefix, for example three hundred:
return milText;
} else {
// otherwise, spell the cent and append it to mil prefix.
// (now, rem is a cent).
// For example, three Hundred and Sixty-Four:
return milText + " and " + spellBelow1000(rem);
}
}
}
}发布于 2012-10-13 20:52:42
你希望在这一行中找到一个bug:
int rem = (int) number % 100; // TODO will findbugs detect unnecessary (int)?是错误的,因为%运算的结果通常不是整数。
在C和C++中,余数运算符只接受整数操作数,但在Java语言中,它也接受浮点操作数。这意味着像double x = 8.2 % 4;这样的语句在Java中是非常有效的,结果可能是一个非整数值。(本例中为0.1999999999999993)
请参阅Java语言规范here。
发布于 2012-10-13 21:19:39
你似乎在findbugs配置方面有问题。我建议通过Sonar使用Findbugs。它更容易配置,你可以得到checkstyle,pmd和一个管理和解决违规的系统。
Sonar findbugs page
发布于 2012-10-13 21:51:00
我认为问题在于你误解了FindBugs做什么以及它的功能是什么。
基本上,FindBugs解析每个类以生成解析树-程序结构在内存中的表示。然后尝试在树中找到与表示不正确或有问题的编程的已知模式匹配的位置。例如:
if (someString == "42") {
....
}FindBugs很可能会告诉你,这里使用'==‘运算符比较字符串是错误的。它所做的是在类中查找运算符为“==”并且其中一个或两个操作数都是字符串的任何表达式节点。它将对已编程为检测和报告的大量模式重复此过程。有些会比这个更复杂,但基本上,FindBug只做了一种结构模式匹配。
FindBugs不能也不能做的是理解你的程序实际上应该做什么。举个例子:
public boolean isOdd(int arg) {
return (arg % 2) == 0;
}对于任何理解简单数学的人来说,这显然是不正确的。但是FindBugs不会注意到的。这是因为FindBugs不知道该方法实际上应该做什么。此外,它不能进行基本的语义分析,而这些分析是确定代码没有实现数学运算所必需的。
我这样做的原因是因为我需要做一个查找bugs的演示,我需要一个应用程序来生成一些bugs来展示它是如何工作的。
也许你需要作弊一点:
这也是值得包括的例子,你知道它找不到...这样你就可以解释Findbugs的局限性。
https://stackoverflow.com/questions/12873045
复制相似问题