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 49fe4b9

Browse filesBrowse files
committed
Merge pull request giantray#29 from lxn348567248/master
我应该用哪一个@NotNull注解
2 parents 18b2564 + b7ce650 commit 49fe4b9
Copy full SHA for 49fe4b9

File tree

Expand file treeCollapse file tree

1 file changed

+53
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+53
-0
lines changed
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
我应该用哪一个@NotNull注解?
2+
===
3+
4+
我试图代码的可读性像我们的开发工具IDE一样进行代码检查、静态代码分析(FindBugs,Sonar)避免空指针.大多数的开发工具是不兼容的,对于@NotNull/@NonNull/@Nonnull注解,对于理解它们是非常困难的。那么那一个是最好的选择?下面是我找到的对于注解的说明:
5+
6+
7+
1.javax.validation.constraints.NotNull
8+
9+
运行时进验证,不静态分析
10+
11+
12+
2.edu.umd.cs.findbugs.annotations.NonNull
13+
14+
用于finbugs和Sonar静态分析
15+
16+
3.javax.annotation.Nonnull
17+
18+
只适用FindBugs,JSR-305不适用
19+
20+
4.org.jetbrains.annotations.NotNull
21+
22+
适用用于IntelliJ IDEA静态分析
23+
24+
5.lombok.NonNull
25+
26+
适用Lombok项目中代码生成器。不是一个标准的占位符注解.
27+
28+
6.android.support.annotation.NonNull
29+
30+
适用于Android项目的标记注解,位于support-annotations包中
31+
32+
回答
33+
---
34+
35+
我只用javax命名空间下的注解(虽然我喜欢Lombok和Intelij做的事情).
36+
37+
我用javax.validation.constraints.NotNull,因为它已经在Java EE 6中定义
38+
39+
javax.annotation.NonNull可能直到java 8都不存在(正如Stephen指出)。其他的都不是标准的注解 .你可能在语法上得到帮助而不能在运行时给你帮助,对于许多项目,这是好的,但是对于我这不是合适的.
40+
41+
如果注解是可扩展的,那将是一件美好的事情.这样,我们会能过封装和继承定义的的null注解.当标准解决了这个问题,你就不得不重新定义你的注解。
42+
43+
不幸的,这种事情没有发生.
44+
45+
46+
47+
48+
stackoverflow链接: http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use
49+
50+
51+
52+
53+

0 commit comments

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