Java中的NotNull在哪里?

use*_*932 6 java

我想用oracle jdk 1.8编译简单的代码:

class Foo {
   public static void test(@NotNull String a) {}
}
Run Code Online (Sandbox Code Playgroud)

但是idea不了解这样的代码,建议添加:

import com.sun.istack.internal.NotNull;
Run Code Online (Sandbox Code Playgroud)

这里面编译后idea的作品,但如果运行构建通过gradle这样的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
}
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

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
Run Code Online (Sandbox Code Playgroud)

我以为是NotNull在添加1.8?如何使用NotNullin ideagradle

Kay*_*man 6

@NotNull标准JDK中没有供一般使用的注释(com.sun.internal.istack.NotNull绝对不是供您使用的注释,因为它是内部类)。

Java Bean验证框架(JSR-303)具有javax.validation.constraints.NotNull,由(例如)Hibernate验证器实现。那可能就是您要寻找的。