10

I've been using the @Nonnull and @Nullable annotations on methods to give other programmers (and myself!) a clue about what a method can return. I finally decided to actually run Findbugs on a class (IntelliJ - FindBugs-IDEA v1.0.1), and I don't understand the behavior I'm seeing. The documentation hasn't helped either.

Let's say I have the following example code:

import javax.annotation.Nonnull;

public class Main {

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

    @Nonnull
    public static String myFunc(){
        return new String("foo");
    }

    @Nonnull
    public static String myFunc2(){
        return "foo";
    }
}

Findbugs flags myFunc()'s return statement as having "Redundant nullcheck of value known to be non-null", but is happy with myFunc2().

Is it expected that findbugs sees these differently? (Link to documentation would be appreciated) Am I completely misunderstanding the use of @Nonnull on methods?

[edit]

After some research, I've decided that the org.jetbrains @Contract annotation (with contract violations changed to errors) will better serve my needs. Thank you Guillaume F. for your help!

KathyA.
  • 671
  • 6
  • 14
  • Neither can be null, but it's basically never necessary to use `new String("string literal")`. – Andy Turner Jan 26 '17 at 22:16
  • Obviously. It just makes a much simpler example than my actual code. – KathyA. Jan 26 '17 at 22:21
  • But it sounds like you're omitting pertinent details: are they `String`s, allowing you to use string literals; or are they something else? – Andy Turner Jan 26 '17 at 22:23
  • I've added the complete class used to demonstrate the problem. It's not the use of the functions that findbugs is complaining about -- the error shows up on the 'return' line of myFunc. Remember, this is just an example. – KathyA. Jan 26 '17 at 22:31
  • Just another Findbugs bug. – user207421 Jan 27 '17 at 01:26

1 Answers1

3

You have to understand what redundant Null-Check means. When Findbugs gives you this warning, it means you did a Null-Check two times, and the second time is not necessary. Which matches your example.

In the first code, new String("foo") does an implicit null check, because new String(null) will throw an Exception. So this new String object is checked non-null implicitly. Then you do another @Nonnull check while leaving the method. Findbugs sees this and gives you the warning.

In your second code, there is no such control since you return a raw String and do the @Nonnull check only once. Everything is fine.


Maybe you will want to use edu.umd.cs.findbugs.annotations.NonNull instead. This one will just hint Findbugs that you want a non-null result, without having a real check.

If you are using Maven:

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>annotations</artifactId>
        <version>${findbugs-annotations.version}</version>
        <scope>provided</scope>
    </dependency>

https://blogs.oracle.com/java-platform-group/entry/java_8_s_new_type

Which @NotNull Java annotation should I use?

Community
  • 1
  • 1
Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
  • Thanks -- your explanation makes sense. Unfortunately, using edu.umd.cs.findbugs.annotations.NonNull doesn't change the result -- not only does it still complain about the redundant check, it also shows as deprecated, and recommends using javax.annotation.Nonnull. I think my best bet will be to search for an alternative to Findbugs, since this behavior is not what I want. – KathyA. Jan 27 '17 at 15:17
  • You can disable this specific warning in Findbugs options if it bothers you, or you can use `@SuppressWarnings` : http://stackoverflow.com/questions/1829904/is-there-a-way-to-ignore-a-single-findbugs-warning I seen your edit, glad that you found a solution. If my answer helped, can you mark it as such? Thank you. – Guillaume F. Jan 27 '17 at 19:31