How to use @Nullable and @Nonnull annotations more effectively?

后端 未结 9 441
你的背包
你的背包 2020-12-12 12:01

I can see that @Nullable and @Nonnull annotations could be helpful in preventing NullPointerExceptions but they do not propag

相关标签:
9条回答
  • 2020-12-12 12:45

    What I do in my projects is to activate the following option in the "Constant conditions & exceptions" code inspection:
    Suggest @Nullable annotation for methods that may possibly return null and report nullable values passed to non-annotated parameters

    When activated, all non-annotated parameters will be treated as non-null and thus you will also see a warning on your indirect call:

    clazz.indirectPathToA(null); 
    

    For even stronger checks the Checker Framework may be a good choice (see this nice tutorial.
    Note: I have not used that yet and there may be problems with the Jack compiler: see this bugreport

    0 讨论(0)
  • 2020-12-12 12:49

    Compiling the original example in Eclipse at compliance 1.8 and with annotation based null analysis enabled, we get this warning:

        directPathToA(y);
                      ^
    Null type safety (type annotations): The expression of type 'Integer' needs unchecked conversion to conform to '@NonNull Integer'
    

    This warning is worded in analogy to those warnings you get when mixing generified code with legacy code using raw types ("unchecked conversion"). We have the exact same situation here: method indirectPathToA() has a "legacy" signature in that it doesn't specify any null contract. Tools can easily report this, so they will chase you down all alleys where null annotations need to be propagated but aren't yet.

    And when using a clever @NonNullByDefault we don't even have to say this every time.

    In other words: whether or not null annotations "propagate very far" may depend on the tool you use, and on how rigorously you attend to all the warnings issued by the tool. With TYPE_USE null annotations you finally have the option to let the tool warn you about every possible NPE in your program, because nullness has become an intrisic property of the type system.

    0 讨论(0)
  • 2020-12-12 12:53

    Other than your IDE giving you hints when you pass null to methods that expect the argument to not be null, there are further advantages:

    • Static code analysis tools can test the same as your IDE (e.g. FindBugs)
    • You can use AOP to check these assertions

    This can help your code be more maintainable (since you do not need null checks) and less error-prone.

    0 讨论(0)
  • 2020-12-12 12:56

    I agree that the annotations "don't propagate very far". However, I see the mistake on the programmer's side.

    I understand the Nonnull annotation as documentation. The following method expresses that is requires (as a precondition) a non-null argument x.

        public void directPathToA(@Nonnull Integer x){
            x.toString(); // do stuff to x        
        }
    

    The following code snippet then contains a bug. The method calls directPathToA() without enforcing that y is non-null (that is, it does not guarantee the precondition of the called method). One possibility is to add a Nonnull annotation as well to indirectPathToA() (propagating the precondition). Possibility two is to check for the nullity of y in indirectPathToA() and avoid the call to directPathToA() when y is null.

        public void indirectPathToA(Integer y){
            directPathToA(y);
        }
    
    0 讨论(0)
  • 2020-12-12 13:02

    I think this original question indirectly points to a general recommendation that run-time null-pointer check is still needed, even though @NonNull is used. Refer to the following link:

    Java 8's new Type Annotations

    In the above blog, it is recommended that:

    Optional Type Annotations are not a substitute for runtime validation Before Type Annotations, the primary location for describing things like nullability or ranges was in the javadoc. With Type annotations, this communication comes into the bytecode in a way for compile-time verification. Your code should still perform runtime validation.

    0 讨论(0)
  • 2020-12-12 13:04

    In Java I'd use Guava's Optional type. Being an actual type you get compiler guarantees about its use. It's easy to bypass it and obtain a NullPointerException, but at least the signature of the method clearly communicates what it expects as an argument or what it might return.

    0 讨论(0)
提交回复
热议问题