这是真正的警告还是过于敏感的皮棉?

Ken*_*Y-N 3 java findbugs

我有这个方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

对于标记的行/*WARN*/,FindBugs 3.0.0告诉我:

com.foo.MyClass.compare(String,String,String)中整数引用的可疑比较[最可怕(1),高可信度]

我认为代码int没问题Integer,因为我比较s而不是s,所以我可以安全地@SuppressWarnings在这一行吗?

chr*_*ke- 5

因为compareTo返回一个原语int,你是对的,这个代码很好.我建议将此作为针对FindBugs的错误提交.

请注意,您还会为变量导致不必要的自动装箱.您可以将它们存储在ints中并使用Integer.compare(propertyValue, operandValue).