【问题标题】:where is NotNull in java? [duplicate]java中的NotNull在哪里? [复制]
【发布时间】:2017-07-23 10:31:24
【问题描述】:

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

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

但是idea看不懂这样的代码,建议补充:

import com.sun.istack.internal.NotNull;

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
}

我收到错误:

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

我以为NotNull是在1.8里加的? 如何在ideagradle 中使用NotNull

【问题讨论】:

标签: java


【解决方案1】:

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

Java Bean 验证框架(JSR-303)有javax.validation.constraints.NotNull,它由(例如)Hibernate 验证器实现。这可能就是你要找的。​​p>

【讨论】:

    【解决方案2】:

    我认为您应该添加 org.jetbrains.annotations.NotNull,因为 IntelliJ IDEA IDE 使用它进行静态分析。 参考:
    IntelliJ Documentation
    This SO Question

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 2015-01-25
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多