False positive in SpotBugs (D'Oh! Nonsensical invocation - DMI_DOH)?

162 views Asked by At

The code below is generating the SpotBugs warning, "D'Oh! A nonsensical invocation of isNullOrEmpty(String) in com.myco.Launcher.validArguments() [Scary(7), Normal confidence]" (DMI_DOH).

public class Launcher {
    @Injected(name="-f", description="optional name of file")
    private static String filename;

    public static void main(String[] args) {
        if (validArguments()) {
            ...
    }

    private boolean validArguments() {
        boolean result = true;
        if (com.google.common.base.Strings.isNullOrEmpty(Launcher.filename)) {
            result = false;
        }
        else if (...) {
            ...
        }
        return result;
    }
}

filename is a value collected from the command line as I'm using a CLI library to set that value for me if/when it's supplied. If the user doesn't supply the value on the command line, the field will be null. My hunch is this is a false positive and I'll need to suppress this warning. If not, please help me understand how this invocation is a blunder.

I removed the static modifier from the field and it appeared to resolve it. It seems this warning is best suited for invocations such as:

Strings.isNullOrEmpty("abcd");

That's a blunder. LOL!

Honestly, I don't see why having this field static is a mistake. My CLI library sets it correctly either way. And without the annotation, it's going to be null either way, but non-static somehow satisfies it?

0

There are 0 answers