How to enable JSR308 null annotations on types in Eclipse?
Asked Answered
S

1

6

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,

Sophomore answered 20/3, 2014 at 11:15 Comment(5)
I think you should read this : types.cs.washington.edu/jsr308Portray
possible duplicate of Java 8, Type Annotations and JSR 308Portray
@SvetlinZarev The checker framework you are referring to is not what I mean. I'm trying to use Eclipse's NonNull annotations on types, which are supported by the java 8 feature patch I mentioned.Sophomore
@SvetlinZarev It's not exactly a duplicate; my question is specifically about Eclipse. The mentioned question is about type annotations in general.Sophomore
@Sophomore What is @NonNull's package? The annotation should be defined with @Target(ElementType.TYPE_USE).Pu
S
5

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!

Sophomore answered 20/3, 2014 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.