简体   繁体   English

IDEA中是否有@NonNullByDefault注释?

[英]Is there a @NonNullByDefault annotation in IDEA?

Eclipse has the @NonNullByDefault annotation, which treats all values as @NonNull unless you explicitly annotate them as @Nullable . Eclipse具有@NonNullByDefault注释,它将所有值视为@NonNull除非您明确地将它们注释为@Nullable

Is there an equivalent option in IntelliJ IDEA, or do you have to always use @Nonnull ? IntelliJ IDEA中是否有等效选项,或者您是否必须始终使用@Nonnull

Idea version 14 will include support for the JSR 305 "@TypeQualifierDefault" annotation, which allows the user to create a custom annotation, to be used on a package declaration in a package-info.java file, that specifies that everything in that package (not just parameters, but method return values, local variables, etc.) will be implicitly annotated as not allowing null values. Idea版本14将包括对JSR 305“@TypeQualifierDefault”注释的支持,该注释允许用户创建自定义注释,以用于package-info.java文件中的包声明,该文件指定该包中的所有内容(不仅仅是参数,而且方法返回值,局部变量等)将被隐式注释为不允许空值。

Unfortunately, this doesn't (currently) recursively affect subpackages, so each subpackage has to have a package-info.java file too, declaring that subpackage to use the annotation. 不幸的是,这不会(当前)递归地影响子包,因此每个子包也必须有一个package-info.java文件,声明该子包使用注释。

See here for details and an example of use: 有关详细信息和使用示例,请参见此处:

http://youtrack.jetbrains.com/issue/IDEA-125281 http://youtrack.jetbrains.com/issue/IDEA-125281

Note that this is already implemented in Early Access Program (EAP) builds. 请注意,这已在早期访问程序(EAP)版本中实现。

No, it is currently not supported by IDEA. 不,IDEA目前不支持它。

As a proof, see lena's link about the open feature request to allow 'NotNull' as the default element behavior for a given class or package . 作为证明,请参阅lena关于开放特征请求的链接,以允许'NotNull'作为给定类或包的默认元素行为

Maybe a similar feature will be become standard with JSR-305 , which may include the @ParametersAreNonnullByDefault annotation and also the opposite annotation @ParametersAreNullableByDefault . 也许类似的功能将成为JSR-305的标准功能,其中可能包括@ParametersAreNonnullByDefault注释以及相反的注释@ParametersAreNullableByDefault Note that in contrast to @NonNullByDefault , return values are not covered by those two annotations. 请注意,与@NonNullByDefault ,这两个注释不包含返回值。 So, you still had to annotate the return value explicitely. 因此,您仍然需要明确地注释返回值。

All that doesn't change the current state, though. 但是,所有这些都不会改变目前的状态。 Neither has JSR-305 become a standard, nor does IDEA implement it. JSR-305也没有成为标准,IDEA也没有实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM