'@NotNull' not applicable to type use

I would like to use the @NotNull and @Nullable annotations on types, as was introduced with java 8. I understood that IDEA 13.1 supports java 8, and therefore also type annotations.
I've set up my project to use language level 8.0, and I've added the annotations.jar (from <IDEA>/lib) to my module's dependencies.

However, when I create a class like this:

public class Test {
    private List<@NotNull String> list;
}

I'm getting an error on @NotNull, saying "'@NotNull' not applicable to type use".

I'm new to IDEA, and I wanted to see if it handles @NotNull type annotations better than Eclipse does, but so far I haven't been able to get it working. Obviously I missed something, but I wouldn't now what. Any help is appreciated.

Thanks,
Tom

6 comments
Comment actions Permalink

I'm not sure if I fully understand what's being said there. Does this mean that the Intellij @NotNull/@Nullable annotations can't be used as type annotations?

I got the impression that Intellij supports all new Java 8 features, including type annotations, but from the response in this ticket I understand that I need to write my own @NotNull/@Nullable annotations, or start using those from the Checker Framework? Does this mean that I'll have to replace all existing @NotNull/@Nullable annotations in my code with other ones? And will the compiler or analyzer actually use them, to generate warnings/errors?

I suspect I'm don't fully understand the issue here. Why is there no Java 8 @NotNull annotation available?

Thanks,
Tom

0
Comment actions Permalink

Tom,

unfortunately jvm 7 doesn't support new targets. That's why we have to provide 2 different packages and leave the user to deside which one to use or smth like that - which is not the best solution. So we try to trick the old VMs and need some time for that.

Sorry for the inconvenience.
Anna

0
Comment actions Permalink

Ok, I think I understand what you're saying: it's not possible to provide an annotations jar with the new type annotations, because JVMs < 8 don't support the new annotation target that is required.

I would be happy to choose from 2 different annotation jars, if that means that I can go ahead and start using the new type annotations. Are you saying that the new java 8 annotations jar is already available, or that you are working on it (and when can we expect a solution)?
Maybe you should have a look at how this was solved by Eclipse; they already support the new type annotations.


Kind regards,
Tom

0
Comment actions Permalink

For anyone reading this today, here is a solution to Tom's problem:

1. Define a NonNull.java annotation:

package org.your.project.package;

import java.lang.annotation.*;

/**
* you have to define your own NonNull annotation to be able to mark a generic as not null
* because IDEA does not allow using it's @NonNull for java 7 compatibility reasons
*/
@Documented
@Retention(RetentionPolicy.CLASS)
@Target(value=ElementType.TYPE_USE) // this is the parameter that was introduced in java 8
public @interface NonNull {
String value() default "";

Class<? extends Exception> exception() default Exception.class;
}

2. Go to Settings -> Build, Execution, Deployment -> Compiler, press Configure annotations... and add the annotation you defined to the NotNull annotations section using the green cross button as described here: https://www.jetbrains.com/help/idea/nullable-notnull-configuration-dialog.html.

Voila:

It would probably be a better practice to use some existing annotation library, like eclipse one or findbugs one, than defining your own, but I didn't try them and I don't know which of them supports the `@Target(value=ElementType.TYPE_USE)`, so I can't suggest any specific library.

0
Comment actions Permalink

Today JetBrains publish 2 versions of annotation.jar, one for java < 1.8 and one with type annotations support for java since 1.8. There is no need to create your custom annotations, please don't do this!

Anna

0

Please sign in to leave a comment.