我有這樣的方法:這是一個真正的警告或過敏皮棉?
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