Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should annotations in jar305.jar be preferred over similar annotations in annotation.jar for FindBugs?

In the FindBugs distribution, annotations.jar is not a subset of jsr305.jar. However, several annotations seem to be duplicated (either exactly, or very closely). Should I prefer an annotation in jsr305.jar if I have a choice?

Note that I'm not just interested in knowing that it would be "better" to use annotations from jsr305.jar simply because they represent a standard. Rather, I want to know whether the FindBugs tool will perform the same (or better) analysis if I prefer the jsr305.jar version of a particular annotation. It could be the case that some jsr305.jar annotations should be preferred, but others should not.

I'm using FindBugs 1.3.9 which is the most recent version as of this writing. With this version, I see the following choices (please update this table if there are others):

edu.umd.cs.findbugs.annotations.CheckForNull → javax.annotation.CheckForNull
edu.umd.cs.findbugs.annotations.CheckReturnValue → javax.annotation.CheckReturnValue
edu.umd.cs.findbugs.annotations.NonNull → javax.annotation.Nonnull (NB capitalization)
edu.umd.cs.findbugs.annotations.Nullable → javax.annotation.Nullable
edu.umd.cs.findbugs.annotations.When → javax.annotation.meta.When

Also, all of the JCIP annotations are duplicated:

net.jcip.annotations.GuardedBy → javax.annotation.concurrent.GuardedBy
net.jcip.annotations.Immutable → javax.annotation.concurrent.Immutable
net.jcip.annotations.NotThreadSafe → javax.annotation.concurrent.NotThreadSafe
net.jcip.annotations.ThreadSafe → javax.annotation.concurrent.ThreadSafe

like image 281
Greg Mattes Avatar asked Jun 12 '11 15:06

Greg Mattes


1 Answers

Yes, you should prefer the JSR305 annotation if possible. Why? Because the JSR305 is a standard and it makes total sense to stick to the standards where possible. While porting my own applications I did not notice any problems or changes in behavior. Moreover, you can even define your own @NotNull annotation and findbugs will pick it up (as long as you name it NotNull) see this blog entry for more details.

As far as I can see by looking at the sources, findbugs is using the same analysis methods internally. The dispatching is done only based on the annotations name. As mentioned in the blog post linked above, have a look at the edu.umd.cs.findbugs.ba.NullnessAnnotation and NullnessAnnotationDatabase classes to get an initial view how this is done internally. Have a look at this package and you'll find similar classes for the other annotations like the jcip ones.

So from an implementations point of view it really doesn't matter. For everybody still not sure which annotations to use I would consider using the standard annotations or self defined ones to avoid their code being dependent on the findbugs library.

like image 186
Steffen Avatar answered Sep 22 '22 03:09

Steffen