简体   繁体   English

如何在包上使用NonNullByDefault

[英]How to use NonNullByDefault on a package

My compiler warns me about A default nullness annotation has not been specified for the package . 我的编译器警告我A default nullness annotation has not been specified for the package I'm using Juno with Java 7. 我正在使用Juno和Java 7。

The documentation stats you should add @NonNullByDefault as a package option, but I don't know what that means. 您应该将@NonNullByDefault作为包选项添加文档统计信息,但我不知道这意味着什么。

Can I somehow set NonNullByDefault for the whole package? 我可以以某种方式为整个包设置NonNullByDefault吗? In Eclipse I can't right click on a package and say add option or something similar. 在Eclipse中,我无法右键单击包并说出add option或类似的东西。

Or do I just add @NonNullByDefault to any class in a package and then it's valid for all beans in that package? 或者我只是将@NonNullByDefault添加到包中的任何类,然后它对该包中的所有bean都有效? Or do I have to create some meta info file in the pakage to add package options? 或者我是否必须在pakage中创建一些元信息文件以添加包选项? I must be blind because I can't find any help online.... 我必须是盲人,因为我在网上找不到任何帮助....

A small example would be great. 一个小例子会很棒。

Summary: 摘要:
Getting the above advice to work was not intuitive to me. 获得上述建议对我来说并不直观。 Nor was the actual solution I just figured out. 我刚刚想出的实际解决方案也不是。 In the package-info.java file: 在package-info.java文件中:
a. 一个。 put the annotation ABOVE the package declaration 把注释放在包声明上面
b. put the NonNullByDefault annotation import AFTER the package declaration 在包声明后输入NonNullByDefault注释


Details: 细节:
I don't know if I am the only person who read the above answers and couldn't figure out how to get this working. 我不知道我是否是唯一一个阅读上述答案并且无法弄清楚如何使其工作的人。 So, just to be extra clear, here's the (non-obvious nor intuitive) steps I took to get this to finally work in Eclipse-Juno: 所以,为了更清楚,这里是我为了最终在Eclipse-Juno中工作而采取的(非显而易见的或直观的)步骤:

  1. In Package Explorer, goto the root Java package showing the warning (or error) message starting with " A default nullness annotation has not been specified for the package... " - for this example, please assume the package name of "org.public_domain" 在Package Explorer中,转到显示警告(或错误)消息的根Java包,该消息以“ 尚未为包指定默认空值注释...开头 - 对于此示例,请假设包名称为“org.public_domain” “
  2. On the Java package, activate the right-click-menu and select the " New -> Package " option - yes, I know your package already exists, consider it an eclipse idiosyncrasy - it's not going to do anything to your existing package 在Java包上,激活右键菜单并选择“ New - > Package ”选项 - 是的,我知道你的包已经存在,认为它是一个eclipse特性 - 它不会对你现有的包做任何事情
  3. When the " New Java Package " dialog appears, check the " Create package-info.java " checkbox and click "Finish" 出现“ New Java Package ”对话框时,选中“ Create package-info.java ”复选框,然后单击“Finish”
  4. Your Java editor will now have a new file titled " package-info.java " displayed with the following content: 您的Java编辑器现在将显示一个名为“ package-info.java ”的新文件,其中包含以下内容:

     /** * */ /** * @author xyz * */ package org.public_domain; 
  5. Now, rearrange the file content to look like this: 现在,重新排列文件内容,如下所示:

     /** * */ /** * @author xyz * */ @NonNullByDefault package org.public_domain; import org.eclipse.jdt.annotation.NonNullByDefault; 

    ...or like this... ......或者像这样......

     /** * */ /** * @author xyz * */ @org.eclipse.jdt.annotation.NonNullByDefault package org.public_domain; 
  6. Enjoy the awesomeness that is eclipse @NonNullByDefault 享受eclipse @NonNullByDefault的精彩

My challenge was figuring out that I was to put the annotation ABOVE the package declaration . 我的挑战是弄清楚我要把注释放在包装声明上面 And to put the NonNullByDefault annotation import AFTER the package declaration . 在程序包声明后输入NonNullByDefault批注 For me, that was not intuitive. 对我来说,这不直观。

Hope this saves someone else the time I've ended burning up chasing down this particular pathway. 希望这能节省别人我已经结束燃烧追逐这条特殊途径的时间。

If you simply want to get rid of the warning and ignore this feature because you're not making use of it, you can simply disable it in the Eclipse preferences: 如果您只是想摆脱警告并忽略此功能,因为您没有使用它,您可以在Eclipse首选项中禁用它:

Window > Preferences > Java > Compiler > Errors/Warnings > Null analysis > Missing '@NonNullByDefault' annotation on package = [Ignore] 窗口>首选项> Java>编译器>错误/警告>空分析>在包上缺少'@NonNullByDefault'注释= [忽略]

See the source code or JavaDocs: 查看源代码或JavaDocs:

Note that for applying an annotation to a package a file by the name
package-info.java is used.

And: 和:

@Target({PACKAGE,TYPE,METHOD,CONSTRUCTOR})

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

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