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,

Tom
  • 1,414
  • 12
  • 21
  • I think you should read this : http://types.cs.washington.edu/jsr308/ – Svetlin Zarev Mar 20 '14 at 11:34
  • possible duplicate of [Java 8, Type Annotations and JSR 308](http://stackoverflow.com/questions/20197440/java-8-type-annotations-and-jsr-308) – Svetlin Zarev Mar 20 '14 at 11:37
  • @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. – Tom Mar 20 '14 at 11:46
  • @SvetlinZarev It's not exactly a duplicate; my question is specifically about Eclipse. The mentioned question is about type annotations in general. – Tom Mar 20 '14 at 11:48
  • 1
    @Tom What is `@NonNull`'s package? The annotation should be defined with `@Target(ElementType.TYPE_USE)`. – sndyuk Mar 20 '14 at 17:06

1 Answers1

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!

Tom
  • 1,414
  • 12
  • 21