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

时间:2015-07-05 20:25:01

标签: 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;
}

FindBugs抱怨toLowerCase电话:

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

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

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

我有同样的错误。试试这个:

yourString.toLowerCase(Locale.getDefault());

答案 2 :(得分:-1)

使用如下所示并发出警告。

import org.apache.commons.lang3.StringUtils;
filename = StringUtils.upperCase(filename);