简体   繁体   中英

How do I use the Eclipse External null Annotations (EEAs) for the JDK/JRE and 3rd-party libraries inside Eclipse?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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