对于 FindBugs,jar305.jar 中的注释是否应该优先于 comment.jar 中的类似注释?
在 FindBugs 发行版中,annotations.jar
不是 jsr305.jar 的子集。然而,有几个注释似乎是重复的(要么完全重复,要么非常接近)。如果可以选择的话,我应该更喜欢 jsr305.jar
中的注释吗?
请注意,我不仅仅想知道使用 jsr305.jar 中的注释会“更好”,因为它们代表了一个标准。相反,我想知道如果我更喜欢特定注释的 jsr305.jar
版本,FindBugs 工具是否会执行相同(或更好)的分析。可能的情况是,某些 jsr305.jar
注释应该是首选,但其他注释则不应该。
我使用的是 FindBugs 1.3.9,这是撰写本文时的最新版本。在此版本中,我看到以下选项(如果有其他选项,请更新此表格):
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 大写)
edu.umd.cs.findbugs.annotations.Nullable → javax.annotation.Nullable
edu.umd.cs.findbugs.annotations.When → javax.annotation.meta.When
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果可能的话,您应该更喜欢 JSR305 注释。为什么?因为 JSR305 是一个标准,因此尽可能遵守该标准是完全有意义的。在移植我自己的应用程序时,我没有注意到任何问题或行为变化。此外,您甚至可以定义自己的 @NotNull 注释,findbugs 会选择它(只要您将其命名为 NotNull),请参阅 此博客条目了解更多详细信息。
据我通过查看源代码发现,findbugs 在内部使用相同的分析方法。仅根据注释名称进行调度。正如上面链接的博客文章中提到的,请查看edu. umd.cs.findbugs.ba.NullnessAnnotation 和 NullnessAnnotationDatabase 类,以初步了解这是如何在内部完成的。看一下这个包,您会发现其他注释(例如 jcip 注释)的类似类。
因此,从实现的角度来看,这并不重要。对于仍然不确定要使用哪些注释的每个人,我会考虑使用标准注释或自定义注释,以避免他们的代码依赖于 findbugs 库。
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.