英文:
How do I use the Eclipse External null Annotations (EEAs) for the JDK/JRE and 3rd-party libraries inside Eclipse?
问题
我有一个项目,在其中我正在使用 Eclipse 的空值检查,该检查受到 org.eclipse.jdt.annotation
的 @NonNull
、@Nullable
和 @NonNullByDefault
支持。正如许多地方所指出的,如果没有全面的第三方支持,尤其是在 JDK/JRE 中,这些注解和检查器的效用就会降低。这种支持是通过使用 Eclipse 外部空值注解(EEAs)来实现的。
我找到了指向 Eclipse 帮助页面 和 lastnpe github 仓库的链接,该仓库托管了一系列的 EEAs。不幸的是,这些帮助页面被设计成通用的,而且 github 仓库实际上只提供了与 Maven 和 Maven-Tycho 集成的说明。这里相关的问题通常涉及具体的问题,而不是首次设置连接 EEAs 的基本任务。
我的问题是:我如何在 Eclipse 中使用 lastnpe github 仓库?具体来说,对于给定的 JRE,我如何将 EEAs 连接到 JRE?类似地,对于第三方库,我如何将 EEAs 连接到该库?
英文:
I have a project in which I'm using the Eclipse null checking that's supported by org.eclipse.jdt.annotation
's @NonNull
, @Nullable
and @NonNullByDefault
. As has been noted in many places, the utility of the annotations and the checker is reduced without comprehensive 3rd-party support — especially in the JDK/JRE. This support is facilitated through the use of Eclipse External null Annotations (EEAs).
I found links to Eclipse help pages and to the lastnpe github repository that hosts a collection of EEAs. Unfortunately, the help pages were designed to be generic and the github repository really only provides instructions for maven and maven-tycho integration. Related questions here generally address specific issues and not the basic task of hooking up the EEAs in the first place.
My question: how do I use the lastnpe github repository inside Eclipse? Specifically, given a JRE, how do I connect the EEAs to the JRE? And similarly, given a 3rd-party library, how do I connect the EEAs to that library?
答案1
得分: 3
回答我的问题,在调查后。
首先,克隆最后一个非空引用扩展增强(lastnpe)github 存储库。假设你将其放在 /repos/eeas
中。这将成为你的JRE/JDK和第三方库的注解来源。
适用于JRE/JDK的扩展增强注解(EEAs)
你可以在每个项目中单独设置这些,也可以在Eclipse首选项中全局设置。Eclipse的帮助页面详细介绍了如何操作;我将重点介绍你应该使用的lastnpe存储库中的路径。简单地说,在首选项中,转到Java > 已安装的JRE。然后,选择所需的JRE并点击编辑。
如果你使用的是Java 8,你可能会发现你的JRE有很多JAR文件。你可以将它们全部标记,并同时为所有JAR文件设置相同的外部注解,方法是点击“外部注解…”。
这会弹出一个对话框。然后,选择“外部位置”,点击“外部文件夹…”以浏览到以下路径,或输入以下路径:
/repos/eeas/libraries/java
然后点击确定并将更改应用到首选项中。
现在,你应该会发现JRE中的某些方法已经被注解。例如,给定以下代码:
"foo".split("bar");
如果你将鼠标悬停在split
上,你将看到split
返回一个@NonNull String @NonNull []
。
这将让你快速评估你是否已经正确添加了JRE的EEAs。请注意,并非JRE中的所有内容都被注解,因此你可能会认为在添加注解时出了问题,而实际上你只是在使用一个未经注解的方法。
第三方库的EEAs
第三方库的过程类似。例如,如果你使用Guava,你可以右键单击项目,选择配置构建路径,转到库选项卡,展开guava JAR,选择外部注解,然后点击编辑…
如上所述,你需要提供指向存储库中guava文件夹的路径,就像这样:
/repos/eeas/libraries/guava
你可以通过调用Maps.newHashMap()
并将鼠标悬停在newHashMap
上来验证这是否起作用。你将会看到它返回一个@NonNull HashMap。
英文:
Answering my own question, after investigation.
First, clone the lastnpe github repository. Let's say you put it in /repos/eeas
. This will be your source of annotations for both the JRE/JDK and for 3rd-party libraries.
EEAs for the JRE/JDK
You can set these on a per-project basis or globally in your Eclipse preferences. The Eclipse help pages walk through the mechanics of doing this; I'll focus on the path within the lastnpe repo that you should use. Briefly, in your preferences, go to Java > Installed JREs. Then, select the JRE you want and click Edit.
If you use Java 8, you may find that your JRE has a lot of JARs. You can highlight all of them and set the same external annotations for all the JARs at the same time, by clicking on External annotations...
This brings up a dialog. You then select "External location" and click "External folder..." to browse to or enter the following path:
/repos/eeas/libraries/java
Then click OK and Apply your changes to the preferences.
You should now find that certain methods in the JRE are annotated. For example, given the code:
"foo".split("bar");
if you hover over split
, you will see that split
returns a @NonNull String @NonNull []
.
This will let you quickly assess that you've correctly added the EEAs for your JRE. Note that not everything in the JRE is annotated, so you might be believe that you've failed to hook up the annotations when, in fact, you're just using an unannotated method.
EEAs for 3rd-Party Libraries
The process for 3rd-party libraries is similar. For example, if you use guava, you can right-click on your project, select Configure Build Path, go to the Libraries tab, expand the guava JAR, select External annotations, and click Edit...
As above, you need to give the path to the guava folder in the repo, like this:
/repos/eeas/libraries/guava
You can verify that this is working by calling Maps.newHashMap()
and hovering over newHashMap
. You will see that it returns a @NonNull HashMap.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论