如何修复FindBugs的警告"使用非本地化的String.toUpperCase()或String.toLowerCase()"?

DP_*_*DP_ 2 java findbugs

我有以下代码:

/**
 * Performs the filename extension check (command-line argument validity).
 * @param valid True, if the check should be performed.
 * @param filename File name to test.
 * @return False, if the test was done and the filename does not end with
 *  ".xml". Value of valid otherwise.
 */
private boolean checkFileNameExtension(final boolean valid,
    final String filename) {
    boolean result = valid;
    if (valid
        && !filename.toLowerCase(Locale.ENGLISH).endsWith(".xml")) {
        this.logger.error("File doesn't have XML extension.");
        result = false;
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

FindBugs抱怨toLowerCase电话:

[WARNING] FindBugs: L I Dm: Use of non-localized String.toUpperCase() or 
String.toLowerCase() in [...]checkFileNameExtension(boolean, String)
Run Code Online (Sandbox Code Playgroud)

如果我可以确定所有文件名始终只有拉丁字母的名称,我该如何正确修复该警告(正确的方法)?

Bre*_*ail 5

添加Locale.ENGLISH(或Locale.ROOT)是修复错误的正确方法,因此如果FindBugs在添加错误后仍然报告该错误,那么您可能在FindBugs本身中发现了一个错误.

  • 作为 FindBugs 开发人员,我怀疑这是一个 FindBugs 错误。看起来 OP 由于某种原因没有重新运行分析并查看旧报告。可能他使用 IDE 应该自动重新运行分析,但由于某种原因没有这样做。或者他重新运行分析,但没有重新编译之前的代码(FindBugs 分析编译后的代码,而不是源代码),所以虽然他更新了源代码,但字节码仍然很旧。 (3认同)