Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. Which @Nullable should I use to mark return value with one?

My java method can return null and non null results. I want mark method with @Nullable annotation to make it more readable.

I used com.sun.istack.internal.Nullable but sonar says Classes from "sun.*" packages should not be used. Code smell Major squid:S1191.

I try to find more similar annotations, but there are variants from different IDE, not from java vendor.

Does java (oracle) provides alternatives for nullable annotation or I should use third party libraries only? If I have to use third party library to have an @Nullable which one should be good?

like image 311
Sergii Avatar asked Jul 25 '17 09:07

Sergii


2 Answers

Well, I think for Sonar you could use edu.umd.cs.findbugs.annotations.*, which are deprecated and advise you to use javax.annotation.Nullable.

I mainly use the Jetbrains ones, https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html, but my main goal is the analysis in IntelliJ.

like image 179
David Vonka Avatar answered Oct 27 '22 01:10

David Vonka


If you can, consider using Optional<T> as the return type to make it more readable. You can express optionality utilizing the type system and benefit from the rich monadic-like API instead of wondering which annotation is the right one.

like image 21
Grzegorz Piwowarek Avatar answered Oct 26 '22 23:10

Grzegorz Piwowarek