Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 83f795a

Browse filesBrowse files
committed
优化"我应该用哪一个@NotNull注解?"
1 parent 49fe4b9 commit 83f795a
Copy full SHA for 83f795a

File tree

Expand file treeCollapse file tree

3 files changed

+52
-55
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+52
-55
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ stackoverflow-Java-top-qa
1414
- 另外,为了避免多人重复新翻译一个问题,你可以提issue,说明你计划翻译的问题及时间点,我可以先更新到下面的”未翻译问题“中,说明已有人领了这个问题。当然,也不一定要提issue,一般情况下,只要及时提pr,我及时审核,出现”撞车“的概率并不高。
1515

1616
一些基本的约定:
17-
- 文档的文件名,和stackoverflow上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
17+
- 文档的文件名,和stackoverflowhich-notnull-java-annotation-should-i-usew上的url保持一致。例如,http://stackoverflow.com/questions/8710619/java-operator 的文件名, 就是java-operator.md
1818
- 在每篇翻译文档内容的最后,要附上stackoverflow的原文链接
1919

2020
每个人可以做(但不限于):
@@ -67,6 +67,8 @@ stackoverflow-Java-top-qa
6767
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
6868
* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
6969
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
70+
* [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md)
71+
7072

7173
> 网络
7274
@@ -140,7 +142,6 @@ stackoverflow-Java-top-qa
140142
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
141143
- [How do I “decompile” Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
142144
- [Useful Eclipse Java Code Templates [closed]](http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates)
143-
- [Which @NotNull Java annotation should I use?](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use)
144145
- [How to call SOAP web service in Android](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-in-android)
145146

146147
### contributors

‎contents/Which @NotNull Java annotation should I use.md

Copy file name to clipboardExpand all lines: contents/Which @NotNull Java annotation should I use.md
-53Lines changed: 0 additions & 53 deletions
This file was deleted.
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
我应该用哪一个@NotNull注解?
2+
===
3+
我希望能通过注解的方式,尽量避免程序中出现空指针问题,同时既能保障代码的可读性,又能和IDE的代码检查,静态代码扫描工具结合起来。相关的注解,我看到有好多种@NotNull/@NonNull/@Nonnull,而他们彼此间又有冲突,不能共用,下面是我找到的一些注解,哪个是最好的选择呢?
4+
5+
1.javax.validation.constraints.NotNull
6+
7+
运行时进验证,不静态分析
8+
9+
2.edu.umd.cs.findbugs.annotations.NonNull
10+
11+
用于finbugs和Sonar静态分析
12+
13+
3.javax.annotation.Nonnull
14+
15+
只适用FindBugs,JSR-305不适用
16+
17+
4.org.jetbrains.annotations.NotNull
18+
19+
适用用于IntelliJ IDEA静态分析
20+
21+
5.lombok.NonNull
22+
23+
适用Lombok项目中代码生成器。不是一个标准的占位符注解.
24+
25+
6.android.support.annotation.NonNull
26+
27+
适用于Android项目的标记注解,位于support-annotations包中
28+
29+
回答
30+
---
31+
32+
我推荐用javax命名空间下的注解(虽然我喜欢Lombok和Intelij做的事情),使用其他命名空间的注解,等于你还需要引入其他依赖。
33+
34+
我用javax.validation.constraints.NotNull,因为它已经在Java EE 6中定义
35+
36+
javax.annotation.NonNull可能直到java 8都不存在(正如Stephen指出)。其他的都不是标准的注解 .
37+
38+
如果注解是可扩展的,那将是一件美好的事情.你可以自己写一个`non-null`注解,然后继承上面说的这些注解。如果标准的注解不支持某个特性,你就可以在自己定义的注解里面扩展。
39+
40+
41+
42+
43+
44+
stackoverflow链接: http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use
45+
46+
47+
48+
49+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.