对于 FindBugs,jar305.jar 中的注释是否应该优先于 comment.jar 中的类似注释?

发布于 2024-11-15 05:19:22 字数 1501 浏览 3 评论 0原文

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

另外,所有 JCIP 注释重复:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-11-22 05:19:22

是的,如果可能的话,您应该更喜欢 JSR305 注释。为什么?因为 JSR305 是一个标准,因此尽可能遵守该标准是完全有意义的。在移植我自己的应用程序时,我没有注意到任何问题或行为变化。此外,您甚至可以定义自己的 @NotNull 注释,findbugs 会选择它(只要您将其命名为 NotNull),请参阅 此博客条目了解更多详细信息

据我通过查看源代码发现,findbugs 在内部使用相同的分析方法。仅根据注释名称进行调度。正如上面链接的博客文章中提到的,请查看edu. umd.cs.findbugs.ba.NullnessAnnotationNullnessAnnotationDatabase 类,以初步了解这是如何在内部完成的。看一下这个包,您会发现其他注释(例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文