13

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

Community
  • 1
  • 1
Greg Mattes
  • 33,090
  • 15
  • 73
  • 105
  • 1
    Sorry, I haven't realized the scope of your question. However, I converted all of my projects using findbugs own annotations to the standardized ones and did not notice any changes. Just my personal observations. If I remember correctly findbugs uses the same code for verification. I will have a look at the code later. Meanwhile, I voted to delete my answer, since it is not really answering your question :) – Steffen Jun 12 '11 at 17:57
  • Yes, but your comment makes a pretty good answer! Consider making it an answer please. Thanks again. – Greg Mattes Jun 12 '11 at 18:12
  • I modified my answer. Still searching for some self-explaning code parts and a couple of good links. I don't think it makes much sense pointing to numerous class files one has to follow to understand my point. – Steffen Jun 13 '11 at 10:01

1 Answers1

10

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.

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
Steffen
  • 8,033
  • 1
  • 28
  • 34
  • Thanks for your response, but please re-read my question completely. I'm not so much interested in preferring jsr305 stuff simply because it's a standard. I agree with you that, ceteris parabus, standards should be preferred, but I want to know whether FindBugs has identical analysis support for similar/identical annotations. – Greg Mattes Jun 12 '11 at 17:36
  • I had no idea it just matched by name (obviously I've not studied the sources). Thanks for the insight! Now I wonder... does it do case sensitive matching? (for Nonnull :) – Greg Mattes Jun 13 '11 at 10:44
  • Additionally, the annotations in `edu.umd.cs.findbugs.annotations` are marked as `@Deprecated`. – Dan R. Feb 09 '12 at 22:12