8

I want to compile simple code with oracle jdk 1.8:

class Foo {
   public static void test(@NotNull String a) {}
}

But idea not understand such code, and suggest to add:

import com.sun.istack.internal.NotNull;

after that compilation inside idea works, but if run build by gradle with such build.gradle:

version '1.0-snaphshot_03.03.2017'

apply plugin: 'java'

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

artifacts {
    archives sourcesJar
}

I got error:

error: package com.sun.istack.internal does not exist
import com.sun.istack.internal.NotNull;

 error: cannot find symbol
    public static void test(@NotNull String a) {
                                  ^
  symbol:   class NotNull
  location: class Foo

I thought that NotNull was added in 1.8? How can I make possible of usage of NotNull in idea and gradle?

user1244932
  • 7,352
  • 5
  • 46
  • 103

2 Answers2

7

There is no @NotNull annotation in the standard JDK that would be meant for general use (the com.sun.internal.istack.NotNull is definitely not for your usage, as it's an internal class).

The Java Bean validation framework (JSR-303) has javax.validation.constraints.NotNull, which is implemented by (for example) Hibernate validator. That's probably what you're looking for.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

I think you should be adding org.jetbrains.annotations.NotNull as this is Used by IntelliJ IDEA IDE for static analysis. Refer :
IntelliJ Documentation
This SO Question

Community
  • 1
  • 1
boredDev
  • 317
  • 3
  • 18