简体   繁体   English

@Nonnull默认情况下在eclipse / kepler中

[英]@Nonnull by default in eclipse/kepler

In eclipse, it's possible to set not null by default for a single package. 在eclipse中,默认情况下可以为单个包设置非null。 Is there any way to set it across all packages, or for a tree of packages? 有没有办法在所有包或一个包树中设置它?

Edit 编辑

I'm talking about the @Nonnull annotations that eclipse respects and polices. 我在谈论日食尊重和政策的@Nonnull注释。 At the moment, we are putting them on every function, every variable every parameter. 目前,我们将它们放在每个函数,每个参数的每个变量上。 I know we can just set them package wide, by putting the attribute in package-info - but we have hundreds of packages, and it's easy to miss one. 我知道我们可以通过将属性设置在package-info中来设置它们的包宽度,但是我们有数百个包,而且很容易错过一个包。 I am hoping to be able to set eclipse to say "assume @Nonnull by default, unless @Nullable is specified". 我希望能够设置eclipse说“默认情况下假设@Nonnull ,除非指定@Nullable ”。

Eclipse can help you detect any packages that are lacking a @NonNullByDefault if you enable the compiler option "Missing '@NonNullByDefault' annotation on package". 如果在包上启用编译器选项“Missing'@NonNullByDefault'注释”,Eclipse可以帮助您检测缺少@NonNullByDefault的任何包。 This option is set to "Ignore" by default, set to "Warning" or "Error" as you like. 默认情况下,此选项设置为“忽略”,根据需要设置为“警告”或“错误”。

See http://help.eclipse.org/luna/topic/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm?cp=1_4_2_0_3_1 请参阅http://help.eclipse.org/luna/topic/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm?cp=1_4_2_0_3_1

Try this cute little project: 试试这个可爱的小项目:

https://code.google.com/p/notnullcheckweaver/ https://code.google.com/p/notnullcheckweaver/

It inserts null checks around every element annotated with @NotNull 它在每个使用@NotNull注释的元素周围插入空检查

Its default setting is @Nullable, though, so you should still annotate your (root) package, to get it to enforce everything except @Nullable annotated elements. 它的默认设置是@Nullable,所以你仍然应该注释你的(root)包,让它强制执行除@Nullable注释元素之外的所有东西。

Drawbacks: 缺点:

  • It introduces yet another @NotNull annotation and does not allow others (eg java's own @Nonnull, spring annotations, etc.) 它引入了另一个@NotNull注释,并且不允许其他注释(例如java自己的@Nonnull,spring注释等)
  • It works through bytecode manipulation and java-agents 它通过字节码操作和java代理来工作

Are you sure you want to add @Notnull to every function, every variable and every parameter in your project (keeping in mind that you have hundreds of packages)? 您确定要将@Notnull添加到项目中的每个函数,每个变量和每个参数(请记住,您有数百个包)吗?

What if you function needs to return null or parameter needs to be null? 如果你的函数需要返回null或参数需要为null怎么办?

I don't think this is a good idea. 我不认为这是个好主意。

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

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