Is there some way to assume @Nullable as default? (using FindBugs or any other free tool).

Posted by alex2k8 on Stack Overflow See other posts from Stack Overflow or by alex2k8
Published on 2010-05-07T21:48:57Z Indexed on 2010/05/07 21:58 UTC
Read the original article Hit count: 159

Filed under:
|
|

Consider such code

public void m1(String text) {
    if(text == null)
        text = "<empty>";

    System.out.println(text.toLowerCase());
}

And this is a buggy version:

public void m1(String text) {
    System.out.println(text.toLowerCase());
}

If null value passed, the NullPointerException may be thrown. I would like the static-analysis tool (e.g. FindBugs) to report this issue. Unsuccessfully the FindBugs (at least by default) requires me to specify @Nullable annotation explicitly.

public void m1(@Nullable String text) {
    System.out.println(text.toLowerCase()); // FindBugs: text must be nonnull but is marked as nullable
}

The problem is that if I forget to annotate it, the bug will be missed!!!

How can I make the FindBugs (or any other free tool) to assume @Nullable by default?

© Stack Overflow or respective owner

Related posts about java

Related posts about static-analysis