对于 FindBugs,jar305.jar 中的注释是否应该优于 annotation.jar 中的类似注释? [英] Should annotations in jar305.jar be preferred over similar annotations in annotation.jar for FindBugs?
问题描述
在 FindBugs 发行版中,annotations.jar
不是 jsr305.jar
的子集.但是,几个注释似乎是重复的(完全或非常接近).如果可以选择,我是否应该更喜欢 jsr305.jar
中的注释?
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?
请注意,我不只是想知道使用来自 jsr305.jar
的注释会更好",因为它们代表一个标准.相反,如果我更喜欢特定注释的 jsr305.jar
版本,我想知道 FindBugs 工具是否会执行相同(或更好)的分析.可能的情况是某些 jsr305.jar
注释应该是首选,但其他注释不应该.
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.
我使用的是 FindBugs 1.3.9,它是 最新版本写作.在此版本中,我看到以下选项(如果有其他选项,请更新此表格):
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 大写)
edu.umd.cs.findbugs.annotations.Nullable →javax.annotation.Nullable
edu.umd.cs.findbugs.annotations.When →javax.annotation.meta.When
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
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
推荐答案
是的,如果可能,您应该更喜欢 JSR305 注释.为什么?因为 JSR305 是一个标准,在可能的情况下坚持这些标准是完全有意义的.在移植我自己的应用程序时,我没有注意到任何问题或行为上的变化.此外,您甚至可以定义自己的@NotNull 注释,findbugs 会提取它(只要您将其命名为 NotNull),请参阅 此博客条目了解更多详情.
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.
据我查看源文件所见,findbugs 在内部使用相同的分析方法.仅根据注释名称进行调度.正如上面链接的 博客文章 中所述,请查看 edu.umd.cs.findbugs.ba.NullnessAnnotation 和 NullnessAnnotationDatabase 类,以初步了解这是如何在内部完成的.看看这个包,你会发现其他注释的类似类,比如 jcip 的.
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.
所以从实现的角度来看,这真的无关紧要.对于仍然不确定要使用哪些注解的每个人,我会考虑使用标准注解或自定义注解以避免他们的代码依赖于 findbugs 库.
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.
这篇关于对于 FindBugs,jar305.jar 中的注释是否应该优于 annotation.jar 中的类似注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!