Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

java - Found 'UR'-anomaly for variable 'city' (lines 'XX'-'YY')

Small question regarding how to fix a 'UR'-anomaly please.

On this very simple piece of code:

 private final Environment          environment;
    private final Map<String, Foo> partnerConfiguration;
    @Value("something")
    private List<String> goodCities;


    @PostConstruct
    public void setup() {
        for (final String city : goodCities) {
            final String   nameAndCountry = environment.getProperty(PROPERTY_PREFIX + city);
            final String[] nameAndCountrySplit  = Objects.requireNonNullElse(nameAndCountry, "").split(";");
            final String   name        = nameAndCountrySplit[0];
            final String   country   = nameAndCountrySplit[1];
            partnerConfiguration.put(city, new Foo(name, country));
        }
    }

I got flagged with 'UR'-anomaly on those lines:

   public void setup() {
        for (final String city : goodCities) {
            final String   nameAndCountry = environment.getProperty(PROPERTY_PREFIX + city);

I tried using a functional version of it, refactoring by creating a sub method, changing variable names, no luck.

I know static analysis tools are just tools, but I am quite interested what would be the code level answer on how to fix this "anomaly" please.

Thank you

question from:https://stackoverflow.com/questions/65829316/found-ur-anomaly-for-variable-city-lines-xx-yy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

7 year old bug.

Your code is completely fine, it's the linter tool that misunderstands java constructs and is now throwing out irrelevant warnings.

Stop using this tool. It's unmaintained, and as a result, it just doesn't understand any java code with 'modern' features anymore, and this isn't even a modern language construct (it was when PMD was in its final days of maintenance).

There are many other java linter tools out there. Heck, most IDEs ship with a decent set already.

NB: Your question doesn't mention the word 'pmd' and isn't tagged 'pmd', so I'm guessing. You should update your question. Next time you ask on SO, please be aware that those reading your question do not have a crystal ball and have no idea what kind of setup you're running. If this warning is being printed out not by PMD but something else, this answer is probably not nearly as useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...