JSR 305

JSR-305

This Google Tech Talk was about JSR-305, a JSR that was adopted and will be in Java 7 (I believe). This JSR involves added annotations for software defect detection - essentially, it aims to provide standard annotations that tools can understand and use to help detect errors in your code. Some of these have been implemented nonstandardly in tools such as IntelliJ, such as the @Nonnull annotation.

This talk went over what would be provided and examples of why this would be useful, instead of going into implementation which would be tool-dependent. He did mention one possibility for enforcing these annotations at runtime, but I think it would have been interesting to go more in-depth on this. Oh well, there’s only so much you can cover in an hour.

I’ve known about some of the annotations before as ones that were coming to Java7, eg @Nonnull, but not some of the others. While (hopefully) the ones built-in to Java7 will have compiler support, since the JSR includes a way of defining your own classes it would be nice if there was a way to be able to tell javac how to verify those, reducing the need for external tools. On second thought, this seems like informing the compiler how to check for violations of these annotations seem rather difficult; maybe it should be left to tools developers.

One thing mentioned in this presentation that I was unaware of was class- and package-level default annotations. When I heard about @Nonnull, I liked the idea, but it seemed like it would end up making Java even more verbose, since you’d usually want to add it to essentially every variable. Default annotations allow you to annotate a class or package and essentially have variables ‘inherit’ from this default; this allows you to make every variable in one class @Nonnull with only one annotation, much better than if each variable declaration needed an annotation.

Tags: ,

Leave a Reply