5

IntelliJ is suggesting to import com.sun.istack.internal.NotNull the @NotNull annotation (which is wrong) in the following program:

public class Test implements Comparable<Test> {
    @Override
    public int compareTo(@NotNull Test o) {
        return 0;
    }
}

When trying the correct import com.intellij.annotations.NotNull (I think) it looks like it can't find the class:

enter image description here

traveh
  • 2,700
  • 3
  • 27
  • 44

4 Answers4

3

You can Alt+Enter on the warning before you add the annotation, press Right, choose Edit Inspection Settings, then Configure Annotations and specify the annotation you want to be inserted there.

Peter Gromov
  • 17,615
  • 7
  • 49
  • 35
  • I tried it - `org.jetbrains.annotations.NotNull` was already selected as default. – traveh Jul 07 '15 at 07:23
  • 1
    IDEA assumes you have the jar with the chosen annotation in the classpath and just adds a reference. For this annotation, you need to have annotations.jar in the dependencies (which've you already found how to add). It wasn't clear from your question that you didn't actually want another NotNull annotation (from another library that you already have) to be inserted. – Peter Gromov Jul 07 '15 at 18:13
  • Well, the thing is that apparently IntelliJ automatically inserts the `@org.jetbrains.annotations.NotNull` annotation to the `Comparable<>` interface and then complains about it... seems like some kind of bug to me, or at least unfriendly design, but as long as it's the normal behavior I'm not concerned. – traveh Jul 08 '15 at 05:38
2

Remove the import from the code. IntelliJ will ask you to import it again. It should suggest that there are multiple choices available. The annotation you want should be in the list.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • That's the first thing I tried. It only suggests `com.sun.istack.internal.NotNull`... – traveh Jul 06 '15 at 10:33
  • 1
    Now I see that it also suggests to add `annotations.jar` to classpath and if I do, it adds `import org.jetbrains.annotations.NotNull`, which seems to be right, but I still don't get why I need to do it manually. – traveh Jul 06 '15 at 10:39
2

I found I had to add the following dependency declaration to my Java project's build.gradle file before I was able to import the org.jetbrains.annotations.NotNull and org.jetbrains.annotations.Nullable annotations rather than the com.sun.istack.internal.NotNull and com.sun.istack.internal.Nullable annotations:

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.jetbrains:annotations:15.0'
}
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
0

For later versions of Intellij, use Cmd+Shift+A (on MacOS) to bring up the Find Actions menu, then type Configure annotations, select the suggestion to bring up the Compiler preference window.

Then click on the Configure annotations button next to the option Add runtime assertions for notnull-annotated methods and parameters. It will then bring up the list of NonNull and Nullable configuration defaults. Select the one you want as default then set it as default with the check button.

Aion
  • 430
  • 1
  • 5
  • 16