我有这样的方法:这是一个真正的警告或过敏皮棉?
private Boolean compare(String property, String relationOperator,
String operand) {
Integer propertyValue = NumberUtils.toInt(property);
Integer operandValue = NumberUtils.toInt(operand);
switch (relationOperator)
{
case "<": return propertyValue.compareTo(operandValue) < 0;
case "<=": return propertyValue.compareTo(operandValue) <= 0;
/*WARN*/case "=": return propertyValue.compareTo(operandValue) == 0;
case ">=": return propertyValue.compareTo(operandValue) >= 0;
case ">": return propertyValue.compareTo(operandValue) > 0;
case "!=": return propertyValue.compareTo(operandValue) != 0;
}
return Boolean.FALSE;
}
对于标/*WARN*/
行,FindBugs的3.0.0告诉我:
在 com.foo.MyClass.compare(字符串整数引用的可疑比较,字符串,字符串)[最可怕的(1),高 信心]
我认为的代码是OK,因为我比较int
不是Integer
s,所以我可以在这条线上安全地@SuppressWarnings
?
谢谢,这让警告消失。我最初选择'Integer'来匹配使用'String.compareTo()'的现有代码。 – 2014-10-20 03:21:52
因为Java现在支持切换“String”,所以这里'switch'上的一串'if..else'链没有任何优势。如果需要重构,那么将比较放到枚举中。 – chrylis 2014-10-20 03:32:06