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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With