簡體   English   中英

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

[英]How to fix FindBugs' warning "Use of non-localized String.toUpperCase() or String.toLowerCase()"?

我有以下一段代碼:

/**
 * 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)

如果我可以確定所有文件名總是只有拉丁字母的名稱,我該如何正確修復該警告(正確的方法)?

添加 Locale.ENGLISH(或 Locale.ROOT)是修復錯誤的正確方法,因此如果添加后 FindBugs 仍然報告該錯誤,那么您可能已經在 FindBugs 中發現了錯誤。

我有同樣的錯誤。 試試這個:

yourString.toLowerCase(Locale.getDefault());

像下面這樣使用,警告就會消失。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM