Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is NotNull in java? [duplicate]

Tags:

java

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?

like image 823
user1244932 Avatar asked Mar 03 '17 10:03

user1244932


People also ask

What is NotNull Java?

notNull() is a static method of the Validate class that is used to check whether the passed object is null or not. If the passed object is null , then the method raises an exception with a formatted message. If the passed object is not null , then the method returns the input as it is.

What is the use of @NotNull?

The @NotNull Annotation is, actually, an explicit contract declaring the following: A method should not return null. A variable (like fields, local variables, and parameters) cannot should not hold null value.

Where is null in Java?

Null is a reserved keyword in the Java programming language. It's technically an object literal similar to True or False. Null is case sensitive, like any other keyword in Java. When programming in Java, it's important to write null in lowercase.


1 Answers

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.

like image 54
Kayaman Avatar answered Sep 22 '22 04:09

Kayaman