在 Java 中使用 @Nullable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22988243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:48:07  来源:igfitidea点击:

Using @Nullable in Java

java

提问by Slimu

I have a question regarding the usage of @Nullableannotation in Java.

我有一个关于@Nullable在 Java 中使用注解的问题。

From what I've read, it's a good practice to set in on methods that may return a null value. In this way, the IDE may help detect some null pointer exception errors or suggest the removal unnecessary null pointer exception checks if @NotNullis used.

从我读过的内容来看,设置可能返回空值的方法是一个很好的做法。这样,IDE 可以帮助检测一些空指针异常错误或建议删除不必要的空指针异常检查(如果@NotNull使用)。

So far so good, but what about using @Nullablefor method parameters? Is this a good practice or will the code become even more verbose (if used with final) and the benefit may be missing since you don't always know what arguments will be passed to the function call? Also, what is your opinion on using @Nullablewith setter methods?

到目前为止一切顺利,但是@Nullable用于方法参数呢?这是一个很好的做法,还是代码会变得更加冗长(如果与 一起使用final)并且可能会丢失好处,因为您并不总是知道将哪些参数传递给函数调用?另外,您对使用@Nullablesetter 方法有什么看法?

The usage is related to a situation when working in a company and not to a small project (like a homework).

用法与在公司工作时的情况有关,而不是与小项目(如家庭作业)有关。

采纳答案by kAnNaN

Due to the inherent complexity, flow analysis is best performed in small chunks. Analyzing one method at a time can be done with good tool performance - whereas whole-system analysis is out of scope for the Eclipse Java compiler. The advantageis: analysis is fast and can be done incrementally such that the compiler can warn you directly as you type. The down-side: the analysis can not "see" which values (null or non-null) are flowing between methods (as parameters and return values).

This is where null annotationscome into play. By specifying a method parameter as @NonNullyou can tell the compiler that you don't want a null value in this position.

由于固有的复杂性,流分析最好在小块中执行。一次分析一种方法可以通过良好的工具性能来完成 - 而整个系统分析超出了 Eclipse Java 编译器的范围。的优点是:分析速度快,可以逐步做到这样编译器可以直接警告你,你键入。所述下侧:分析不能“看到”该值(null或非空)的方法之间流动(如参数和返回值)。

这就是空注释发挥作用的地方。通过指定方法参数,@NonNull您可以告诉编译器您不希望此位置出现空值。

Reference

参考

Reference 2

参考文献2

Usage:This link explainswhat annotation to use where.

用法:链接解释了在何处使用什么注释。

Usage 2

用法2

Getters/Setters: Yes, it is possible. The Project Lombok (http://projectlombok.org/index.html) defines annotations for generating getters/setters and more.

So for example

@lombok.Data;
public class Person {
   private final String name;
   private int age;
}

Will generate getter for name (not setter since it is final) and getter/setter for age. It will also generate equals, hashCode, toStringand construtor initializing required fields (name). Adding @AllArgsConstructorwould generate constructor initializing both fields.

There are other annotations and parameters giving you control over access rights (should your getter be protected or public), names (getName or name?), etc. And there is more. For example, I really like the extension methods.

Lombok is very easy to use. Just download the jar and use the annotations, then the getter/setters can be used in your code without actually being spelled out. Moreover, IDE's like Netbeans support this, so that you see the getter/setter in code completion, navigation, etc. The annotations are used only during compilation not during runtime, so you don't distribute lombok with your jar's.

NotNull: This is supported by findbugs and IdeaJ IDE, maybe others

Getters/Setters: 对的,这是可能的。Project Lombok ( http://projectlombok.org/index.html) 定义了用于生成 getter/setter 等的注释。

所以例如

@lombok.Data;
public class Person {
   private final String name;
   private int age;
}

将生成 name 的 getter(不是 setter,因为它是最终的)和 getter/setter 的年龄。它还将生成equals, hashCode, toString和构造器初始化所需的字段(名称)。添加@AllArgsConstructor将生成初始化两个字段的构造函数。

还有其他注释和参数可以让您控制访问权限(您的 getter 应该受到保护还是公开)、名称(getName 或 name?)等。还有更多。例如,我非常喜欢扩展方法。

Lombok 非常易于使用。只需下载 jar 并使用注释,然后 getter/setter 就可以在您的代码中使用,而无需实际拼写。此外,IDE 之类的 Netbeans 支持这一点,因此您可以在代码完成、导航等中看到 getter/setter。注释仅在编译期间使用,而不是在运行时使用,因此您不会将 lombok 与 jar 一起分发。

NotNull:这由 findbugs 和 IdeaJ IDE 支持,也许其他

Reference 3

参考文献 3

回答by ifloop

For own, little projects one doesn't have to use this. But when creating libraries for others, this may help your API users to write robust applications. IMO

对于自己的小项目,人们不必使用它。但是当为其他人创建库时,这可能有助于您的 API 用户编写健壮的应用程序。海事组织

As I posted in the comment: See nullable annotation usagefor a good answer to that particular question.

正如我在评论中发布的那样:请参阅可空注释用法以获得对该特定问题的良好答案。

回答by Mason T.

The usage of @Nullable is to let a user and the compiler know it is fine to allow a null value in as that parameter.

@Nullable 的用法是让用户和编译器知道允许空值作为该参数是可以的。

I believe it is a more straight-forward way of letting a user looking at your API to know that passing null will not create a NPE or undefined behavior.

我相信这是一种更直接的方式,让用户查看您的 API 以了解传递 null 不会创建 NPE 或未定义的行为。

For setter, if that field is going to be used in many places, it would be nice to look at it's setter and discover null is handled.

对于 setter,如果该字段将在许多地方使用,那么查看它的 setter 并发现 null 被处理会很好。

That being said I think it should only be used for API's or libraries, using them in your own code would create too much useless code.

话虽如此,我认为它应该只用于 API 或库,在您自己的代码中使用它们会创建太多无用的代码。