How to fix FindBugs' warning "Use of non-localized String.toUpperCase() or String.toLowerCase()"?
Asked Answered
A

3

6

I have following piece of code:

/**
 * 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 complains about the toLowerCase call:

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

How can I correctly fix that warning (the proper way), if I can be sure that all file names will always have names with Latin letters only?

Agronomy answered 5/7, 2015 at 20:25 Comment(3)
Since all the characters you're interested in are present in the English alphabet, you can just ignore the warning. Alternatively, don't convert the string to lower case, use a case-insensitive regular expression instead.Pruinose
Adding Locale.ENGLISH (or Locale.ROOT) is the proper way to fix the error, so if it's still happening after you doing that, then you might have found a bug in FindBugs itself.Sheronsherourd
@bkail Thanks. Please submit your comment as an answer and I'll accept it.Agronomy
S
10

Adding Locale.ENGLISH (or Locale.ROOT) is the proper way to fix the error, so if FindBugs is still reporting that error after you add that, then you might have found a bug in FindBugs itself.

Sheronsherourd answered 6/7, 2015 at 4:51 Comment(2)
As a FindBugs developer I doubt that it's a FindBugs bug. Looks like the OP did not rerun the analysis for some reason and looking on the old report. Probably he uses IDE which should rerun the analysis automatically, but for some reason did not do this. Or he rerun the analysis, but did not recompile the code before it (FindBugs analyzes compiled code, not the source), so while he has updated source, the bytecode is still old.Wedded
@TagirValeev, you might add it as an answerWithout
A
1

I had the same error. Try this one:

yourString.toLowerCase(Locale.getDefault());
Ascogonium answered 22/4, 2020 at 12:39 Comment(1)
This does not seem a good idea. Locale.ENGLISH might be there for purpose and you are suggesting that OP would write some random stuff just to get FindBugs to shut up.Natka
K
-2

Use like below and warning will go.

import org.apache.commons.lang3.StringUtils;
filename = StringUtils.upperCase(filename);
Kimberli answered 1/9, 2016 at 4:13 Comment(1)
Downvoted - if you want the warning "to go away" you can simply ignore the FindBugs report.Melcher

© 2022 - 2025 — McMap. All rights reserved.