Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable JSR308 null annotations on types in Eclipse?

With the release of Eclipse's java 8 support, I understood that null annotations on types (JSR 308) were possible, as described here. I have installed JDK8, and the Java 8 feature patch for Eclipse Kepler. I was expecting to be able to declare a list that does not allow nulls like this:

List<@NonNull String> nonulls;

However, the compiler tells me that "The annotation @NonNull is disallowed for this location" :(

My project is configured to use compiler compliance level 1.8, and the org.eclipse.jdt.annotation jar is included in the class path.

What am I missing here?

Regards,

like image 276
Tom Avatar asked Mar 20 '14 11:03

Tom


1 Answers

The problem was caused by the way I added the Eclipse annotation jar to the project's build path. I used the Quick Fix called "Copy library with default null annotations to build path", which adds an old version of the jar (org.eclipse.jdt.annotation_1.1.0.v20140129-1625.jar). This version is the pre-java-8 version, and does not support type annotations.

The fix is to put the correct (java-8) version of the jar on the build path. This version came with the Kepler Java 8 feature patch, and is located in the 'plugins' directory in the Eclipse installation directory: org.eclipse.jdt.annotation_2.0.0.v20140317-1808.jar. If you add this jar to your build path, type annotations work fine.

Credits to Thomas Schindl!

like image 89
Tom Avatar answered Oct 31 '22 15:10

Tom