tlinkowski / basic-annotations

A couple of basic Java annotations that cover null safety (via JSR 305), collection mutability (via Kotlin), and a few other concepts.
http://annotation.tlinkowski.pl/
Apache License 2.0
9 stars 2 forks source link

Static verification support #21

Open nedtwigg opened 4 years ago

nedtwigg commented 4 years ago

I'm writing java-only code, and I want it to be friendly to Kotlin users. I also would like the same Kotlin non-null semantics in Java, as much as possible.

I would like to apply [SpotBugs, Checker Framework, whatever], and have it yell at me if I'm assigning null to a field or variable anywhere that wasn't annotated as @OrNull.

Do you have any experience / recommendations for using your annotations with other tools? I really enjoyed your https://dzone.com/articles/when-to-use-jsr-305-for-nullability-in-java article, thanks for digging in and sorting through the various efforts!

tlinkowski commented 4 years ago

Hi Ned, thanks for your kind words!

I agree it would be very useful to have a simple and universal way of enforcing the nullability contract at compile-time.

I haven't truly explored this area so far, but I gave it a cursory look right now, and here's what I found...

SpotBugs

I wasn't able to find out in the docs if SpotBugs honors custom JSR-305-based nullability annotations (i.e. @NonNullPackage and @NullOr). Perhaps it can handle them out of the box like IntelliJ and Kotlin compiler (especially since SpotBugs is a successor of FindBugs, which relied on JSR-305). I guess you'd need to run SpotBugs and verify this.

Theoretically, one could also implement a custom SpotBugs plugin for it, but it'd seem an overkill to me.

Checker Framework

Checker Framework clearly supports non-meta JSR-305 annotations like @javax.annotation.Nonnull.

However, I wasn't able to find out in the docs if it also supports meta JSR-305 annotations (@TypeQualifierDefault or @TypeQualifierNickname, which I used to define my annotations). So again, I guess you'd need to try it out.

IDEA Inspection Plugin

If you're using Gradle, one option that might work as expected (although I haven't tried it) could be IntelliJ IDEA inspection plugin (still in the "incubator", though). Since IntelliJ honors my annotations out of the box, simply turning on some nullable inspections should suffice.


Hope it helps.