我应该使用哪种@NotNull Java注释? [英] Which @NotNull Java annotation should I use?

查看:10298
本文介绍了我应该使用哪种@NotNull Java注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待让我的code更具可读性,以及像IDE code检查和/或静态code分析(FindBugs的和声纳),以避免NullPointerException异常使用的工具。许多工具,似乎对对方的不兼容 @NotNull / @NonNull / @Nonnull 注释,并列出他们都在我的code将是可怕的阅读。其中之一任何建议是最好的?下面是我发现相当于注释的列表:

I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others' @NotNull/@NonNull/@Nonnull annotation and listing all of them in my code would be terrible to read. Any suggestions of which one is the 'best'? Here is the list of equivalent annotations I've found:


  • javax.validation.constraints.NotNull 结果
    创建了运行时验证,而不是静态分析。的结果
    <一href=\"http://download.oracle.com/javaee/6/api/javax/validation/constraints/NotNull.html\">documentation

edu.umd.cs.findbugs.annotations.NonNull 结果
按FindBugs的静态分析使用,因此声纳的结果
文档

javax.annotation.Nonnull 结果
这可能与FindBugs的工作太多,但JSR-305是无效的。的结果
<一href=\"http://$c$c.google.com/p/jsr-305/source/browse/trunk/ri/src/main/java/javax/annotation/Nonnull.java\">source

org.jetbrains.annotations.NotNull 结果
由IntelliJ IDEA的IDE静态分析中使用。的结果
<一href=\"http://www.jetbrains.com/idea/webhelp/nullable-and-notnull-annotations.html\">documentation

lombok.NonNull 结果
用于控制项目龙目岛code产生。的结果
占位符注释,因为没有标准。的结果

文档

android.support.annotation.NonNull 结果
标记标注在Android中使用,支持的注解包提供的结果
<一href=\"https://developer.android.com/reference/android/support/annotation/NonNull.html\">documentation

推荐答案

我只会使用的javax 命名空间(尽管我热爱龙目岛和IntelliJ正在做下的事)。否则,你可能会在创造比运行时给你的东西是pretty多的语义的其他东西的依赖。也许对于一些项目,这是确定,但会是一个交易断路器我。

I would only use things under the javax namespace (even though I love what Lombok and IntelliJ are doing). Otherwise, you might be creating a dependency on something other than what the run-time gives you for something that is pretty much semantics. Maybe for some projects, that's ok, but that'd be a deal-breaker for me.

我会使用 javax.validation.constraints.NotNull ,因为这是已经在这里与Java EE 6

I would use javax.validation.constraints.NotNull because that's already here with Java EE 6.

javax.annotation.NonNull 可能不会在这里,直到8的Java(斯蒂芬指出)。而其他不规范标注。

The javax.annotation.NonNull might not be here until Java 8 (as Stephen pointed out). And the others are not standard annotations.

这本来是很好,如果注释是可扩展的。这样,你可以定义你自己的非空标注继承/从任何延长。然后,当标准得到理顺,所有你需要做的是重新定义自己的自定义注释。

It would have been nice if annotations were extensible. That way you could define your own non-null annotation inheriting/extending from anything. Then when standards get ironed out, all you would have to do would be to redefine your own custom annotation.

不幸的是,这不是这种情况。

Unfortunately that's not the case.

这篇关于我应该使用哪种@NotNull Java注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆