如何修复 FindBugs 的警告 "Use of non-localized String.toUpperCase() or 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 中发现了错误本身。
像下面这样使用,警告就会消失。
import org.apache.commons.lang3.StringUtils;
filename = StringUtils.upperCase(filename);
我有同样的错误。试试这个:
yourString.toLowerCase(Locale.getDefault());
我有以下代码:
/**
* 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 中发现了错误本身。
像下面这样使用,警告就会消失。
import org.apache.commons.lang3.StringUtils;
filename = StringUtils.upperCase(filename);
我有同样的错误。试试这个:
yourString.toLowerCase(Locale.getDefault());