Wednesday, March 7, 2012

FindBugs and JSR-305

Suppose that group of developers work in parallel on parts of big project - some developers are working on service implementation, while others are working on code using this service. Both groups agreed on service API, and started working separately, having in mind the API assumptions...

Do you think this story will have happy end? Well, ... - maybe :) - there are tools which can help achieve it :) - one of them is FindBugs, supported with JSR-305 (annotations for software defect detection).

Let's take a look at the service API contract:

As you see there are annotations like @Nonnull or @CheckForNull added to the service method signatures. The purpose of their usage is to define the requirements for the method parameters (ex. identifier parameter cannot be null), and the expectations for the values returned by methods (ex. service method result can be null and you should check it in your code).

So what? - you may ask - should I check them in the code by myself or trust the co-workers that they will use the guidelines defined by those annotations? Of course not :) - trust no one, use the tools which will verify the API assumptions, like FindBugs.

Suppose that we have following service API usage:

Let's try to verify the code against the service API assumptions:

FindBugs will analyze your code, and switch to the FindBugs perspective showing potential problems:

Null passed for nonnull parameter
Possible null pointer dereference

Similar way, guys writing the service code may verify their work against defined API assumptions, for ex. if you run FindBugs for the very early version of service implementation:

Following error will be found:

As you see, nothing can hide from the FindBugs and his ally - JSR-305 ;)

Few links for the dessert:


This article has been republished on Java Code Geeks (03/15/2012) and Dzone's Javalobby (08/29/2012).


  1. Hi Michał,

    I ran across your blog today while looking for content for DZone's Javalobby ( and wondered if you would be interested in having some of your work republished on our site. If so, please send me an email and I can provide some more details.


    Chris Smith

  2. Isn't JSR-305 moribund? As such, is it really advisable to use these annotations going forward?

  3. See my AspectJ aspect I wrote for runtime validation: